-
Notifications
You must be signed in to change notification settings - Fork 1
/
imgur.c
90 lines (71 loc) · 2.16 KB
/
imgur.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <curl/curl.h>
#define CLIENT_ID "c3d5102cafbba4c"
#define CMDLEN 256
typedef struct {
char *buffer;
size_t size;
} Mem_buffer;
size_t curl_write_memory(char *data, size_t size, size_t elements, void *membuf) {
Mem_buffer *mem = membuf;
size_t total_size = size * elements;
mem->buffer = realloc(mem->buffer, mem->size + total_size + 1);
if (!mem->buffer)
return 0;
memcpy(&(mem->buffer[mem->size]), data, total_size);
mem->size += total_size;
mem->buffer[mem->size] = '\0';
return total_size;
}
int main(int argc, char *argv[]) {
CURL *curl;
CURLcode code;
struct curl_httppost *firstitem = NULL, *lastitem = NULL;
struct curl_slist *headers = NULL;
Mem_buffer mem = { NULL, 0 };
char cmd[CMDLEN], *url, *temp = NULL;
int status;
if (argc != 2) {
fprintf(stderr, "Usage: %s <path/to/image.(png|jpg)>\n", argv[0]);
return EXIT_FAILURE;
}
curl = curl_easy_init();
if (!curl)
return EXIT_FAILURE;
headers = curl_slist_append(headers, "Authorization: Client-ID " CLIENT_ID);
curl_formadd(&firstitem, &lastitem,
CURLFORM_COPYNAME, "image",
CURLFORM_FILE, argv[1],
CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_URL, "https://api.imgur.com/3/upload.xml");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, firstitem);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_memory);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &mem);
code = curl_easy_perform(curl);
if (code != CURLE_OK || !mem.buffer) {
fprintf(stderr, "Error: %s\n", curl_easy_strerror(code));
return EXIT_FAILURE;
}
url = strstr(mem.buffer, "http");
if (url)
temp = strstr(url, "</");
if (!temp) {
system("notify-send 'Upload error'");
return EXIT_FAILURE;
}
*temp = '\0';
snprintf(cmd, CMDLEN, "echo -n %s | xclip -selection c", url);
status = system(cmd);
if (WEXITSTATUS(status) == 127)
fputs(url, stdout); // print the url to stdout if xclip is not present
system("notify-send 'Upload complete'");
free(mem.buffer);
curl_formfree(firstitem);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return EXIT_SUCCESS;
}