summaryrefslogtreecommitdiff
path: root/getabook.c
blob: 098a5c395fdc79db18d9534fe8b1760015234be4 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* See COPYING file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"

#define usage "getabook " VERSION " - an amazon look inside the book downloader\n" \
              "usage: getabook [-n] asin\n" \
              "  -n download pages from numbers in stdin\n" \
              "  otherwise, all available pages will be downloaded\n"

#define URLMAX 1024
#define STRMAX 1024
#define MAXPAGES 9999

typedef struct {
	int num;
	char url[URLMAX];
} Page;

Page **pages;
int numpages;
char *bookid;

int fillurls(char *buf) {
	char m[STRMAX];
	char *c, *s;
	int i;

	if(!(s = strstr(buf, "\"jumboImageUrls\":{"))) {
		return 1;
	}
	s += strlen("\"jumboImageUrls\":{");

	for(i=0; *s && i<numpages; i++) {
		c = s;

		snprintf(m, STRMAX, "\"%d\":", pages[i]->num);

		while(strncmp(c, m, strlen(m)) != 0) {
			while(*c && *c != '}' && *c != ',')
				c++;
			if(*c == '}')
				break;
			c++;
		}
		if(*c == '}')
			continue;

		c += strlen(m);
		if(!sscanf(c, "\"//sitb-images.amazon.com%[^\"]\"", pages[i]->url))
			continue;
	}

	return 0;
}

int getpagelist()
{
	char url[URLMAX], b[STRMAX];
	char *buf = NULL;
	char *s, *c;
	int i;
	Page *p;

	snprintf(url, URLMAX, "/gp/search-inside/service-data?method=getBookData&asin=%s", bookid);

	if(!get("www.amazon.com", url, NULL, NULL, &buf))
		return 1;

	/* amazon have a canonical asin, which is needed to get all available pages */
	if((s = strstr(buf, "\"ASIN\":\"")) != NULL) {
		s+=strlen("\"ASIN\":\"");
		strncpy(bookid, s, 10);
		bookid[10] = '\0';
	}

	if((s = strstr(buf, "\"litbPages\":[")) == NULL)
		return 1;
	s+=strlen("\"litbPages\":[");

	for(i=0, p=pages[0];*s && i<MAXPAGES; s++) {
		for(c = b; *s != ',' && *s != ']'; s++, c++) *c = *s;
		*(c+1) = '\0';
		p=pages[i++]=malloc(sizeof(**pages));;
		sscanf(b, "%d,", &(p->num));
		if(s[0] == ']')
			break;
		p->url[0] = '\0';
	}
	numpages = i;

	fillurls(buf);

	free(buf);
	return 0;
}

int getpageurls(int pagenum) {
	char url[URLMAX];
	char query[URLMAX];
	char *buf = NULL;

	strncpy(url, "/gp/search-inside/service-data", URLMAX);
	snprintf(query, URLMAX, "method=goToPage&asin=%s&page=%d", bookid, pagenum);

	if(!post("www.amazon.com", url, query, &buf))
		return 1;

	fillurls(buf);

	free(buf);
	return 0;
}

int getpage(Page *page)
{
	char path[STRMAX];
	snprintf(path, STRMAX, "%04d.png", page->num);

	if(page->url[0] == '\0') {
		fprintf(stderr, "%d not found\n", page->num);
		return 1;
	}

	if(gettofile("sitb-images.amazon.com", page->url, NULL, NULL, path)) {
		fprintf(stderr, "%d failed\n", page->num);
		return 1;
	}

	printf("%d downloaded\n", page->num);
	fflush(stdout);
	return 0;
}

int main(int argc, char *argv[])
{
	char buf[BUFSIZ], pgpath[STRMAX];
	char in[16];
	int a, i, n;
	FILE *f;

	if(argc < 2 || argc > 3 ||
	   (argc == 3 && (argv[1][0]!='-' || argv[1][1] != 'n'))
	   || (argc >= 2 && argv[1][0] == '-' && argv[1][1] == 'h')) {
		fputs(usage, stdout);
		return 1;
	}

	bookid = argv[argc-1];

	pages = malloc(sizeof(*pages) * MAXPAGES);
	if(getpagelist(bookid, pages)) {
		fprintf(stderr, "Could not find any pages for %s\n", bookid);
		return 1;
	}

	if(argc == 2) {
		for(i=0; i<numpages; i++) {
			snprintf(pgpath, STRMAX, "%04d.png", pages[i]->num);
			if((f = fopen(pgpath, "r")) != NULL) {
				fclose(f);
				continue;
			}
			if(pages[i]->url[0] == '\0')
				getpageurls(pages[i]->num);
			getpage(pages[i]);
		}
	} else if(argv[1][0] == '-' && argv[1][1] == 'n') {
		while(fgets(buf, BUFSIZ, stdin)) {
			sscanf(buf, "%15s", in);
			i = -1;
			sscanf(in, "%d", &n);
			for(a=0; a<numpages; a++) {
				if(pages[a]->num == n) {
					i = a;
					break;
				}
			}
			if(i == -1) {
				fprintf(stderr, "%s not found\n", in);
				continue;
			}
			if(pages[i]->url[0] == '\0')
				getpageurls(pages[i]->num);
			getpage(pages[i]);
		}
	}

	for(i=0; i<numpages; i++) free(pages[i]);
	free(pages);

	return EXIT_SUCCESS;
}