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
use crate::client::{Lichess, LichessResult}; use crate::models::game::{Game, TVChannels, UserGame}; use bytes::Bytes; use futures_util::stream::Stream; use serde_json::{from_value, Value}; impl Lichess { pub async fn export_one_game_pgn( &self, game_id: &str, query_params: Option<&Vec<(&str, &str)>>, ) -> LichessResult<String> { let url = format!("{}/game/export/{}", self.base, game_id); let mut builder = self .client .get(&url) .header("Accept", "application/x-chess-pgn"); if let Some(query) = query_params { builder = builder.query(&query) } self.to_raw_str(builder).await } pub async fn export_one_game_json( &self, game_id: &str, query_params: Option<&Vec<(&str, &str)>>, ) -> LichessResult<Game> { let url = format!("{}/game/export/{}", self.base, game_id); let mut builder = self.client.get(&url).header("Accept", "application/json"); if let Some(query) = query_params { builder = builder.query(&query) } self.to_model_full(builder).await } pub async fn export_ongoing_game_pgn( &self, username: &str, query_params: Option<&Vec<(&str, &str)>>, ) -> LichessResult<String> { let url = format!("{}/api/user/{}/current-game", self.base, username); let mut builder = self .client .get(&url) .header("Accept", "application/x-chess-pgn"); if let Some(query) = query_params { builder = builder.query(query) } self.to_raw_str(builder).await } pub async fn export_ongoing_game_json( &self, username: &str, query_params: Option<&Vec<(&str, &str)>>, ) -> LichessResult<Game> { let url = format!("{}/api/user/{}/current-game", self.base, username); let mut builder = self.client.get(&url).header("Accept", "application/json"); if let Some(query) = query_params { builder = builder.query(query) } self.to_model_full(builder).await } pub async fn export_all_games_pgn( &self, username: &str, query_params: Option<&Vec<(&str, &str)>>, ) -> LichessResult<impl Stream<Item = LichessResult<Bytes>>> { let url = format!("{}/api/games/user/{}", self.base, username); let mut builder = self .client .get(&url) .header("Accept", "application/x-chess-pgn"); if let Some(query) = query_params { builder = builder.query(query) } self.to_raw_bytes(builder).await } pub async fn export_all_games_json( &self, username: &str, query_params: Option<&Vec<(&str, &str)>>, ) -> LichessResult<impl Stream<Item = LichessResult<Game>>> { let url = format!("{}/api/games/user/{}", self.base, username); let mut builder = self .client .get(&url) .header("Accept", "application/x-ndjson"); if let Some(query) = query_params { builder = builder.query(query) } self.to_model_stream(builder).await } pub async fn export_games_by_ids_json( &self, ids: &[&str], query_params: Option<&Vec<(&str, &str)>>, ) -> LichessResult<impl Stream<Item = LichessResult<Game>>> { let url = format!("{}/games/export/_ids", self.base); let mut builder = self .client .post(&url) .body(ids.join(",")) .header("Accept", "application/x-ndjson"); if let Some(query) = query_params { builder = builder.query(query) } self.to_model_stream(builder).await } pub async fn stream_current_games( &self, ids: &[&str], ) -> LichessResult<impl Stream<Item = LichessResult<Game>>> { let url = format!("{}/api/stream/games-by-users", self.base); let builder = self.client.post(&url).body(ids.join(",")); self.to_model_stream(builder).await } pub async fn get_ongoing_games(&self, nb_games: u8) -> LichessResult<Vec<UserGame>> { let url = format!("{}/api/account/playing", self.base); let builder = self.client.get(&url).query(&[("nb", nb_games)]); let now_playing_json = self.to_model_full::<Value>(builder); from_value(now_playing_json.await?["nowPlaying"].take()).map_err(Into::into) } pub async fn get_current_tv_games(&self) -> LichessResult<TVChannels> { let url = format!("{}/tv/channels", self.base); let builder = self.client.get(&url); self.to_model_full(builder).await } pub async fn import_one_game(&self, pgn: &str) -> LichessResult<String> { let url = format!("{}/api/import", self.base); let builder = self.client.post(&url).form(&[("pgn", pgn)]); let url_json = self.to_model_full::<Value>(builder); from_value(url_json.await?["url"].take()).map_err(Into::into) } }