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
use std::ops::Deref;
use std::str::FromStr;
use chrono::{DateTime, Utc};
use conv::TryFrom;
use futures::{future, Future, stream, Stream as StdStream};
use hyper::client::Connect;
use regex::Regex;
use serde::de::{self, Deserialize, Deserializer};
use ::{Client, League, ParseLeagueError};
use super::Stream;
#[derive(Clone, Debug)]
pub struct Leagues<C>
where C: Clone + Connect
{
client: Client<C>,
}
impl<C: Clone + Connect> Leagues<C> {
#[inline]
pub(crate) fn new(client: Client<C>) -> Self {
Leagues { client }
}
}
impl<C: Clone + Connect> Leagues<C> {
#[inline]
pub fn all(&self) -> Stream<LeagueInfo> {
self.get_leagues_stream(Type::All)
}
#[inline]
pub fn main(&self) -> Stream<LeagueInfo> {
self.get_leagues_stream(Type::Main)
}
#[inline]
pub fn event(&self) -> Stream<LeagueInfo> {
self.get_leagues_stream(Type::Event)
}
#[inline]
pub fn in_season<S: Into<String>>(&self, season: S) -> Stream<LeagueInfo> {
self.get_leagues_stream(Type::Season(season.into()))
}
fn get_leagues_stream(&self, type_: Type) -> Stream<LeagueInfo> {
enum State {
Start(Type),
Next{type_: Type, offset: usize},
End,
}
let this = self.clone();
Box::new(
stream::unfold(State::Start(type_), move |state| {
let (type_, offset) = match state {
State::Start(type_) => (type_, None),
State::Next{type_, offset} => (type_, Some(offset)),
State::End => return None,
};
let base_url = {
let mut season = None;
let type_param = match type_ {
Type::All => "all",
Type::Main => "main",
Type::Event => "event",
Type::Season(ref s) => { season = Some(s.clone()); "season" }
};
format!("{}?type={}&compact=1{}", LEAGUES_URL, type_param,
season.map(|s| format!("&season={}", s)).unwrap_or_else(String::new)).into()
};
let url = match offset {
Some(off) => format!("{}&offset={}", base_url, off),
None => base_url,
};
Some(this.client.get(url).and_then(move |resp: LeaguesResponse| {
let count = resp.len();
let next_offset = offset.unwrap_or(0) + count;
let leagues = resp.into_iter()
.filter(|l| l.start_at.is_some())
.filter_map(|l| {
let repr = format!("{:?}", l);
LeagueInfo::try_from(l).map_err(|e| {
warn!("Failed to parse league {}: {}", repr, e); e
}).ok()
});
let next_state = if count == MAX_COMPACT_ITEMS {
State::Next{type_, offset: next_offset}
} else {
State::End
};
future::ok((stream::iter_ok(leagues), next_state))
}))
})
.flatten()
)
}
}
const LEAGUES_URL: &str = "/leagues";
const MAX_COMPACT_ITEMS: usize = 230;
#[derive(Debug)]
pub struct LeagueInfo {
pub id: String,
league: League,
pub start_at: DateTime<Utc>,
pub end_at: Option<DateTime<Utc>>,
}
impl Deref for LeagueInfo {
type Target = League;
fn deref(&self) -> &Self::Target {
&self.league
}
}
impl TryFrom<RawLeagueInfo> for LeagueInfo {
type Err = ParseLeagueError;
fn try_from(input: RawLeagueInfo) -> Result<Self, Self::Err> {
lazy_static! {
static ref JUNK_RE: Regex = Regex::new(r#"\([^)]+\)"#).unwrap();
}
let league = League::from_str(JUNK_RE.replace(&input.id, "").trim())?;
Ok(LeagueInfo {
id: input.id,
league: league,
start_at: input.start_at.expect("RawLeagueInfo::start_at"),
end_at: input.end_at,
})
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
enum Type {
All,
Main,
Event,
Season(String),
}
type LeaguesResponse = Vec<RawLeagueInfo>;
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RawLeagueInfo {
id: String,
url: Option<String>,
#[serde(deserialize_with = "deserialize_opt_datetime")]
start_at: Option<DateTime<Utc>>,
#[serde(deserialize_with = "deserialize_opt_datetime")]
end_at: Option<DateTime<Utc>>,
}
fn deserialize_opt_datetime<'de, D>(deserializer: D) -> Result<Option<DateTime<Utc>>, D::Error>
where D: Deserializer<'de>
{
let opt_s: Option<String> = Deserialize::deserialize(deserializer)?;
match opt_s {
Some(ref s) => {
let fixed_datetime: DateTime<_> = DateTime::parse_from_rfc3339(s).map_err(|e| {
de::Error::custom(format!("failed to parse string as RFC3339 datetime: {}", e))
})?;
if fixed_datetime.offset().local_minus_utc() != 0 {
return Err(de::Error::custom(format!("expected UTC datetime, got one with offset {}",
fixed_datetime.offset())));
}
Ok(Some(fixed_datetime.with_timezone(&Utc)))
}
None => Ok(None),
}
}