google-book-scraper 0.1.0

Tool and library for downloading the contents of books hosted on books.google.com for offline viewing.
Documentation
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use bitflags::bitflags;
use scraper::{ElementRef, Html, Selector};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};
use std::fs::OpenOptions;
use std::io::{self};
use std::io::{Read, Write};
use url::Url;

use crate::cbz::create_cbz;
use crate::pdf::{create_pdf_with_toc, TableOfContents};

/// Scrape options.
pub struct ScraperOptions {
    /// If true, downloaded images will not be deleted after conversion.
    pub keep_images: bool,
    /// Format(s) to convert downloaded images to.
    pub formats: FormatFlags,
    /// IDs of issues to skip.
    pub already_downloaded: HashSet<String>,
    /// File to store IDs of already downloaded books.
    pub archive_file: Option<String>,
}

impl Default for ScraperOptions {
    fn default() -> Self {
        Self {
            keep_images: false,
            formats: FormatFlags::Pdf,
            already_downloaded: HashSet::new(),
            archive_file: None,
        }
    }
}

bitflags! {
    /// Format(s) downloaded images to
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct FormatFlags:u32 {
        const None = 0b000;
        const Pdf =  0b001;
        const Cbz =  0b010;
        const All =  0b011;
    }
}

/// Metadata for single issue.
pub struct IssueMetadata {
    /// ID used to identify book resource
    pub id: String,
    /// Name of series/magazine issue belongs to
    pub series_name: String,
    /// Date issue was published
    pub publish_date: String,
    /// Volume of issue
    pub volume: String,
    /// ISSN of publication
    pub issn: String,
    /// Publisher
    pub publisher: String,
    /// Description of publication
    pub description: String,
}

#[derive(Serialize, Deserialize)]
struct PageJson {
    pid: String,
    src: Option<String>,
}

#[derive(Serialize, Deserialize)]
struct IssueJson {
    page: Vec<PageJson>,
}

impl IssueMetadata {
    fn new(id: &str) -> IssueMetadata {
        IssueMetadata {
            id: id.to_string(),
            series_name: String::new(),
            publish_date: String::new(),
            volume: String::new(),
            issn: String::new(),
            publisher: String::new(),
            description: String::new(),
        }
    }

    /// Parses metadata from the provided HTML element.
    fn parse(&mut self, element: &ElementRef) {
        let selector = Selector::parse(".booktitle").unwrap();
        for e in element.select(&selector) {
            for child in e.text() {
                self.series_name = child.to_string();
                break;
            }
            break;
        }
        let selector = Selector::parse("#synopsistext").unwrap();
        for e in element.select(&selector) {
            for child in e.text() {
                self.description = child.to_string();
                break;
            }
            break;
        }
        let selector = Selector::parse("#metadata").unwrap();
        for e in element.select(&selector) {
            let mut i: u32 = 0;
            for child in e.text() {
                match i {
                    0 => {
                        self.publish_date = child.to_string();
                    }
                    2 => {
                        self.volume = child.to_string();
                    }
                    3 => {
                        self.issn = child.to_string();
                    }
                    4 => {
                        self.publisher = child.to_string();
                    }
                    _ => (),
                }
                i += 1;
            }
            break;
        }
    }
}

