Individually proxy custom emojis

This commit is contained in:
spikecodes 2021-02-19 18:18:09 -08:00
parent c586de66ba
commit 902c9a6e42
No known key found for this signature in database
GPG Key ID: 004CECFF9B463BCB
3 changed files with 19 additions and 13 deletions

View File

@ -175,6 +175,7 @@ async fn main() -> tide::Result<()> {
app.at("/vid/:id/:size/").get(proxy::video); app.at("/vid/:id/:size/").get(proxy::video);
app.at("/img/:id/").get(proxy::image); app.at("/img/:id/").get(proxy::image);
app.at("/thumb/:point/:id/").get(proxy::thumbnail); app.at("/thumb/:point/:id/").get(proxy::thumbnail);
app.at("/emoji/:id/:name/").get(proxy::emoji);
// Browse user profile // Browse user profile
app.at("/u/:name/").get(user::profile); app.at("/u/:name/").get(user::profile);

View File

@ -51,6 +51,13 @@ pub async fn thumbnail(req: Request<()>) -> tide::Result {
request(url).await request(url).await
} }
pub async fn emoji(req: Request<()>) -> tide::Result {
let id = req.param("id").unwrap_or_default();
let name = req.param("name").unwrap_or_default();
let url = format!("https://emoji.redditmedia.com/{}/{}", id, name);
request(url).await
}
async fn request(url: String) -> tide::Result { async fn request(url: String) -> tide::Result {
let http = surf::get(url).await.unwrap(); let http = surf::get(url).await.unwrap();

View File

@ -188,27 +188,25 @@ pub fn format_url(url: &str) -> String {
} else { } else {
let domain = Url::parse(url).map(|f| f.domain().unwrap_or_default().to_owned()).unwrap_or_default(); let domain = Url::parse(url).map(|f| f.domain().unwrap_or_default().to_owned()).unwrap_or_default();
let capture = |regex: &str, format: &str| { let capture = |regex: &str, format: &str, levels: i16| {
Regex::new(regex) Regex::new(regex)
.map(|re| match re.captures(url) { .map(|re| match re.captures(url) {
Some(caps) => [format, &caps[1], "/"].join(""), Some(caps) => match levels {
1 => [format, &caps[1], "/"].join(""),
2 => [format, &caps[1], "/", &caps[2], "/"].join(""),
_ => String::new()
},
None => String::new(), None => String::new(),
}) })
.unwrap_or_default() .unwrap_or_default()
}; };
match domain.as_str() { match domain.as_str() {
"v.redd.it" => { "v.redd.it" => capture(r"https://v\.redd\.it/(.*)/DASH_([0-9]{2,4}(\.mp4|$))", "/vid/", 2),
let re = Regex::new(r"https://v\.redd\.it/(.*)/DASH_([0-9]{2,4}(\.mp4|$))").unwrap(); "i.redd.it" => capture(r"https://i\.redd\.it/(.*)", "/img/", 1),
"a.thumbs.redditmedia.com" => capture(r"https://a\.thumbs\.redditmedia\.com/(.*)", "/thumb/a/", 1),
match re.captures(url) { "b.thumbs.redditmedia.com" => capture(r"https://b\.thumbs\.redditmedia\.com/(.*)", "/thumb/b/", 1),
Some(caps) => format!("/vid/{}/{}", &caps[1], &caps[2]), "emoji.redditmedia.com" => capture(r"https://emoji\.redditmedia\.com/(.*)/(.*)", "/emoji/", 2),
None => String::new(),
}
}
"i.redd.it" => capture(r"https://i\.redd\.it/(.*)", "/img/"),
"a.thumbs.redditmedia.com" => capture(r"https://a\.thumbs\.redditmedia\.com/(.*)", "/thumb/a/"),
"b.thumbs.redditmedia.com" => capture(r"https://b\.thumbs\.redditmedia\.com/(.*)", "/thumb/b/"),
_ => format!("/proxy/{}/", encode(url).as_str()), _ => format!("/proxy/{}/", encode(url).as_str()),
} }
} }