about summary refs log tree commit diff
path: root/users
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-04-16T19·27+0200
committertazjin <tazjin@tvl.su>2022-04-16T20·35+0000
commitf6b8bf8b4188a05eec815a3fa6b6f3d8627f32e6 (patch)
tree3202c9fd8949ac28092b8c166b55d0ec5143ed66 /users
parent3d58dfb4ab1747f4cbc9e98ccc9b4b229476067a (diff)
feat(tazjin/tgsa): Support linked video thumbnails r/3964
I couldn't figure out how to get the ID for the `single=1` mode for
linking to a video directly, but linking to the post in embed mode
should also work.

Change-Id: Iebbd62724c36cee227a7c24968617ab418e8bd0c
Reviewed-on: https://cl.tvl.fyi/c/depot/+/5467
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'users')
-rw-r--r--users/tazjin/tgsa/src/main.rs73
1 files changed, 44 insertions, 29 deletions
diff --git a/users/tazjin/tgsa/src/main.rs b/users/tazjin/tgsa/src/main.rs
index d5c5e18b226e..cfb1c7223f79 100644
--- a/users/tazjin/tgsa/src/main.rs
+++ b/users/tazjin/tgsa/src/main.rs
@@ -30,6 +30,7 @@ struct TgMessage {
     author: String,
     message: Option<String>,
     photos: Vec<String>,
+    videos: Vec<String>,
 }
 
 fn extract_photo_url(style: &str) -> Option<&str> {
@@ -70,71 +71,85 @@ fn parse_tgmessage(embed: &str) -> Result<TgMessage> {
         }
     }
 
+    let video_sel = Selector::parse("i.tgme_widget_message_video_thumb").unwrap();
+    let mut videos = vec![];
+
+    for video in doc.select(&video_sel) {
+        if let Some(style) = video.value().attr("style") {
+            if let Some(url) = extract_photo_url(style) {
+                videos.push(url.to_string())
+            }
+        }
+    }
+
     Ok(TgMessage {
         author,
         message,
         photos,
+        videos,
     })
 }
 
-fn shorten_photo_links(msg: &mut TgMessage) -> Result<()> {
+fn shorten_link(link: &str) -> Result<String> {
     let mut url = url::Url::parse("https://tinyurl.com/api-create.php")?;
-    let mut shortened = vec![];
+    url.query_pairs_mut().clear().append_pair("url", link);
 
-    for photo in &msg.photos {
-        url.query_pairs_mut().clear().append_pair("url", &photo);
-        let request = url.as_str();
-
-        let response = crimp::Request::get(request)
-            .send()
-            .context("failed to shorten URL")?
-            .as_string()
-            .context("failed to decode shortened URL")?
-            .error_for_status(|resp| {
-                anyhow!("tinyurl request failed: {} ({})", resp.body, resp.status)
-            })?;
-
-        shortened.push(response.body.trim().into());
-    }
+    let request = url.as_str();
 
-    msg.photos = shortened;
+    let response = crimp::Request::get(request)
+        .send()
+        .context("failed to shorten URL")?
+        .as_string()
+        .context("failed to decode shortened URL")?
+        .error_for_status(|resp| {
+            anyhow!("tinyurl request failed: {} ({})", resp.body, resp.status)
+        })?;
 
-    Ok(())
+    Ok(response.body.trim().into())
 }
 
-fn to_bbcode(link: &TgLink, msg: &TgMessage) -> String {
+fn to_bbcode(link: &TgLink, msg: &TgMessage) -> Result<String> {
     let mut out = String::new();
 
     out.push_str(&format!("[quote=\"{}\"]\n", msg.author));
 
+    for video in &msg.videos {
+        out.push_str(&format!("[url=\"{}\"]", link.to_url()));
+        out.push_str(&format!("[img]{}[/img]", shorten_link(video)?));
+        out.push_str("[/url]\n");
+        out.push_str("[sub](Click thumbnail to open video)[/sub]\n")
+    }
+
     for photo in &msg.photos {
-        out.push_str(&format!("[timg]{}[/timg]\n", photo));
+        out.push_str(&format!("[timg]{}[/timg]\n", shorten_link(photo)?));
     }
 
     if let Some(message) = &msg.message {
         out.push_str(message);
     }
 
-    out.push_str("\n[/quote]\n");
     out.push_str(&format!(
-        "[i](via [url=\"{}\"]Telegram[/url])[/i]",
+        "\n\n[sub](via [url=\"{}\"]Telegram[/url])[/sub]",
         link.to_url(),
     ));
 
-    return out;
+    out.push_str("\n[/quote]\n");
+
+    Ok(out)
 }
 
 fn main() {
     crimp::init();
 
     let link = TgLink {
-        username: "RWApodcast".into(),
-        message_id: 113,
+        // username: "RWApodcast".into(),
+        // message_id: 113,
+        username: "intelslava".into(),
+        message_id: 25483,
     };
 
     let embed = fetch_embed(&link).unwrap();
-    let mut msg = parse_tgmessage(&embed).unwrap();
-    shorten_photo_links(&mut msg).unwrap();
+    let msg = parse_tgmessage(&embed).unwrap();
 
-    println!("{}", to_bbcode(&link, &msg));
+    println!("{}", to_bbcode(&link, &msg).unwrap());
 }