summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--getgbook.c12
-rw-r--r--util.c43
2 files changed, 42 insertions, 13 deletions
diff --git a/getgbook.c b/getgbook.c
index 358d8c3..fe4ccd1 100644
--- a/getgbook.c
+++ b/getgbook.c
@@ -43,7 +43,7 @@ char *getbookid(char *isbn)
bookid = malloc(sizeof(char *) * BOOKID_LEN);
- if((buf = get("books.google.com", url)) == NULL)
+ if(!get("books.google.com", url, &buf))
return NULL;
if((c = strstr(buf,"<dc:identifier>")) == NULL)
@@ -62,7 +62,7 @@ char *getpageurl(char *bookid, char *pg)
snprintf(url, URLMAX, "/books?id=%s&pg=%s&jscmd=click3", bookid, pg);
- if((buf = get("books.google.com", url)) == NULL)
+ if(!get("books.google.com", url, &buf))
return NULL;
snprintf(m, 80, "\"pid\":\"%s\"", pg);
@@ -105,10 +105,12 @@ int main(int argc, char *argv[])
printf("bookid is %s\n", bookid);
strncpy(pg, "PA2", 12);
- if((url = getpageurl(bookid, pg)) != NULL)
- printf("page %s url is %s\n", pg, url);
- else
+ if((url = getpageurl(bookid, pg)) == NULL)
fprintf(stderr, "Could not find page %s\n", pg);
+ else {
+ printf("page %s url is %s\n", pg, url);
+ gettofile(url, "test.png");
+ }
}
free(bookid);
diff --git a/util.c b/util.c
index 8fa00bd..d9aaa54 100644
--- a/util.c
+++ b/util.c
@@ -34,7 +34,7 @@ static int dial(char *host, char *port) {
return srv;
}
-char *get(char *host, char *path) {
+int get(char *host, char *path, char **gotbuf) {
size_t l, res;
int fd, i;
char *buf, *c, *p;
@@ -44,30 +44,57 @@ char *get(char *host, char *path) {
srv = fdopen(fd, "r+");
fprintf(srv, "GET %s HTTP/1.0\r\nUser-Agent: getgbook-"VERSION" (not mozilla)\r\nHost: %s\r\n\r\n", path, host);
-
fflush(srv);
- l=0;
-
buf = malloc(sizeof(char *) * 4096);
- for(i=0; (res = read(fd, buf+l, 4096)) > 0; l+=res, i++)
+ for(i=0, l=0; (res = read(fd, buf+l, 4096)) > 0; l+=res, i++)
buf = realloc(buf, sizeof(char *) * (l+4096));
/* check that it's a 200 */
if(strncmp(buf+9, "200 ", 4)) {
free(buf);
- return NULL;
+ return 0;
}
/* exclude header */
for(p = buf; *p && *(p+1) && *(p+2) && *(p+3); p++)
if(!strncmp(p, "\r\n\r\n", 4)) break;
p+=4;
-
+
i = l - (p - buf);
c = malloc(i+1);
memcpy(c, p, i);
free(buf);
- return c;
+ *gotbuf = c;
+
+ return i;
+}
+
+int gettofile(char *url, char *savepath) {
+ char *buf = 0;
+ FILE *f;
+ size_t i, l;
+
+ if(!(l = get("books.google.com", url, &buf))) {
+ fprintf(stderr, "Could not download %s\n", url);
+ return 1;
+ }
+
+ if((f = fopen(savepath, "w")) == NULL) {
+ fprintf(stderr, "Could not create file %s\n", savepath);
+ return 1;
+ }
+
+ for(i=0; i < l; i+=512)
+ if(!fwrite(buf+i, l-i > 512 ? 512 : l-i, 1, f)) {
+ fprintf(stderr, "Error writing file %s\n", savepath);
+ free(buf); fclose(f);
+ return 1;
+ }
+
+ free(buf);
+ fclose(f);
+
+ return 0;
}