From ec9146c96c09524fe4257d4632039f7f684e49e8 Mon Sep 17 00:00:00 2001 From: Nick White Date: Thu, 6 Oct 2011 21:29:25 +0100 Subject: Download all pages of a book into their own directory --- TODO | 1 - getabook.c | 18 ++++++++++++++---- getgbook.c | 18 ++++++++++++++---- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/TODO b/TODO index ece973d..7be9dfe 100644 --- a/TODO +++ b/TODO @@ -4,7 +4,6 @@ before 1.0: create bn tool, fix http bugs, be unicode safe, package for osx & wi # other todos -mkdir of bookid and save pages in there use wide string functions when dealing with stuff returned over http; it's known utf8 http://triptico.com/docs/unicode.html#utf-8 diff --git a/getabook.c b/getabook.c index 098a5c3..9f4015b 100644 --- a/getabook.c +++ b/getabook.c @@ -2,6 +2,9 @@ #include #include #include +#include +#include +#include #include "util.h" #define usage "getabook " VERSION " - an amazon look inside the book downloader\n" \ @@ -20,7 +23,8 @@ typedef struct { Page **pages; int numpages; -char *bookid; +char bookid[STRMAX]; +char *bookdir; int fillurls(char *buf) { char m[STRMAX]; @@ -116,7 +120,7 @@ int getpageurls(int pagenum) { int getpage(Page *page) { char path[STRMAX]; - snprintf(path, STRMAX, "%04d.png", page->num); + snprintf(path, STRMAX, "%s/%04d.png", bookdir, page->num); if(page->url[0] == '\0') { fprintf(stderr, "%d not found\n", page->num); @@ -147,7 +151,8 @@ int main(int argc, char *argv[]) return 1; } - bookid = argv[argc-1]; + strncpy(bookid, argv[argc-1], STRMAX); + bookdir = argv[argc-1]; pages = malloc(sizeof(*pages) * MAXPAGES); if(getpagelist(bookid, pages)) { @@ -155,9 +160,14 @@ int main(int argc, char *argv[]) return 1; } + if(!(opendir(bookdir) || !mkdir(bookdir, S_IRWXU))) { + fprintf(stderr, "Could not create directory %s\n", bookdir); + return 1; + } + if(argc == 2) { for(i=0; inum); + snprintf(pgpath, STRMAX, "%s/%04d.png", bookdir, pages[i]->num); if((f = fopen(pgpath, "r")) != NULL) { fclose(f); continue; diff --git a/getgbook.c b/getgbook.c index 751b659..a06573e 100644 --- a/getgbook.c +++ b/getgbook.c @@ -2,6 +2,9 @@ #include #include #include +#include +#include +#include #include "util.h" #define usage "getgbook " VERSION " - a google books downloader\n" \ @@ -25,7 +28,8 @@ typedef struct { Page **pages; int totalpages; char cookies[COOKIENUM][COOKIEMAX]; -char *bookid; +char bookid[STRMAX]; +char *bookdir; int getpagelist() { @@ -115,7 +119,7 @@ int getpageurls(char *pagecode, char *cookie) { int getpage(Page *page) { char path[STRMAX]; - snprintf(path, STRMAX, "%04d.png", page->num); + snprintf(path, STRMAX, "%s/%04d.png", bookdir, page->num); if(page->url[0] == '\0') { fprintf(stderr, "%s not found\n", page->name); @@ -172,7 +176,8 @@ int main(int argc, char *argv[]) free(tmp); } - bookid = argv[argc-1]; + strncpy(bookid, argv[argc-1], STRMAX); + bookdir = argv[argc-1]; pages = malloc(sizeof(*pages) * MAXPAGES); if(!(totalpages = getpagelist(bookid, pages))) { @@ -180,9 +185,14 @@ int main(int argc, char *argv[]) return 1; } + if(!(opendir(bookdir) || !mkdir(bookdir, S_IRWXU))) { + fprintf(stderr, "Could not create directory %s\n", bookdir); + return 1; + } + if(argc == 2) { for(i=0; inum); + snprintf(pgpath, STRMAX, "%s/%04d.png", bookdir, pages[i]->num); if((f = fopen(pgpath, "r")) != NULL) { fclose(f); continue; -- cgit v1.2.3