All pastes #950330 Raw Edit

Anonymous

public c v1 · immutable
·published 2008-03-20 14:30 UTC
rendered paste body
/**************************************************************************** Copyright (C) 2008 by CaptainCrunch                                      **                                                                          ** This program is free software; you can redistribute it and/or modify     ** it under the terms of the GNU General Public License as published by     ** the Free Software Foundation; either version 2 of the License, or        ** (at your option) any later version.                                      **                                                                          ** This program is distributed in the hope that it will be useful,          ** but WITHOUT ANY WARRANTY; without even the implied warranty of           ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             ** GNU General Public License for more details.                             **                                                                          ** You should have received a copy of the GNU General Public License        ** along with this program; if not, write to the                            ** Free Software Foundation, Inc.,                                          ** 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.                 ****************************************************************************/#include <stdio.h>#include <stdlib.h>#include <dir.h>#include <curl/curl.h>typedef char    u8;typedef short   u16;typedef long    u32;char *getURL(char *);           //strip gif url from junkchar *getTITLE(char *);         //strip title from junku32 getFileSize(const char *);  //get filesizeconst char src[] = "tmp.txt";   //temporary storageint main(int argc, char *argv[]){    char *title = NULL,*gif = NULL;    FILE *fd;    char buffer[4096];    char *list[2048] = {NULL};    short found = 0;    u8 istitle = 0;    u16 urlen = strlen(argv[1]);    char url[256],tmpurl[256];    int i;    CURL *curl;    CURLcode res;    u32 page = 1;    u32 maxpages = (u32)atoi(argv[2]);    if(argc!=3 || maxpages <=0 || !strstr(argv[1],"http://"))    {        printf("Wrong parameter use neogif.exe URL PAGES\n");        printf("e.g. neogif.exe http://www.neogaf.com/forum/showthread.php?t=1337 8\n");        printf("Will fetch gifs from the first 8 pages of given url\n");        return 0;    }     curl = curl_easy_init();     if(!curl)     {       printf("Couldn't initialize libcurl!\n");       return 0;     }    //check url validity    strncpy(url,argv[1],sizeof(url));    for(i = urlen;i>0;i--)        if(url[i]=='&')        {            url[i]=0x00;            break;        }    printf("|-------------------------------------------------|\n");    printf("| NEOGIF - get gifs from the mighty neogaf forums |\n");    printf("| CaptainCrunch                           (c)2008 |\n");    printf("|-------------------------------------------------|\n");    printf("Fetching GIF's from:\n");    printf(" ->%s\n",url);    strncpy(tmpurl,url,256);    for(;;)    {        //download html page        fd = fopen(src,"w");        curl_easy_setopt(curl, CURLOPT_URL, url);        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,NULL);        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) fd);        res = curl_easy_perform(curl);        if(res)        {            printf("Error malformed url!\n");            remove(src);            return 0;        }        fclose(fd);        fd = fopen(src,"r+t");        if(!fd)        {            printf("Problem fetching GIF's!\n");            return 0;        }        while(!feof(fd))        {            fgets(buffer,sizeof(buffer),fd);            if(page==1)            {                if(!istitle)                {                    title = getTITLE(buffer);                    if(title)                    {                        istitle = 1;                        printf(" ->%s\n\n",title);                    }                }            }            gif = getURL(buffer);            if(gif)            {                int i,exists=0;                for(i=0;i<found;i++)                {                    if(!strcmp(gif,list[i]))                        exists = 1;                }                if(!exists)                    list[found++] = gif;            }        }        fclose(fd);        printf("Fetching GIF's from page %4d/%d\r",page,maxpages);        page++;        //make new url        sprintf(url,"%s&page=%d",tmpurl,page);        if(page>maxpages)        {            printf("\nDone!\n");            page--;            break;        }    }    if(!title)    {        printf("Couldn't locate title!\n");        printf("Will use \"neogif\" as folder!\n");        strcpy(title,"neogif");    }    //make dir and strip special characters    for(i=0;i<strlen(title);i++)        if(title[i]=='\\' || title[i]=='/' || title[i]==':' || title[i]=='*' || title[i]=='?' || title[i]=='"' || title[i]=='<' || title[i]=='>' ||title[i]=='|')            title[i]=' ';    char path[512];    strncpy(path,title,512);    strcat(path,"\\neogif.html");    //remove old entry    remove(path);    rmdir(title);    if(mkdir(title))    {        printf("Error creating directory!\n");        return 0;    }    fd = fopen(path,"w+t");    fprintf(fd,"<h3>%s</h3><br>\n",title);    for(i=0;i<found;i++)    {        fprintf(fd,"<a href=\"%s\">%s</a><br>",list[i],list[i]);    }    printf(" Found %d GIF's in %d pages\n",found,page);    fclose(fd);    curl_easy_cleanup(curl);    free(list);    remove(src);    return 0;}char *getURL(char *buffer){    char *gif,*start,*end;    short len = 0;    gif = strstr(buffer,".gif");    if(!gif)        return NULL;    //skip avatar gifs    if(strstr(buffer," Avatar\""))      return NULL;    end = start = gif+3;    start--;    int i;    for(i=0;i<strlen(buffer);i++)    {        if(*start == '"' || *start == ' ')            break;        else            start--;    }    //error check    if(*start != '"' || *start == ' ')    {        return NULL;    }    len = end-start;    //allocate memory    gif = (char*)malloc(len+1);    if(!gif)    {        printf("Couldn't allocate memory!\n");        return NULL;    }    strncpy(gif,start+1,len);    gif[len] = 0x00;    //strip relative path    if(strstr(gif,"http://"))        return gif;    else    {        free(gif);        return NULL;    }}char *getTITLE(char *buffer){    char *title,*start,*end;    u16 len;    title = strstr(buffer,"<title>");    if(!title)        return NULL;    start = end = title+7;    int i;    for(i=0;i<strlen(buffer);i++)    {        if(*end == '<')            break;        else            end++;    }    //error check    if(*end != '<')        return NULL;    len = end - start;    title = (char*)malloc(len+1);    if(!title)        return NULL;    strncpy(title,start,len);    title[len]=0x00;    return title;}u32 getFileSize(const char *file){    FILE *fd = fopen(file,"r+b");    u32 size = 0;    if(!fd)        return 0;    fseek(fd,0,SEEK_END);    size = ftell(fd);    fclose(fd);    return size;}