From b3f1968f84ecbef95057e3a80c40c7051d3b19f3 Mon Sep 17 00:00:00 2001 From: Nick White Date: Sat, 19 Nov 2011 17:13:19 +0000 Subject: Add getbnbook tool --- Makefile | 2 +- TODO | 4 +- getbnbook.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 getbnbook.c diff --git a/Makefile b/Makefile index 733a7e1..65ea185 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ include config.mk NAME = getxbook -SRC = getgbook.c getabook.c +SRC = getgbook.c getabook.c getbnbook.c LIB = util.o GUI = getxbookgui.tcl DOC = README COPYING INSTALL LEGAL diff --git a/TODO b/TODO index 7133837..15cf2ef 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,4 @@ -before 1.0: create bn tool, fix http bugs, package for osx & windows - -# getbnbook +before 1.0: fix http bugs, package for osx & windows # other todos diff --git a/getbnbook.c b/getbnbook.c new file mode 100644 index 0000000..806b2e8 --- /dev/null +++ b/getbnbook.c @@ -0,0 +1,152 @@ +/* See COPYING file for copyright and license details. */ +#include +#include +#include +#include +#include +#include +#include "util.h" + +#define usage "getbnbook " VERSION " - a barnes and noble book downloader\n" \ + "usage: getbnbook [-n] isbn13\n" \ + " -n download pages from numbers in stdin\n" \ + " otherwise, all available pages will be downloaded\n" + +#define URLMAX 1024 +#define STRMAX 1024 +#define MAXPAGES 9999 + +int pages[MAXPAGES]; +int numpages; +char urlpath[STRMAX]; +char bookid[STRMAX]; +char *bookdir; +char cookies[COOKIEMAX]; + +int getpagelist() +{ + char url[URLMAX]; + char *buf = NULL; + char *s, *l; + int i, num; + char avail[STRMAX]; + char dummy1[STRMAX], dummy2[STRMAX]; + + numpages = 0; + + snprintf(url, URLMAX, "/DigBooks/viewer/bookviewmanager.aspx?op=getbookinfo&ean=%s", bookid); + + if(!get("search2.barnesandnoble.com", url, NULL, cookies, &buf)) + return 1; + + /* find page url structure */ + if((s=strstr(buf, "")) == NULL) { + free(buf); + return 1; + } + s+=strlen(""); + l = strchr(s, '<'); + for(i=0; s", &num, dummy1, dummy2, avail); + + if(strncmp(avail, "true", STRMAX) == 0) + pages[i++] = num; + } + + numpages = i; + + free(buf); + return 0; +} + +int getpage(int pagenum) +{ + char path[STRMAX], pageurl[STRMAX]; + char *s; + + snprintf(path, STRMAX, "%s/%04d.png", bookdir, pagenum); + + s=strchr(urlpath+7, '/'); + snprintf(pageurl, STRMAX, s, pagenum); + + if(gettofile("search2.barnesandnoble.com", pageurl, cookies, NULL, path)) { + fprintf(stderr, "%d failed\n", pagenum); + return 1; + } + + printf("%d downloaded\n", pagenum); + fflush(stdout); + return 0; +} + +int main(int argc, char *argv[]) +{ + char *tmp; + char buf[STRMAX], pgpath[STRMAX]; + char in[16]; + int a, i, n; + FILE *f; + DIR *d; + + if(argc < 2 || argc > 3 || + (argc == 3 && (argv[1][0]!='-' || argv[1][1] != 'n')) + || (argc >= 2 && argv[1][0] == '-' && argv[1][1] == 'h')) { + fputs(usage, stdout); + return 1; + } + + strncpy(bookid, argv[argc-1], STRMAX); + bookdir = argv[argc-1]; + + /* get cookie */ + if(get("www.barnesandnoble.com", "/", NULL, cookies, &tmp)) + free(tmp); + + if(getpagelist(bookid, pages)) { + fprintf(stderr, "Could not find any pages for %s\n", bookid); + return 1; + } + + if(!((d = opendir(bookdir)) || !mkdir(bookdir, S_IRWXU))) { + fprintf(stderr, "Could not create directory %s\n", bookdir); + return 1; + } + if(d) closedir(d); + + if(argc == 2) { + for(i=0; i