1use crate::client::ShindenAPI;
2use anyhow::Result;
3use reqwest::header::{HeaderMap, HeaderValue};
4use scraper::{Html, Selector};
5use crate::headers::get_headers_for_type;
6
7impl ShindenAPI {
8 pub async fn login(&self, email: &str, password: &str) -> Result<()> {
9 let url = "https://shinden.pl/main/login";
10 let _get_response_body = self.get_html(url).await?;
11
12 let form = vec![
13 ("username".into(), email.into()),
14 ("password".into(), password.into()),
15 ("remember".into(), "on".into()),
16 ];
17
18 let mut post_specific_headers = HeaderMap::new();
19 post_specific_headers.insert(reqwest::header::REFERER, HeaderValue::from_static("https://shinden.pl/main/login")); self.post_form(url, &form, Some(post_specific_headers)).await?;
22
23 Ok(())
24 }
25
26 pub async fn logout(&self) -> Result<()> {
27 let url = "https://shinden.pl/main/logout";
28 self.get_html(url).await?;
29 Ok(())
30 }
31
32 pub async fn get_user_name(&self) -> Result<Option<String>> {
33 let html = self.get_html("https://shinden.pl/user").await?;
34 let doc = Html::parse_document(&html);
35 let title = doc
36 .root_element()
37 .select(&Selector::parse("title").unwrap())
38 .next();
39 if let Some(t) = title {
40 let text = t.text().collect::<String>();
41 let username = text.split("(użytkownik)").next().unwrap_or("").trim();
42 return Ok(Some(username.to_string()));
43 }
44 Ok(None)
45 }
46
47 pub async fn get_user_profile_image(&self) -> Result<Option<String>> {
48 let html = self.get_html("https://shinden.pl/user").await?;
49 let doc = Html::parse_document(&html);
50 let img = doc
51 .select(&Selector::parse(".info-aside-img").unwrap())
52 .next();
53 if let Some(img_el) = img {
54 if let Some(src) = img_el.value().attr("src") {
55 return Ok(Some(format!("https://shinden.pl{}", src)));
56 }
57 }
58 Ok(None)
59 }
60}