gog-sync 0.3.4

Synchronizes a GOG library with a local folder.
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! Uses the GOG api as described [here](https://gogapidocs.readthedocs.io/en/latest/).
//!
//! # Example
//! ```
//! use gog::Gog;
//!
//! let mut http_client = Http::new();
//! let mut gog = Gog::new(&mut http_client);
//! gog.login();
//! gog.sync(download_folder);
//! ```

use configfiles::{ConfigFiles, ConfigError};
use http::{Http, HttpError};
use models;
use models::content::Content;
use models::contentinfo::ContentInfo;
use models::data::Data;
use models::datainfo::DataInfo;
use models::extra::Extra;
use models::extrainfo::ExtraInfo;
use models::token::Token;
use serde_json;
use serde_json::Value;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::fmt;
use std::io;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;

/// Wraps `ConfigError`, `HttpError`, `serde_json::Error`, and `io::Error`.
#[derive(Debug)]
pub enum GogError {
    Error(&'static str),
    ConfigError(ConfigError),
    HttpError(HttpError),
    SerdeError(serde_json::Error),
    IOError(io::Error),
}

impl fmt::Display for GogError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            GogError::Error(ref err) => fmt::Display::fmt(err, f),
            GogError::ConfigError(ref err) => fmt::Display::fmt(err, f),
            GogError::HttpError(ref err) => fmt::Display::fmt(err, f),
            GogError::SerdeError(ref err) => fmt::Display::fmt(err, f),
            GogError::IOError(ref err) => fmt::Display::fmt(err, f),
        }
    }
}

impl From<ConfigError> for GogError {
    fn from(e: ConfigError) -> Self {
        GogError::ConfigError(e)
    }
}

impl From<HttpError> for GogError {
    fn from(e: HttpError) -> Self {
        GogError::HttpError(e)
    }
}

impl From<serde_json::Error> for GogError {
    fn from(e: serde_json::Error) -> Self {
        GogError::SerdeError(e)
    }
}

impl From<io::Error> for GogError {
    fn from(e: io::Error) -> Self {
        GogError::IOError(e)
    }
}

/// Wraps the GOG api.
/// At the moment Client ID and Client Secret are hardcoded to the galaxy client.
pub struct Gog<'a> {
    client_id: String,
    client_secret: String,
    http_client: &'a mut Http,
}

