lol-cal
terminal lolesports schedule viewer
git clone https://jhhm.no/repos/lol-cal.git
lol-cal/src/net/schedule.rs
get raw file: schedule.rs
1use serde::de; 2// TODO: remove this later 3use serde::Deserialize; 4use serde::Deserializer; 5use serde::Serialize; 6 7use reqwest::Client; 8use serde_json::Value; 9use tracing::info; 10 11use crate::net::*; 12 13#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] 14#[serde(rename_all = "camelCase")] 15struct Root { 16 data: Data, 17} 18 19#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] 20#[serde(rename_all = "camelCase")] 21struct Data { 22 schedule: Schedule, 23} 24 25#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] 26#[serde(rename_all = "camelCase")] 27pub struct Schedule { 28 pub pages: Pages, 29 #[serde(deserialize_with = "deserialize_events")] 30 pub events: Vec<Event>, 31} 32 33#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] 34#[serde(rename_all = "camelCase")] 35pub struct Pages { 36 pub older: Option<String>, 37 pub newer: Option<String>, 38} 39 40#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] 41#[serde(rename_all = "camelCase")] 42pub struct Event { 43 pub start_time: String, 44 pub block_name: String, 45 #[serde(rename = "match")] 46 pub match_field: Match, 47 pub state: String, 48 #[serde(rename = "type")] 49 pub type_field: String, 50 pub league: League, 51} 52 53#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] 54#[serde(rename_all = "camelCase")] 55pub struct Match { 56 pub teams: Vec<Team>, 57 pub id: String, 58 pub strategy: Strategy, 59} 60 61#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] 62#[serde(rename_all = "camelCase")] 63pub struct Team { 64 pub code: String, 65 pub image: String, 66 pub name: String, 67 pub result: Option<Resultt>, 68 pub record: Option<Record>, 69} 70 71#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] 72#[serde(rename_all = "camelCase")] 73pub struct Resultt { 74 pub game_wins: i64, 75 pub outcome: Option<String>, 76} 77 78#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] 79#[serde(rename_all = "camelCase")] 80pub struct Record { 81 pub losses: i64, 82 pub wins: i64, 83} 84 85#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] 86#[serde(rename_all = "camelCase")] 87pub struct Strategy { 88 pub count: i64, 89 #[serde(rename = "type")] 90 pub type_field: String, 91} 92 93#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] 94#[serde(rename_all = "camelCase")] 95pub struct League { 96 pub name: String, 97 pub slug: String, 98} 99 100// When an event is not a match the "type" field != match. 101// We need to filter these out 102fn deserialize_events<'de, D>(deserializer: D) -> Result<Vec<Event>, D::Error> 103where 104 D: Deserializer<'de>, 105{ 106 let raw_events: Vec<Value> = Deserialize::deserialize(deserializer)?; 107 let mut filtered = Vec::new(); 108 109 for raw in raw_events { 110 // Only process if type == "match" 111 if raw 112 .get("type") 113 .and_then(Value::as_str) 114 .map(|s| s == "match") 115 .unwrap_or(false) 116 { 117 let event: Event = serde_json::from_value(raw).map_err(|e| de::Error::custom(e))?; 118 filtered.push(event); 119 } 120 } 121 122 Ok(filtered) 123} 124 125const SCHEDULE_URL: &str = 126 "https://esports-api.lolesports.com/persisted/gw/getSchedule?hl=en-US&leagueId="; 127 128pub async fn fetch_schedule( 129 client: &Client, 130 slug: &str, 131 page: Option<&str>, 132) -> Result<Schedule, Error> { 133 let url = match page { 134 Some(token) => SCHEDULE_URL.to_owned() + &slug + "pageToken=" + &token, 135 None => SCHEDULE_URL.to_owned() + &slug, 136 }; 137 138 let response = client 139 .get(url) 140 .header(X_API_KEY_NAME, X_API_KEY_VALUE) 141 .send() 142 .await?; 143 144 if response.status().is_success() { 145 let api_response: Root = response 146 .json() 147 .await 148 .map_err(|e| Error::Deserialize(e.to_string()))?; 149 info!("{:?}", api_response.data.schedule); 150 return Ok(api_response.data.schedule); 151 } else { 152 return Err(Error::Request(response.status())); 153 } 154}