/// Downloads issue at the provided URL and performs any necessary format conversion.
///
/// # Arguments
///
/// * `url` - URL of issue to download.
/// * `dest` - Filename of image to link to.
/// * `options` - Various options for how to process downloaded images.
pub fn download_issue(url: &str, dest: &str, options: &mut ScraperOptions) -> io::Result<()> {
    // Parse ID from URL.
    let mut url_obj = Url::try_from(url).unwrap();

    let mut id = String::new();
    for (key, val) in url_obj.query_pairs() {
        if key == "id" {
            id = val.to_string();
            break;
        }
    }

    if options.already_downloaded.contains(&id) {
        println!("Skipping already downloaded book: {id}...");
        return Ok(());
    }

    println!("Identifying book: {id}...");

    // Fetch page.
    let mut res = reqwest::blocking::get(url).unwrap();
    let mut body = String::new();
    res.read_to_string(&mut body)?;
    let doc = Html::parse_document(&body);

    // Parse issue metadata from page.
    let mut issue_meta = IssueMetadata::new(&id);
    let selector = Selector::parse("#summary_content_table").unwrap();
    for element in doc.select(&selector) {
        issue_meta.parse(&element);
        break;
    }

    // Derive paths and create any missing directories.
    let issue_combined_id = std::format!("{0} [{1}]", issue_meta.publish_date, issue_meta.id);
    let series_dir = std::format!("{dest}/{0}", issue_meta.series_name);
    let issue_pics_dir = std::format!("{series_dir}/{issue_combined_id}");

    println!("Found: {0} - {issue_combined_id}", issue_meta.series_name);

    let exists_already = std::path::Path::new(&issue_pics_dir).exists();
    if !exists_already {
        std::fs::create_dir_all(&issue_pics_dir)?
    };

    // Check if any needed formats already exist on disk.
    let mut formats = options.formats.clone();
    let filename_pdf = std::format!("{series_dir}/{issue_combined_id}.pdf");
    if std::path::Path::new(&filename_pdf).exists() {
        formats.remove(FormatFlags::Pdf)
    }
    let filename_cbz = std::format!("{series_dir}/{issue_combined_id}.cbz");
    if std::path::Path::new(&filename_cbz).exists() {
        formats.remove(FormatFlags::Cbz)
    }

    if formats == FormatFlags::None && (exists_already || !options.keep_images) {
        println!("Already downloaded. Skipping...");
        return Ok(());
    }

    // Parse TOC info.
    let mut toc_page_title_lookup = HashMap::<String, String>::new();
    let selector = Selector::parse("#toc tr").unwrap();
    let mut i = 0;
    let mut bookmark_text = String::new();
    let mut bookmark_page_id = String::new();
    for element in doc.select(&selector) {
        // Bookmarks encompass two <td>s, so this will alternate between title and description/keywords
        if i % 2 == 0 {
            // Title
            for span in element.select(&Selector::parse("span").unwrap()) {
                for str in span.text() {
                    bookmark_text += str;
                }
                break;
            }

            for link in element.select(&Selector::parse("a").unwrap()) {
                if let Some(href) = link.attr("href") {
                    let link_url_obj = Url::try_from(href).unwrap();
                    for (key, val) in link_url_obj.query_pairs() {
                        if key == "pg" {
                            bookmark_page_id = val.to_string();
                            break;
                        }
                    }
                }
                break;
            }
        } else {
            // Keywords

            // Note: Ignoring this as it seems to be just keywords that make the bookmark overly long if included.
            // let mut bookmark_description = String::new();
            // for str in element.text() {
            //     bookmark_description += str;
            // }
            // while bookmark_description.contains("  ") {
            //     bookmark_description = bookmark_description.replace("  ", " ");
            // }

            toc_page_title_lookup.insert(bookmark_page_id, bookmark_text);
            bookmark_page_id = String::new();
            bookmark_text = String::new();
        }
        i += 1;
    }

    // Fetch JSON to get info about all pages.
    let json_query_str = std::format!("id={id}&lpg=1&pg=1&jscmd=click3");
    url_obj.set_query(Some(&json_query_str));
    let mut res = reqwest::blocking::get(url_obj.to_string()).unwrap();
    let mut body = String::new();
    res.read_to_string(&mut body)?;
    let issue: IssueJson = serde_json::from_str(&body).unwrap();

    // Make lookup of all pages referenced in json and their absolute page number.
    let mut page_number_lookup = HashMap::<String, usize>::new();
    let mut pages_to_download = VecDeque::<String>::new();
    let mut first_page = "1".to_string();
    let mut i = 1;
    for page in issue.page {
        if let None = page.src {
            page_number_lookup.insert(page.pid.clone(), i);
            pages_to_download.push_back(page.pid.clone());
            if i == 1 {
                first_page = page.pid;
            }
            i += 1;
        }
    }

    // Download all pages and associate filenames in TOC.
    let mut toc = TableOfContents::new();
    let mut pages_downloaded = HashSet::<String>::new();
    while !pages_to_download.is_empty() {
        // Get next page ID, skip if already downloaded.
        let page_id = pages_to_download.pop_front().unwrap();
        if pages_downloaded.contains(&page_id) {
            continue;
        }

        // Fetch JSON for page.
        let json_query_str = std::format!("id={id}&lpg={first_page}&pg={page_id}&jscmd=click3");
        url_obj.set_query(Some(&json_query_str));
        let mut res = reqwest::blocking::get(url_obj.to_string()).unwrap();
        let mut body = String::new();
        res.read_to_string(&mut body)?;
        let issue: IssueJson = serde_json::from_str(&body).unwrap();

        // Download images linked in JSON.
        for page in issue.page {
            if let Some(src) = page.src {
                // Fetch image at highest available resolution.
                let mut res = reqwest::blocking::get(src + "&w=10000").unwrap();

                // Determine image type from HTTP result.
                let mut ext = "jpg";
                for (name, value) in res.headers() {
                    if name.as_str() == "content-type" {
                        ext = value.to_str().unwrap();
                        let mut start = 0;
                        if let Some(x) = ext.find("/") {
                            start = x + 1
                        }
                        ext = &ext[start..];
                        if ext == "jpeg" {
                            ext = "jpg"
                        }
                        break;
                    }
                }

                // Generate filename based on page order, page ID, and image type.
                let filename = std::format!(
                    "{0}-{1}.{2}",
                    std::format!("{:0>3}", page_number_lookup.get(&page.pid).unwrap()),
                    page.pid,
                    ext
                );

                // Write to disk.
                if let Ok(mut file) =
                    std::fs::File::create_new(std::format!("{issue_pics_dir}/{filename}"))
                {
                    res.copy_to(&mut file).unwrap();
                }

                // If TOC entry exists for page ID, associate filename.
                if let Some(title) = toc_page_title_lookup.get(&page.pid) {
                    toc.add_page(title, &filename);
                }

                pages_downloaded.insert(page.pid);
            }
        }
    }

    // Download any formats not already downloaded.
    if formats.contains(FormatFlags::Pdf) {
        println!("Generating PDF...");

        create_pdf_with_toc(&issue_pics_dir, &filename_pdf, &toc)?;
    }
    if formats.contains(FormatFlags::Cbz) {
        println!("Generating CBZ...");

        create_cbz(&issue_pics_dir, &filename_cbz)?;
    }

    if !(options.keep_images || exists_already) {
        std::fs::remove_dir_all(&issue_pics_dir)?;
    }

    // All done. Add to list of downloaded books and update archive file if applicable.
    options.already_downloaded.insert(id.to_string());
    if let Some(archive) = options.archive_file.as_ref() {
        if let Ok(mut file) = OpenOptions::new().append(true).create(true).open(archive) {
            if let Err(e) = file.write(std::format!("{id}\n").as_bytes()) {
                eprintln!("Couldn't write to file: {}", e);
            }
        }
    }

    Ok(())
}

