From 6ca2e52598ec57d7e54a46315daf1e7018b8333f Mon Sep 17 00:00:00 2001 From: Nick White Date: Sun, 17 Jul 2011 15:48:24 +0100 Subject: Add ip setting and improve robustness of http --- getgbook.c | 25 +++++++++++++++++-------- util.c | 10 +++++----- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/getgbook.c b/getgbook.c index c208e25..df5de6f 100644 --- a/getgbook.c +++ b/getgbook.c @@ -5,9 +5,10 @@ #include "util.c" #define usage "getgbook " VERSION " - a google books downloader\n" \ - "usage: getgbook [-p|-a] isbn\n" \ + "usage: getgbook [-i ip] [-p|-a] isbn\n" \ " -p print all available pages\n" \ " -a download all available pages\n" \ + " -i appear from ip address\n" \ " otherwise, all pages in stdin will be downloaded\n" #define URLMAX 1024 @@ -19,6 +20,8 @@ typedef struct { char name[80]; } Page; +char extrahdr[1024] = "\0"; + char *getbookid(char *isbn) { char url[URLMAX]; @@ -26,7 +29,7 @@ char *getbookid(char *isbn) snprintf(url, URLMAX, "/books/feeds/volumes?q=isbn:%s", isbn); - if(!get("books.google.com", url, &buf)) + if(!get("books.google.com", url, extrahdr, &buf)) return NULL; if((c = strstr(buf,"")) == NULL) @@ -49,7 +52,7 @@ int gettotalpages(char *bookid) bookid = malloc(sizeof(char *) * BOOKID_LEN); - if(!get("books.google.com", url, &buf)) + if(!get("books.google.com", url, extrahdr, &buf)) return 0; if((c = strstr(buf," pages")) == NULL) @@ -68,7 +71,7 @@ Page *getpagedetail(char *bookid, char *pg) snprintf(url, URLMAX, "/books?id=%s&pg=%s&jscmd=click3", bookid, pg); - if(!get("books.google.com", url, &buf)) + if(!get("books.google.com", url, extrahdr, &buf)) return NULL; snprintf(m, 80, "\"pid\":\"%s\"", pg); @@ -113,10 +116,16 @@ int main(int argc, char *argv[]) int totalpages, i; Page *page; - if(argc < 2 || argc > 3 || - (argv[1][0]=='-' && ((argv[1][1]!='p' && argv[1][1]!='a') || argc < 3))) + if(argc < 2 || argc > 5 || + (argv[1][0]=='-' && ((argv[1][1]!='p' && argv[1][1]!='a' && argv[1][1] != 'i') || argc < 3))) die(usage); + if(argv[1][0] == '-' && argv[1][1] == 'i') { + snprintf(extrahdr, 1024, "X-Forwarded-For: %s\r\n", argv[2]); + argv+=2; + argc-=2; + } + if((bookid = getbookid(argv[argc-1])) == NULL) die("Could not find book\n"); @@ -137,7 +146,7 @@ int main(int argc, char *argv[]) } if(argv[1][1] == 'a') { snprintf(n, 80, "%05d.png", page->num); - gettofile(page->url, n); + gettofile("books.google.com", page->url, extrahdr, n); printf("Downloaded page %d\n", page->num); } else printf("%d\n", page->num); @@ -154,7 +163,7 @@ int main(int argc, char *argv[]) continue; } snprintf(n, 80, "%05d.png", page->num); - gettofile(page->url, n); + gettofile("books.google.com", page->url, extrahdr, n); printf("Downloaded page %d\n", page->num); free(page); } diff --git a/util.c b/util.c index 2cb7c99..68d1d72 100644 --- a/util.c +++ b/util.c @@ -33,7 +33,7 @@ static int dial(char *host, char *port) { return srv; } -int get(char *host, char *path, char **buf) { +int get(char *host, char *path, char *extrahdr, char **buf) { size_t l, res; int fd, i, p; char h[1024] = "\0"; @@ -43,13 +43,13 @@ int get(char *host, char *path, char **buf) { srv = fdopen(fd, "r+"); fprintf(srv, "GET %s HTTP/1.0\r\nUser-Agent: getgbook-"VERSION \ - " (not mozilla)\r\nHost: %s\r\n\r\n", path, host); + " (not mozilla)\r\n%sHost: %s\r\n\r\n", path, extrahdr, host); fflush(srv); while(h[0] != '\r') { fgets(h, 1024, srv); if(sscanf(h, "HTTP/%d.%d %d", &i, &i, &p) == 3 && p != 200) - return 1; + return 0; } *buf = malloc(sizeof(char *) * 4096); @@ -60,12 +60,12 @@ int get(char *host, char *path, char **buf) { return l; } -int gettofile(char *url, char *savepath) { +int gettofile(char *host, char *url, char *extrahdr, char *savepath) { char *buf = 0; FILE *f; size_t i, l; - if(!(l = get("books.google.com", url, &buf))) { + if(!(l = get(host, url, extrahdr, &buf))) { fprintf(stderr, "Could not download %s\n", url); return 1; } -- cgit v1.2.3