summaryrefslogtreecommitdiff
path: root/getgbook.c
diff options
context:
space:
mode:
authorNick White <hg@njw.me.uk>2011-07-15 23:38:01 +0100
committerNick White <hg@njw.me.uk>2011-07-15 23:38:01 +0100
commite85bc3e20bb05cd43bfdf6c55d113906e969db15 (patch)
treedeba0ad762039c7e3d1edeb68eb3513c9f905c48 /getgbook.c
parent9cc59faa6692be97c77e17fec161c678e14a6ad2 (diff)
Get book id from isbn
Diffstat (limited to 'getgbook.c')
-rw-r--r--getgbook.c50
1 files changed, 40 insertions, 10 deletions
diff --git a/getgbook.c b/getgbook.c
index 7858008..98e1b0b 100644
--- a/getgbook.c
+++ b/getgbook.c
@@ -7,32 +7,62 @@
#include <string.h>
#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,"<dc:identifier>");
+ 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;
}