scryers/
lib.rs

1pub mod bulk;
2pub mod card;
3pub mod downloader;
4pub mod scryfall_structures;
5
6use bulk::{BulkDownload, BulkDownloadType};
7use std::{fs, io::Write, time::SystemTime};
8
9/// Downloads high resolution copies of all the cards. This over-respects Scryfall's api limits. Takes about 3 hours to complete.
10pub fn download_all_cards() {
11    let scryr = BulkDownload::new("./scryfall.db", BulkDownloadType::DefaultCards).unwrap();
12    let mut card_index = 1;
13
14    let start_time = SystemTime::now();
15    let cards = scryr.cards();
16    let total_cards = cards.len();
17    for card in cards {
18        if std::path::Path::new(&format!("images/{}-0.jpg", card.id())).exists() {
19            println!(
20                "{:0>5}/{} ({:.2}m..est {:.2}m remaining) : Skipping {}",
21                card_index,
22                total_cards,
23                start_time.elapsed().unwrap().as_secs_f64() / 60.0,
24                (card_index as f64 / start_time.elapsed().unwrap().as_secs_f64()
25                    * (total_cards - card_index) as f64)
26                    / 60.0,
27                card.name()
28            );
29            card_index += 1;
30            continue;
31        }
32
33        println!(
34            "{:0>5}/{} ({:.2}m..est {:.2}m remaining) : Downloading {}",
35            card_index,
36            total_cards,
37            start_time.elapsed().unwrap().as_secs_f64() / 60.0,
38            (card_index as f64 / start_time.elapsed().unwrap().as_secs_f64()
39                * (total_cards - card_index) as f64)
40                / 60.0,
41            card.name()
42        );
43        for (card_art_n, image) in (card.get_images().unwrap()).into_iter().enumerate() {
44            let mut file = fs::OpenOptions::new()
45                .create(true)
46                .write(true)
47                .open(format!("images/{}-{}.jpg", card.id(), card_art_n))
48                .unwrap();
49            file.write_all(&image).unwrap();
50        }
51        card_index += 1;
52    }
53}
54
55/// If any of the card images fail, you can use this to download the images for a specific ID again.
56pub fn download_cards_for_id(id: &str) {
57    let scryr = BulkDownload::new("./scryfall.db", BulkDownloadType::DefaultCards).unwrap();
58    for (card_art_n, image) in (scryr.get_card_by_id(id).unwrap().get_images().unwrap())
59        .into_iter()
60        .enumerate()
61    {
62        let mut file = fs::OpenOptions::new()
63            .create(true)
64            .write(true)
65            .open(format!("images/{}-{}.jpg", id, card_art_n))
66            .unwrap();
67        file.write_all(&image).unwrap();
68    }
69}