/// Downloads all issues within the selected period of the page at the provided URL.
pub fn download_period(url: &str, dest: &str, options: &mut ScraperOptions) -> io::Result<()> {
    for issue_url in get_issue_urls_in_period(url)? {
        download_issue(&issue_url, dest, options)?;
    }
    Ok(())
}

/// Downloads all issues within the series of the issue at the provided URL.
pub fn download_all(url: &str, dest: &str, options: &mut ScraperOptions) -> io::Result<()> {
    for period_url in get_period_urls(url)? {
        download_period(&period_url, dest, options)?;
    }
    Ok(())
}

/// Gets the URLs of available periods in the page at the provided URL.
pub fn get_period_urls(url: &str) -> io::Result<Vec<String>> {
    let mut ret = Vec::new();

    let mut res = reqwest::blocking::get(url).unwrap();
    let mut body = String::new();
    res.read_to_string(&mut body)?;
    let doc = Html::parse_document(&body);

    let selector = Selector::parse("#period_selector a").unwrap();
    for element in doc.select(&selector) {
        if let Some(x) = element.attr("href") {
            ret.push(if x.trim() == "" {
                url.to_string()
            } else {
                x.to_string()
            });
        }
    }

    Ok(ret)
}

/// Gets the URLs of issues within the selected period of the page at the provided URL.
pub fn get_issue_urls_in_period(url: &str) -> io::Result<Vec<String>> {
    let mut ret = Vec::new();

    let mut res = reqwest::blocking::get(url).unwrap();
    let mut body = String::new();
    res.read_to_string(&mut body)?;
    let doc = Html::parse_document(&body);

    let selector = Selector::parse("div.allissues_gallerycell a:first-child").unwrap();
    for element in doc.select(&selector) {
        if let Some(x) = element.attr("href") {
            ret.push(x.to_string());
        }
    }

    Ok(ret)
}