-
Notifications
You must be signed in to change notification settings - Fork 1
/
igdb.rs
166 lines (149 loc) · 4.5 KB
/
igdb.rs
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 serde::{Deserialize, Serialize};
use std::collections::HashSet;
use ureq::get;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Cover {
pub id: u64,
pub image_id: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GameMode {
pub id: u64,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Genre {
pub id: u64,
pub name: String,
pub slug: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Theme {
pub id: u64,
pub name: String,
pub slug: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MultiplayerMode {
pub id: u64,
pub campaigncoop: bool,
pub dropin: bool,
pub game: u64,
pub lancoop: bool,
pub offlinecoop: bool,
pub offlinecoopmax: Option<u32>, // exists if offlinecoop is true
pub offlinemax: Option<u32>,
pub onlinecoop: bool,
pub onlinecoopmax: Option<u32>, // exists if onlinecoop is true
pub onlinemax: Option<u32>,
pub platform: Option<u64>,
pub splitscreen: bool,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Screenshot {
pub id: u64,
pub image_id: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Video {
pub id: u64,
pub video_id: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AlternativeName {
pub id: u64,
pub name: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Game {
pub id: u64,
pub slug: String,
pub name: String,
#[serde(default)]
pub alternative_names: Vec<AlternativeName>,
pub updated_at: u64,
pub summary: Option<String>,
pub cover: Option<Cover>,
#[serde(default)]
pub game_modes: Vec<u64>,
#[serde(default)]
pub genres: Vec<u64>,
#[serde(default)]
pub themes: Vec<u64>,
#[serde(default)]
pub keywords: HashSet<u64>,
#[serde(default)]
pub multiplayer_modes: Vec<MultiplayerMode>,
pub screenshots: Option<Vec<Screenshot>>,
pub videos: Option<Vec<Video>>,
}
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
Json(serde_json::error::Error),
}
const IGDB_MAX_LIMIT: usize = 500;
pub fn get_games<T>(user_key: &str, slugs: &[T]) -> Result<Vec<Game>, Error>
where
T: std::fmt::Display,
{
if slugs.is_empty() {
Ok(vec![])
} else if slugs.len() > IGDB_MAX_LIMIT {
let head = get_games(user_key, &slugs[0..IGDB_MAX_LIMIT]);
let tail = get_games(user_key, &slugs[IGDB_MAX_LIMIT..]);
match (head, tail) {
(Ok(head), Ok(tail)) => Ok([head, tail].concat()),
(err @ Err(_), _) | (_, err @ Err(_)) => err,
}
} else {
let conditions = slugs
.iter()
.map(|s| format!("slug = \"{}\"", &s))
.collect::<Vec<String>>()
.join(" | ");
let fields = [
"id",
"slug",
"name",
"updated_at",
"cover.image_id",
"videos.video_id",
"screenshots.image_id",
"summary",
"multiplayer_modes.*",
"game_modes",
"genres",
"themes",
"keywords",
"alternative_names.name",
];
let query = format!(
"fields {fields}; where {conditions}; limit {limit};",
fields = fields.join(", "),
conditions = conditions,
limit = IGDB_MAX_LIMIT
);
let response = get("https://api-v3.igdb.com/games")
.set("user-key", user_key)
.send_string(&query)
.into_string()
.map_err(Error::Io)?;
serde_json::from_str(&response).map_err(Error::Json)
}
}
pub fn get_genres(user_key: &str) -> Result<Vec<Genre>, Error> {
let response = get("https://api-v3.igdb.com/genres")
.set("user-key", user_key)
.send_string(&format!("fields id, name, slug; limit {};", IGDB_MAX_LIMIT))
.into_string()
.map_err(Error::Io)?;
serde_json::from_str(&response).map_err(Error::Json)
}
pub fn get_themes(user_key: &str) -> Result<Vec<Theme>, Error> {
let response = get("https://api-v3.igdb.com/themes")
.set("user-key", user_key)
.send_string(&format!("fields id, name, slug; limit {};", IGDB_MAX_LIMIT))
.into_string()
.map_err(Error::Io)?;
serde_json::from_str(&response).map_err(Error::Json)
}