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
use crate::models::*;
use futures_util::future;
use reqwest::blocking::Client;
use scraper::Selector;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum ApiError {
    #[error(transparent)]
    NetworkError(#[from] reqwest::Error),

    // #[error("cannot parse the response")]
    #[error(transparent)]
    DeserializeError(#[from] serde_json::Error),

    #[error("cannot find any data")]
    BundleNotFound,
}

pub struct HumbleApi {
    auth_key: String,
}

impl HumbleApi {
    pub fn new(auth_key: &str) -> Self {
        Self {
            auth_key: auth_key.to_owned(),
        }
    }

    pub fn list_bundle_keys(&self) -> Result<Vec<String>, ApiError> {
        let client = Client::new();

        let res = client
            .get("https://www.humblebundle.com/api/v1/user/order")
            .header(reqwest::header::ACCEPT, "application/json")
            .header(
                "cookie".to_owned(),
                format!("_simpleauth_sess={}", self.auth_key),
            )
            .send()?
            .error_for_status()?;

        let game_keys = res
            .json::<Vec<GameKey>>()?
            .into_iter()
            .map(|g| g.gamekey)
            .collect();

        Ok(game_keys)
    }

    pub fn list_bundles(&self) -> Result<Vec<Bundle>, ApiError> {
        const CHUNK_SIZE: usize = 10;

        let client = reqwest::Client::new();
        let game_keys = self.list_bundle_keys()?;

        let runtime = tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .expect("cannot build the tokio runtime");

        let futures = game_keys
            .chunks(CHUNK_SIZE)
            .map(|keys| self.read_bundles_data(&client, keys));

        // Collect the Vec<Result<_,_>> into Result<Vec<_>, _>. This will automatically stop when an error is seen.
        // See https://doc.rust-lang.org/rust-by-example/error/iter_result.html#fail-the-entire-operation-with-collect
        let result: Result<Vec<Vec<Bundle>>, _> = runtime
            .block_on(future::join_all(futures))
            .into_iter()
            .collect();

        Ok(result?.into_iter().flatten().collect())
    }

    async fn read_bundles_data(
        &self,
        client: &reqwest::Client,
        keys: &[String],
    ) -> Result<Vec<Bundle>, ApiError> {
        let mut query_params: Vec<_> = keys
            .iter()
            .map(|key| ("gamekeys", key.as_str()))
            .collect();

        query_params.insert(0, ("all_tpkds", "true"));

        let res = client
            .get("https://www.humblebundle.com/api/v1/orders")
            .header(reqwest::header::ACCEPT, "application/json")
            .header(
                "cookie".to_owned(),
                format!("_simpleauth_sess={}", self.auth_key),
            )
            .query(&query_params)
            .send()
            .await?
            .error_for_status()?;

        let product_map = res.json::<BundleMap>().await?;
        Ok(product_map.into_values().collect())
    }

    pub fn read_bundle(&self, product_key: &str) -> Result<Bundle, ApiError> {
        let url = format!(
            "https://www.humblebundle.com/api/v1/order/{}?all_tpkds=true",
            product_key
        );

        let client = Client::new();
        let res = client
            .get(url)
            .header(reqwest::header::ACCEPT, "application/json")
            .header(
                "cookie".to_owned(),
                format!("_simpleauth_sess={}", self.auth_key),
            )
            .send()?
            .error_for_status()?;

        res.json::<Bundle>().map_err(|e| e.into())
    }

    /// Read Bundle Choices for the give month and year.
    ///
    /// `when` should be in the `month-year` format. For example: `"january-2023"`.
    /// Use `"home"` to get the current active data.
    pub fn read_bundle_choices(&self, when: &str) -> Result<HumbleChoice, ApiError> {
        let url = format!("https://www.humblebundle.com/membership/{}", when);

        let client = Client::new();
        let res = client
            .get(url)
            .header(
                "cookie".to_owned(),
                format!("_simpleauth_sess={}", self.auth_key),
            )
            .send()?
            .error_for_status()?;

        let html = res.text()?;
        self.parse_bundle_choices(&html)
    }

    fn parse_bundle_choices(&self, html: &str) -> Result<HumbleChoice, ApiError> {
        let document = scraper::html::Html::parse_document(html);
        // One of these two CSS IDs will match. First one is for the active
        // month, while the second is for previous months.
        let sel = Selector::parse(
            "script#webpack-subscriber-hub-data, script#webpack-monthly-product-data",
        )
        .unwrap();

        let scripts: Vec<_> = document.select(&sel).collect();
        if scripts.len() != 1 {
            return Err(ApiError::BundleNotFound);
        }

        let script = scripts.get(0).unwrap();
        let txt = script.inner_html();
        let obj: HumbleChoice = serde_json::from_str(&txt)?;
        Ok(obj)
    }
}