bugfixes #15
					 3 changed files with 31 additions and 16 deletions
				
			
		|  | @ -1,6 +1,6 @@ | ||||||
| [package] | [package] | ||||||
| name = "frogbot" | name = "frogbot" | ||||||
| version = "0.1.0" | version = "0.1.1" | ||||||
| edition = "2021" | edition = "2021" | ||||||
| 
 | 
 | ||||||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||||||
|  |  | ||||||
|  | @ -15,6 +15,7 @@ use regex::Regex; | ||||||
| use scraper::{Html, Selector}; | use scraper::{Html, Selector}; | ||||||
| 
 | 
 | ||||||
| /// Represents an Embed in the chat
 | /// Represents an Embed in the chat
 | ||||||
|  | #[derive(Default)] | ||||||
| pub struct Embed { | pub struct Embed { | ||||||
|     /// The title of the embed
 |     /// The title of the embed
 | ||||||
|     pub title: String, |     pub title: String, | ||||||
|  | @ -30,7 +31,7 @@ impl Embed { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /// Scrapes the HTML of a webpage and generates an [`Embed`] with the scraped information.
 | /// Scrapes the HTML of a webpage and generates an [`Embed`] with the scraped information.
 | ||||||
| pub fn parse_metadata(page: &str) -> Embed { | pub fn parse_metadata(page: &str) -> Option<Embed> { | ||||||
|     let doc_body = Html::parse_document(page); |     let doc_body = Html::parse_document(page); | ||||||
| 
 | 
 | ||||||
|     // Selectors used to get metadata are defined here
 |     // Selectors used to get metadata are defined here
 | ||||||
|  | @ -44,19 +45,23 @@ pub fn parse_metadata(page: &str) -> Embed { | ||||||
|     let mut meta_title = String::default(); |     let mut meta_title = String::default(); | ||||||
|     let mut meta_description = String::default(); |     let mut meta_description = String::default(); | ||||||
| 
 | 
 | ||||||
|     if let Some(title) = title { |     match (title, desc) { | ||||||
|         meta_title = title.text().collect(); |         // If both title and description aren't found return None
 | ||||||
|     } else { |         (None, None) => { | ||||||
|         warn!("Failed to parse title HTML"); |             warn!("Couldn't parse any metadata for URL"); | ||||||
|  |             return None; | ||||||
|  |         }, | ||||||
|  |         // Otherwise set the title/description to whatever we find
 | ||||||
|  |         (Some(title), Some(desc)) => { | ||||||
|  |             meta_title = title.text().collect(); | ||||||
|  |             meta_description = desc.value().attr("content").unwrap().to_string(); | ||||||
|  |         } | ||||||
|  |         // Handle logging of parse failures
 | ||||||
|  |         (Some(_), None) => warn!("Failed to parse description HTML"), | ||||||
|  |         (None, Some(_)) => warn!("Failed to parse title HTML"), | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if let Some(desc) = desc { |     Some(Embed::new(meta_title, meta_description)) | ||||||
|         meta_description = desc.value().attr("content").unwrap().to_string(); |  | ||||||
|     } else { |  | ||||||
|         warn!("Failed to parse description HTML"); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     Embed::new(meta_title, meta_description) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /// Check if the message has any urls in it and get them if it does
 | /// Check if the message has any urls in it and get them if it does
 | ||||||
|  | @ -119,15 +124,22 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie | ||||||
| 
 | 
 | ||||||
|         let urls = get_urls_from_message(&text_content.body); |         let urls = get_urls_from_message(&text_content.body); | ||||||
| 
 | 
 | ||||||
|         let reqwest_client = reqwest::Client::builder().user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36").build().unwrap(); |         let reqwest_client = reqwest::Client::builder().user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36").build().unwrap(); | ||||||
| 
 | 
 | ||||||
|         for url in urls { |         for mut url in urls { | ||||||
|             if let Ok(req) = reqwest_client.get(url).send().await { |             if let Ok(req) = reqwest_client.get(url).send().await { | ||||||
|                 if let Ok(res) = req.text().await { |                 if let Ok(res) = req.text().await { | ||||||
|                     // beware, dirty HTML parsing code
 |                     // beware, dirty HTML parsing code
 | ||||||
|                     let embed = parse_metadata(&res); |                     let metadata = parse_metadata(&res); | ||||||
|  | 
 | ||||||
|  |                     // If we didn't get any metadata set URL to nothing so it won't get repeated
 | ||||||
|  |                     // With no other embed data in the bot's embed message
 | ||||||
|  |                     if metadata.is_none() { | ||||||
|  |                         url = ""; | ||||||
|  |                     } | ||||||
| 
 | 
 | ||||||
|                     // Build our message reply
 |                     // Build our message reply
 | ||||||
|  |                     let embed = metadata.unwrap_or(Embed::new("No metadata found".to_string(), "".to_string())); | ||||||
|                     let bot_reply = RoomMessageEventContent::text_html( |                     let bot_reply = RoomMessageEventContent::text_html( | ||||||
|                         &embed.title, |                         &embed.title, | ||||||
|                         format!( |                         format!( | ||||||
|  |  | ||||||
|  | @ -141,6 +141,9 @@ pub async fn run(config: Config) -> anyhow::Result<()> { | ||||||
|         .await |         .await | ||||||
|         .expect("frogbot couldn't log into it's account."); |         .expect("frogbot couldn't log into it's account."); | ||||||
| 
 | 
 | ||||||
|  |     // Set the bot account's display name according to config
 | ||||||
|  |     client.account().set_display_name(Some(&config.display_name)).await?; | ||||||
|  | 
 | ||||||
|     warn!("Logged in successfully!"); |     warn!("Logged in successfully!"); | ||||||
|     warn!( |     warn!( | ||||||
|         "server: '{}', username: '{}', display name: '{}'", |         "server: '{}', username: '{}', display name: '{}'", | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue