From e85bc3e20bb05cd43bfdf6c55d113906e969db15 Mon Sep 17 00:00:00 2001 From: Nick White Date: Fri, 15 Jul 2011 23:38:01 +0100 Subject: Get book id from isbn --- getgbook.c | 50 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) (limited to 'getgbook.c') diff --git a/getgbook.c b/getgbook.c index 7858008..98e1b0b 100644 --- a/getgbook.c +++ b/getgbook.c @@ -7,32 +7,62 @@ #include #include "util.c" -#define usage "getgbook bookid" +#define usage "getgbook isbn" #define hostname "books.google.com" +#define URLMAX 1024 +#define BOOKID_LEN 12 + +char *getgbookid(char *isbn) +{ + char url[URLMAX]; + int i; + FILE *srv; + char *buf, *bookid, *c; + + i = dial("books.google.com", "80"); + srv = fdopen(i, "r+"); + + /* NOTE: new api returns json, and looks like this: + * http://www.googleapis.com/books/v1/volumes?q=isbn:1589235126 + * (this needs https, which we don't yet support) */ + + snprintf(url, URLMAX, "/books/feeds/volumes?q=isbn:%s", isbn); + + bookid = malloc(sizeof(char *) * BOOKID_LEN); + if((buf = get(srv, "books.google.com", url)) == NULL) + fprintf(stderr,"Error downloading page\n"); + else { + c = strstr(buf,""); + strncpy(bookid, c+15, BOOKID_LEN); + bookid[BOOKID_LEN] = '\0'; + free(buf); + } + + return bookid; +} + int main(int argc, char *argv[]) { int i, s; - char *bookid, url[80]; + char *bookid, url[80], isbn[16]; char *curpage; FILE *srv; if(argc != 2) die("usage: " usage "\n"); - bookid = argv[1]; + strncpy(isbn,argv[1],16); i = dial(hostname, "80"); srv = fdopen(i, "r+"); - snprintf(url, 80, "/books?id=%s&pg=%s&jscmd=click3", bookid, "PA1"); - if((curpage = get(srv, hostname, url)) == NULL) - fprintf(stderr,"Error downloading page\n"); - else { - fputs(curpage,stdout); - free(curpage); - } + /* get google book id */ + bookid = getgbookid(isbn); + + printf("bookid is %s\n", bookid); + free(bookid); return 0; } -- cgit v1.2.3