diff options
author | Nick White <git@njw.me.uk> | 2011-10-06 21:29:25 +0100 |
---|---|---|
committer | Nick White <git@njw.me.uk> | 2011-10-06 21:29:25 +0100 |
commit | ec9146c96c09524fe4257d4632039f7f684e49e8 (patch) | |
tree | a76751f7ac5954913765a922f440a881477b8f5f /getgbook.c | |
parent | 95990bd62f671be2f53ffecc470f34ba6f716226 (diff) |
Download all pages of a book into their own directory
Diffstat (limited to 'getgbook.c')
-rw-r--r-- | getgbook.c | 18 |
1 files changed, 14 insertions, 4 deletions
@@ -2,6 +2,9 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <dirent.h> #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; i<totalpages; i++) { - snprintf(pgpath, STRMAX, "%04d.png", pages[i]->num); + snprintf(pgpath, STRMAX, "%s/%04d.png", bookdir, pages[i]->num); if((f = fopen(pgpath, "r")) != NULL) { fclose(f); continue; |