summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick White <hg@njw.me.uk>2011-07-16 16:15:19 +0100
committerNick White <hg@njw.me.uk>2011-07-16 16:15:19 +0100
commita13f5650de7401a9953487a784028676604cb3c0 (patch)
treeb4553eb76d145d8c2c644ada5830db16b64c3fa2
parent21dde74c38f32b4d4243e80e6c7bdf2cf00c9ebe (diff)
Add gettotalpages function
-rw-r--r--TODO10
-rw-r--r--getgbook.c53
2 files changed, 47 insertions, 16 deletions
diff --git a/TODO b/TODO
index 028c3f0..2ee5e68 100644
--- a/TODO
+++ b/TODO
@@ -1 +1,11 @@
use order to be able to use real page numbers (getpagecode)
+ to find this advance through click3 letter by letter until either } (none found) or strcmp "order"
+
+
+# once it is basically working #
+
+to be fast and efficient it's best to crank through all the json 1st, filling in an array of page structs as we go
+ this requires slightly fuller json support
+ could consider making a json reading module, ala confoo, to make ad-hoc memory structures from json
+
+to be super fast we could have 2 threads, one filling the pages structs and one consuming them. this would complicate the code rather, though
diff --git a/getgbook.c b/getgbook.c
index eb5e9a7..468a0f2 100644
--- a/getgbook.c
+++ b/getgbook.c
@@ -1,5 +1,11 @@
/* See COPYING file for copyright, license and warranty details. */
+/* NOTE: there's now a new api that returns json.
+ * it requires https, which we don't yet support.
+ * https://www.googleapis.com/books/v1/volumes?q=isbn:1589235126
+ * https://www.googleapis.com/books/v1/volumes/jglfL_eVG4cC */
+
+
#define VERSION "prealpha"
#include <stdio.h>
@@ -19,35 +25,24 @@
#define BOOKID_LEN 12
typedef struct {
- char *name;
- char *code;
-} pgtype;
-
-pgtype pgtypes[] = {
- {"cover", "PP"},
- {"preface", "PR"},
- {"page", "PA"},
- {"postface", "PA"},
-};
+ int num;
+ char url[URLMAX];
+ char name[80];
+} page;
char *getbookid(char *isbn)
{
char url[URLMAX];
char *buf, *bookid, *c;
- /* 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(!get("books.google.com", url, &buf))
return NULL;
if((c = strstr(buf,"<dc:identifier>")) == NULL)
return NULL;
+ bookid = malloc(sizeof(char *) * BOOKID_LEN);
strncpy(bookid, c+15, BOOKID_LEN);
bookid[BOOKID_LEN] = '\0';
free(buf);
@@ -55,6 +50,27 @@ char *getbookid(char *isbn)
return bookid;
}
+int gettotalpages(char *bookid)
+{
+ char url[URLMAX];
+ char *buf, *c;
+ int total;
+
+ snprintf(url, URLMAX, "/books/feeds/volumes/%s", bookid);
+
+ bookid = malloc(sizeof(char *) * BOOKID_LEN);
+
+ if(!get("books.google.com", url, &buf))
+ return 0;
+
+ if((c = strstr(buf," pages</dc:format>")) == NULL)
+ return 0;
+ while(*c && *c != '>') *c--;
+ sscanf(c+1, "%d ", &total);
+
+ return total;
+}
+
char *getpageurl(char *bookid, char *pg)
{
char url[URLMAX];
@@ -88,6 +104,7 @@ char *getpageurl(char *bookid, char *pg)
int main(int argc, char *argv[])
{
char *bookid, *url, pg[16];
+ int totalpages;
if(argc < 2 || argc > 3)
die(usage);
@@ -103,6 +120,10 @@ int main(int argc, char *argv[])
if((bookid = getbookid(argv[1])) == NULL)
die("Could not find book\n");
+ if(!(totalpages = gettotalpages(bookid)))
+ die("Book has no pages\n");
+ printf("Book has %d pages\n", totalpages);
+
strncpy(pg, "PA2", 12);
if((url = getpageurl(bookid, pg)) == NULL)
fprintf(stderr, "Could not find page %s\n", pg);