impl<'a> Gog<'a> {
    /// Create a new instance of `Gog`.
    pub fn new(http_client: &'a mut Http) -> Gog<'a> {
        Gog {
            client_id: String::from("46899977096215655"),
            client_secret: String::from("9d85c43b1482497dbbce61f6e4aa173a433796eeae2ca8c5f6129f2dc4de46d9"),
            http_client: http_client,
        }
    }

    /// Logs into a gog account.
    /// If a token was already saved it should work automatically. Otherwise you
    /// will get an url which you have to open in a browser. There you have to
    /// login and copy the code parameter from the next page into the prompt.
    pub fn login(&mut self) -> Result<(), GogError> {
        self.refresh_token_from_file()?;
        Ok(())
    }

    fn update_auth_header(&mut self, token: &Token) -> Result<(), GogError> {
        let auth_header = format!("Authorization: Bearer {token}", token = token.access_token);
        Ok(self.http_client.add_header(auth_header.as_str())?)
    }

    fn refresh_token_from_file(&mut self) -> Result<Token, GogError> {
        let config = ConfigFiles::new();
        let mut token: Token = match config.load("token.json") {
            Ok(value) => value,
            Err(_) => {
                let code = self.get_code()?;
                self.get_token(code.as_str())?
            }
        };

        if token.is_expired() {
            token = self.refresh_token(token.refresh_token.as_str())?;
        }

        self.update_auth_header(&token)?;

        config.save("token.json", &token)?;

        Ok(token)
    }

    /// Syncs the contents of a gog account with a local folder.
    /// Uses a hash to figure out whether something has changed.
    pub fn sync(&mut self,
                storage_path_games: &str,
                storage_path_movies: &str,
                os_filters: &Vec<String>,
                language_filters: &Vec<String>,
                resolution_filters: &Vec<String>,
                skip_movies: bool,
                skip_games: bool)
                -> Result<(), GogError> {
        let content_ids = self.get_content_ids()?;

        for content_id in content_ids {
            let content = match self.get_content(content_id,
                                                 &os_filters,
                                                 &language_filters,
                                                 &resolution_filters) {
                Ok(value) => value,
                Err(error) => {
                    error!("{}: {}", &content_id, error);
                    continue;
                }
            };

            if (content.is_movie && skip_movies) || (!content.is_movie && skip_games) {
                info!("filtering {}", content.title);
                continue;
            }

            match self.sync_content(content, storage_path_games, storage_path_movies) {
                Ok(_) => (),
                Err(error) => {
                    warn!("{:?}", error);
                    continue;
                }
            }
        }
        Ok(())
    }

    fn sync_content(&mut self,
                    content: Content,
                    storage_path_games: &str,
                    storage_path_movies: &str)
                    -> Result<(), GogError> {

        let content_root = if content.is_movie {
            Path::new(storage_path_movies).join(&content.title)
        } else {
            Path::new(storage_path_games).join(&content.title)
        };

        let content_info_path = Path::new(&content_root).join("info.json");
        let content_info_saved =
            match ConfigFiles::load_from_path::<ContentInfo>(&content_info_path) {
                Ok(value) => value,
                Err(_) => ContentInfo::new(),
            };

        let content_hash = models::get_hash(&content);

        if content_info_saved.hash == content_hash {
            info!("{} already up to date.", &content.title);
            return Ok(());
        }

        fs::create_dir_all(&content_root)?;

        let mut content_info = ContentInfo {
            hash: content_hash,
            id: content.id,
            title: content.title.clone(),
            cd_keys: content.cd_keys.clone(),
            data: HashMap::new(),
            extras: HashMap::new(),
        };

        ConfigFiles::save_to_path(&content_info_path, &content_info)?;

        self.save_keys(content.cd_keys, &content_root)?;

        for data in content.data {
            self.save_data(content.title.as_str(),
                           data,
                           &content_root,
                           &content_info_path,
                           &content_info_saved,
                           &mut content_info)?;
        }

        let dlc_root = Path::new(&storage_path_games)
            .join(&content.title)
            .join("dlcs");

        for dlc in content.dlcs {
            self.sync_content(dlc,
                              dlc_root.to_string_lossy().as_ref(),
                              storage_path_movies)?;
        }

        for extra in content.extras {
            self.save_extra(content.title.as_str(),
                            extra,
                            &content_root,
                            &content_info_path,
                            &content_info_saved,
                            &mut content_info)?;
        }

        Ok(())
    }

    fn save_file(&mut self,
                 content_title: &str,
                 relative_uri: &str,
                 data_root: &PathBuf)
                 -> Result<String, GogError> {
        fs::create_dir_all(&data_root)?;
        let data_uri = format!("https://embed.gog.com{}", relative_uri);

        let filename = self.api_request_get_filename(&data_uri)?;
        let file_path = Path::new(&data_root).join(&filename);

        if file_path.exists() {
            info!("{} for {} already exists", &filename, content_title);
            return Ok(filename.to_owned());
        }

        info!("downloading {} for {}...", &relative_uri, content_title);
        self.api_request_download(data_uri.as_str(), &data_root)
    }

    fn save_data(&mut self,
                 content_title: &str,
                 data: Data,
                 content_root: &PathBuf,
                 content_info_path: &PathBuf,
                 content_info_saved: &ContentInfo,
                 content_info: &mut ContentInfo)
                 -> Result<(), GogError> {
        let hash_saved = match content_info_saved.data.get(data.manual_url.as_str()) {
            Some(value) => value.hash,
            None => u64::min_value(),
        };

        let hash = models::get_hash(&data);
        if hash_saved == hash {
            info!("{} already up to date.", &data.manual_url);
            return Ok(());
        }

        let data_root = Path::new(&content_root).join(&data.language);

        let filename = self.save_file(content_title, &data.manual_url, &data_root)?;

        let data_info = DataInfo {
            hash: hash,
            filename: filename,
            language: data.language,
        };

        content_info
            .data
            .insert(data.manual_url.clone(), data_info);
        ConfigFiles::save_to_path(&content_info_path, &content_info)?;

        Ok(())
    }

    fn save_extra(&mut self,
                  content_title: &str,
                  extra: Extra,
                  content_root: &PathBuf,
                  content_info_path: &PathBuf,
                  content_info_saved: &ContentInfo,
                  content_info: &mut ContentInfo)
                  -> Result<(), GogError> {
        let hash_saved = match content_info_saved.data.get(extra.manual_url.as_str()) {
            Some(value) => value.hash,
            None => u64::min_value(),
        };

        let hash = models::get_hash(&extra);
        if hash_saved == hash {
            info!("{} already up to date.", &extra.manual_url);
            return Ok(());
        }

        let filename = self.save_file(content_title, &extra.manual_url, &content_root)?;

        let extra_info = ExtraInfo {
            hash: hash,
            filename: filename,
            name: extra.name,
        };

        content_info
            .extras
            .insert(extra.manual_url.clone(), extra_info);
        ConfigFiles::save_to_path(&content_info_path, &content_info)?;

        Ok(())
    }

    fn save_keys(&mut self,
                 cd_keys: BTreeMap<String, String>,
                 content_root: &PathBuf)
                 -> Result<(), GogError> {
        let key_root = content_root.join("keys");

        if cd_keys.len() > 0 {
            fs::create_dir_all(&key_root)?;
        }

        for (key, value) in cd_keys {
            let key_path = key_root.join(format!("{}.txt", key));
            let mut key_file = File::create(&key_path)?;
            key_file.write_all(value.as_bytes())?
        }

        Ok(())
    }

    fn get_code(&mut self) -> Result<String, GogError> {
        let auth_uri = self.auth_uri(self.client_id.as_str(), self.redirect_uri().as_str());

        println!("Open the following url in a browser, login to your account and paste the \
                  resulting code parameter.");
        println!("{}", auth_uri);

        let mut code = String::new();

        print!("Code: ");
        io::stdout().flush()?;

        io::stdin()
            .read_line(&mut code)
            .expect("Failed to read line");

        Ok(code)
    }

    fn get_token(&mut self, code: &str) -> Result<Token, GogError> {
        let token_uri = self.token_uri(self.client_id.as_str(),
                                       self.client_secret.as_str(),
                                       code,
                                       self.redirect_uri().as_str());

        let token_response = self.api_request_get(token_uri.as_str())?;

        match serde_json::from_str(&token_response) {
            Ok(value) => Ok(value),
            Err(error) => Err(GogError::SerdeError(error)),
        }
    }

    fn refresh_token(&mut self, refresh_token: &str) -> Result<Token, GogError> {
        let token_refresh_uri = self.token_refresh_uri(self.client_id.as_str(),
                                                       self.client_secret.as_str(),
                                                       refresh_token);

        let token_response = self.api_request_get(token_refresh_uri.as_str())?;

        match serde_json::from_str(&token_response) {
            Ok(value) => Ok(value),
            Err(error) => Err(GogError::SerdeError(error)),
        }
    }

    fn get_content_ids(&mut self) -> Result<Vec<u64>, GogError> {
        let games_uri = self.games_uri();
        let response = self.api_request_get(games_uri.as_str())?;
        let content_ids_raw: Value = serde_json::from_str(response.as_str())?;
        let content_ids_serde = &content_ids_raw["owned"];

        let mut content_ids: Vec<u64> = Vec::new();

        if !content_ids_serde.is_array() {
            return Err(GogError::Error("Error parsing content ids."));
        }

        for content_id in content_ids_serde.as_array().unwrap() {
            let content_id_parsed = content_id.as_u64().unwrap_or(0);

            if content_id_parsed == 0 {
                error!("Cant parse content id {}", content_id);
                continue;
            }

            // The numbers in this list are excluded because they refer to
            // favourites, promotions and such.
            if [1, 2, 3, 4, 5].contains(&content_id_parsed) {
                continue;
            }

            content_ids.push(content_id_parsed);
        }

        Ok(content_ids)
    }

    fn api_request_ensure_token(&mut self, response: &str) -> Result<bool, GogError> {
        let response_json: Value = match serde_json::from_str(response) {
            Ok(value) => value,
            Err(_) => return Ok(false),
        };

        if response_json.is_object() && response_json.as_object().unwrap().contains_key("error") &&
           response_json["error"] == "invalid_grant" {
            debug!("invalid grant, refreshing token...");
            self.refresh_token_from_file()?;

            Ok(true)
        } else {
            Ok(false)
        }
    }

    fn api_request_get(&mut self, uri: &str) -> Result<String, GogError> {
        let response = self.http_client.get(uri)?;
        if self.api_request_ensure_token(&response)? {
            Ok(self.http_client.get(uri)?)
        } else {
            Ok(response)
        }
    }

    fn api_request_get_filename(&mut self, uri: &str) -> Result<String, GogError> {
        let response = self.http_client.get_filename(uri)?;
        if self.api_request_ensure_token(&response)? {
            Ok(self.http_client.get_filename(uri)?)
        } else {
            Ok(response)
        }
    }

    fn api_request_download(&mut self, uri: &str, path: &PathBuf) -> Result<String, GogError> {
        let response = self.http_client.download(uri, path)?;
        if self.api_request_ensure_token(&response)? {
            Ok(self.http_client.download(uri, path)?)
        } else {
            Ok(response)
        }
    }

    fn get_content(&mut self,
                   content_id: u64,
                   os_filters: &Vec<String>,
                   language_filters: &Vec<String>,
                   resolution_filters: &Vec<String>)
                   -> Result<Content, GogError> {
        let content_uri = self.content_uri(content_id);
        debug!("looking for information at {}...", &content_uri);

        let response = self.api_request_get(content_uri.as_str())?;

        models::content::deserialize(content_id,
                                     response.as_str(),
                                     os_filters,
                                     language_filters,
                                     resolution_filters)
    }

    fn games_uri(&self) -> String {
        String::from("https://embed.gog.com/user/data/games")
    }

    fn redirect_uri(&self) -> String {
        String::from("https://embed.gog.com/on_login_success?origin=client")
    }

    fn auth_uri(&self, client_id: &str, redirect_uri: &str) -> String {
        format!("https://auth.gog.\
                 com/auth?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&layout=client2",
                client_id = client_id,
                redirect_uri = redirect_uri)
    }

    fn token_uri(&self,
                 client_id: &str,
                 client_secret: &str,
                 code: &str,
                 redirect_uri: &str)
                 -> String {
        format!("https://auth.gog.\
                 com/token?client_id={client_id}&client_secret={client_secret}&grant_type=authorization_code&code={code}&redirect_uri={redirect_uri}",
                client_id = client_id,
                client_secret = client_secret,
                code = code.trim(),
                redirect_uri = redirect_uri)
    }

    fn token_refresh_uri(&self,
                         client_id: &str,
                         client_secret: &str,
                         refresh_token: &str)
                         -> String {
        format!("https://auth.gog.\
                 com/token?client_id={client_id}&client_secret={client_secret}&grant_type=refresh_token&refresh_token={refresh_token}",
                client_id = client_id,
                client_secret = client_secret,
                refresh_token = refresh_token)
    }

    fn content_uri(&self, content_id: u64) -> String {
        format!("https://embed.gog.com/account/gameDetails/{content_id}.json",
                content_id = content_id)
    }
}