|
1 | | -use bathbot_util::constants::OSU_BASE; |
| 1 | +use bathbot_model::{ScrapedMedal, ScrapedUser}; |
| 2 | +use bathbot_util::{constants::OSU_BASE, html::decode_html_entities}; |
2 | 3 | use bytes::Bytes; |
3 | | -use eyre::{Report, Result, WrapErr}; |
| 4 | +use eyre::{ContextCompat, Report, Result, WrapErr}; |
4 | 5 | use http::response::Parts; |
5 | 6 | use hyper::{Request, header::USER_AGENT}; |
6 | 7 |
|
@@ -50,6 +51,38 @@ impl Client { |
50 | 51 | .map_err(Report::new) |
51 | 52 | } |
52 | 53 |
|
| 54 | + pub async fn get_medal_icon(&self, url: &str) -> Result<Bytes> { |
| 55 | + self.make_get_request(url, Site::OsuMedalIcon) |
| 56 | + .await |
| 57 | + .map_err(Report::new) |
| 58 | + } |
| 59 | + |
| 60 | + /// Don't use this; use `RedisManager::scraped_medals` instead. |
| 61 | + pub async fn get_medals(&self) -> Result<Box<[ScrapedMedal]>> { |
| 62 | + const KEY: &str = "data-initial-data="; |
| 63 | + |
| 64 | + let bytes = self.peppy_profile().await?; |
| 65 | + let data = std::str::from_utf8(&bytes)?; |
| 66 | + let start = data.find(KEY).wrap_err("missing key")? + KEY.len() + 1; |
| 67 | + let end = memchr::memchr(b'"', &bytes[start..]).wrap_err("missing end quote")? + start; |
| 68 | + |
| 69 | + let data_initial_data = &data[start..end]; |
| 70 | + let decoded = decode_html_entities(data_initial_data); |
| 71 | + |
| 72 | + let ScrapedUser { medals } = serde_json::from_str(&decoded) |
| 73 | + .wrap_err_with(|| format!("Failed to deserialize: {decoded}"))?; |
| 74 | + |
| 75 | + Ok(medals) |
| 76 | + } |
| 77 | + |
| 78 | + async fn peppy_profile(&self) -> Result<Bytes> { |
| 79 | + let url = "https://osu.ppy.sh/users/2"; |
| 80 | + |
| 81 | + self.make_get_request(url, Site::OsuProfile) |
| 82 | + .await |
| 83 | + .map_err(Report::new) |
| 84 | + } |
| 85 | + |
53 | 86 | /// Make sure you provide a valid url to a mapset cover |
54 | 87 | pub async fn get_mapset_cover(&self, cover: &str) -> Result<Bytes> { |
55 | 88 | self.make_get_request(&cover, Site::OsuMapsetCover) |
|
0 commit comments