Skip to content

Instantly share code, notes, and snippets.

@Lumaio
Created September 13, 2014 01:09
Show Gist options
  • Save Lumaio/fe2a6bb01425f186e1f0 to your computer and use it in GitHub Desktop.
Save Lumaio/fe2a6bb01425f186e1f0 to your computer and use it in GitHub Desktop.
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
void random_string(char * string, size_t length)
{
char chars[62] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"1234567890";
unsigned int num_chars = (unsigned int) length;
unsigned int i;
for (i = 0; i < num_chars; ++i)
{
string[i] = chars[rand()%62];
}
string[num_chars] = '\0';
}
int main() {
srand((unsigned int) time(0));
CURL* curl;
CURLcode res;
FILE *fp;
char* urlbegin = "http://i.imgur.com/";
char* ext = ".png";
char url_array[5];
char* url_pointer;
char* out = "out.png";
random_string(url_array, 5);
url_pointer=&url_array[0];
char* url;
url = malloc(strlen(urlbegin)+1+9);
strcpy(url, urlbegin);
strcat(url, url_pointer);
strcat(url, ".png");
// printf("%s\n", url);
curl = curl_easy_init();
struct stat st;
if (curl)
{
fp = fopen(out, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
stat(out, &st);
if (st.st_size < 1) { printf("Searching...\n"); main(); }
else { printf("Found Image: %s\n", url); }
fclose(fp);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment