summaryrefslogtreecommitdiff
path: root/getgbook.c
blob: 6f6870f059b9c74213b31b62ddeda74f0ab01c01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* See COPYING file for copyright, license and warranty details. */

#define VERSION "prealpha"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.c"

#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;
	char *bookid, isbn[16];
	FILE *srv;

	if(argc != 2)
		die("usage: " usage "\n");

	strncpy(isbn,argv[1],16);

	i = dial(hostname, "80");
	srv = fdopen(i, "r+");

	bookid = getgbookid(isbn);
	printf("bookid is %s\n", bookid);

	free(bookid);
	return 0;
}