~erk/inbox

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch

[PATCH] WIP: Use gocomics cdn after 07-22-2020

Details
Message ID
<20200730005916.157217-1-flat@esoteric.moe>
DKIM signature
missing
Download raw message
Patch: +63 -6
Signed-off-by: Ken Swenson <flat@esoteric.moe>
---

This is a WIP patch to use the gocomics cdn after 7-22-2020 as they are not longer uploaded to the old CDN.
The issue with the current project structure is there are many sync fns that prevent us from using reqwest
without converting many things to async. Any feedback or directional ideas to be able to make webcalls from the date_url
function would be appreciated. I was short on time today writing this up.

 Cargo.toml   |  2 +-
 src/comic.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 63 insertions(+), 6 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 51fd814..4092a3d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,7 +13,7 @@ log = "0.4"
tracing = "0.1"
tracing-log = "0.1"
tracing-subscriber = "0.2"
reqwest = { default-features = false, features = ["default-tls"], version = "0.10" }
reqwest = { default-features = false, features = ["default-tls", "blocking"], version = "0.10" }
regex = "1"
rand = "0.7"
time = "0.2"
diff --git a/src/comic.rs b/src/comic.rs
index 3e0cdc7..2d4f3ca 100644
--- a/src/comic.rs
+++ b/src/comic.rs
@@ -9,9 +9,14 @@ use time::UtcOffset;
use crate::GarfieldResult;
use crate::HttpClient;

use regex::Regex;

use reqwest::blocking::Client as ReqwestClient;

/// The timezone of Paws Incâ„¢
const INDIANA: UtcOffset = UtcOffset::west_hours(5);
const GARFIELD_EPOCH: Date = time::date!(1978-06-18);
const IM_SORRY_JON: Date = time::date!(2020-07-22);

const FAVICON: &str =
    "https://cdn.discordapp.com/attachments/381880193700069377/506066660839653386/favicon.png";
@@ -189,12 +194,64 @@ impl ComicEmbed {
fn garfield_url(date: Date) -> Option<String> {
    let now = OffsetDateTime::now_utc().to_offset(INDIANA).date();
    if date > GARFIELD_EPOCH && date <= now {
        Some(format!("https://d1ejxu6vysztl5.cloudfront.net/comics/garfield/{}/{}-{:02}-{:02}.gif?format=png",
                     date.year(),
                     date.year(),
                     date.month(),
                     date.day()))
        if date > IM_SORRY_JON {
            let rqc = ReqwestClient::new();
            let cc = ComicClient::new_rwc(rqc.clone()).ok()?;
            if let Some(hash) = cc.get_hash(date).ok()? {
                Some(format!("https://assets.amuniversal.com/{}", hash))
            } else {
                None
            }
        } else {
            Some(format!("https://d1ejxu6vysztl5.cloudfront.net/comics/garfield/{}/{}-{:02}-{:02}.gif?format=png",
                         date.year(),
                         date.year(),
                         date.month(),
                         date.day()))
        }
    } else {
        None
    }
}

#[derive(Debug)]
pub struct ComicClient {
    rwc: ReqwestClient,
    hash_re: Regex,
}

impl ComicClient {
    pub fn new_rwc(rwc: ReqwestClient) -> GarfieldResult<Self> {
        let hash_re = Regex::new(r#"amuniversal.com/([a-f\d]{32})"#)?;
        Ok(Self {
            rwc,
            hash_re,
        })
    }

    pub fn get_hash(&self, date: Date) -> GarfieldResult<Option<String>> {
        let url = reqwest::Url::parse(
            &format!("https://gocomics.com/garfield/{}/{:02}/{:02}",
                    date.year(),
                    date.month(),
                    date.day())
        )?;

        let page = self
            .rwc
            .get(url)
            .header("user-agent", "curl/7.64.0")
            .send()?
            .text()?;

        if let Some(cap) = self.hash_re.captures(&page) {
            let hash = cap[1].parse::<String>();
            match hash {
                Ok(hash) => Ok(Some(hash)),
                Err(_) => Ok(None),
            }
        } else {
            Ok(None)
        }
    }
}
\ No newline at end of file
-- 
2.28.0
Reply to thread Export thread (mbox)