Skip to content

Commit

Permalink
Fix downloading of twitch user emotes by switching to using their url…
Browse files Browse the repository at this point in the history
… template
  • Loading branch information
Nogesma committed Jun 8, 2024
1 parent f6cd802 commit 0ceeda2
Showing 1 changed file with 44 additions and 22 deletions.
66 changes: 44 additions & 22 deletions src/emotes/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,61 @@ type EmoteMap = HashMap<String, (String, String, bool)>;
mod twitch {
use crate::emotes::downloader::EmoteMap;
use color_eyre::Result;
use log::warn;
use reqwest::Client;
use serde::Deserialize;

#[derive(Deserialize)]
struct Image {
url_1x: String,
}

#[derive(Deserialize)]
#[derive(Deserialize, Debug)]

Check warning on line 24 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L24

Added line #L24 was not covered by tests
struct Emote {
id: String,
name: String,
images: Image,
format: Vec<String>,
scale: Vec<String>,
theme_mode: Vec<String>,
}

#[derive(Deserialize)]
#[derive(Deserialize, Debug)]

Check warning on line 33 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L33

Added line #L33 was not covered by tests
struct EmoteList {
data: Vec<Emote>,
template: String,
}

fn parse_emote_list(v: Vec<Emote>) -> EmoteMap {
v.into_iter()
.map(|emote| {
let url = if emote.format.contains(&String::from("animated")) {
emote.images.url_1x.replace("/static/", "/animated/")
} else {
emote.images.url_1x
};
fn parse_emote_list(v: EmoteList) -> EmoteMap {
let template = v.template;

Check warning on line 40 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L39-L40

Added lines #L39 - L40 were not covered by tests

(emote.name, (emote.id, url, false))
v.data
.into_iter()
.filter_map(|emote| {
let url = template.replace("{{id}}", &emote.id);

Check warning on line 45 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L42-L45

Added lines #L42 - L45 were not covered by tests

let url = url.replace(
"{{format}}",
if emote.format.contains(&String::from("animated")) {
"animated"

Check warning on line 50 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L47-L50

Added lines #L47 - L50 were not covered by tests
} else {
emote.format.first()?

Check warning on line 52 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L52

Added line #L52 was not covered by tests
},
);

let url = url.replace(
"{{theme_mode}}",
if emote.theme_mode.contains(&String::from("dark")) {
"dark"

Check warning on line 59 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L56-L59

Added lines #L56 - L59 were not covered by tests
} else {
emote.theme_mode.first()?

Check warning on line 61 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L61

Added line #L61 was not covered by tests
},
);

let url = url.replace(
"{{scale}}",
if emote.scale.contains(&String::from("1.0")) {
"1.0"

Check warning on line 68 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L65-L68

Added lines #L65 - L68 were not covered by tests
} else {
emote.scale.first()?

Check warning on line 70 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L70

Added line #L70 was not covered by tests
},
);

Some((emote.name, (emote.id, url, false)))
})
.collect()
}

Check warning on line 77 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L74-L77

Added lines #L74 - L77 were not covered by tests
Expand All @@ -59,8 +83,7 @@ mod twitch {
.await?
.error_for_status()?
.json::<EmoteList>()
.await?
.data;
.await?;

Check warning on line 86 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L86

Added line #L86 was not covered by tests

Ok(parse_emote_list(global_emotes))
}

Check warning on line 89 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L88-L89

Added lines #L88 - L89 were not covered by tests
Expand All @@ -72,10 +95,9 @@ mod twitch {
))
.send()
.await?
.error_for_status()?
.error_for_status().map_err(|e| { warn!("Unable to get user emotes, please verify that the access token includes the user:read:emotes scope."); e})?
.json::<EmoteList>()
.await?
.data;
.await?;

Check warning on line 100 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L91-L100

Added lines #L91 - L100 were not covered by tests

Ok(parse_emote_list(user_emotes))

Check warning on line 102 in src/emotes/downloader.rs

View check run for this annotation

Codecov / codecov/patch

src/emotes/downloader.rs#L102

Added line #L102 was not covered by tests
}
Expand Down

0 comments on commit 0ceeda2

Please sign in to comment.