From 18497a8cbd6c04f1ca1a4d10dc6560ceb0629c64 Mon Sep 17 00:00:00 2001 From: xenofem Date: Wed, 5 Jul 2023 12:28:19 -0400 Subject: [PATCH] v1.1.0: optional season/part naming, draft mode --- .gitignore | 3 ++- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 2 ++ src/config.rs | 2 ++ src/main.rs | 14 ++------------ src/shows/mod.rs | 19 ++++++++++++++++++- 7 files changed, 28 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index a50a321..0b587f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /result -/shows.yaml \ No newline at end of file +/shows*.yaml +/.env \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index c49f507..01bfbd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1137,7 +1137,7 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "screencap-bot" -version = "1.0.0" +version = "1.1.0" dependencies = [ "anyhow", "dotenvy", diff --git a/Cargo.toml b/Cargo.toml index 9c85a6d..a333043 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "screencap-bot" -version = "1.0.0" +version = "1.1.0" edition = "2021" authors = ["xenofem "] license = "MIT" diff --git a/README.md b/README.md index ae40cad..0124db3 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ directory: - `SCREENCAP_BOT_COHOST_EMAIL`: the email address the bot should use to log into cohost - `SCREENCAP_BOT_COHOST_PASSWORD`: the password the bot should use to log into cohost - `SCREENCAP_BOT_COHOST_PAGE`: the cohost page the bot should post from +- `SCREENCAP_BOT_COHOST_DRAFT`: whether to create cohost posts as + drafts, eg for testing (default: `false`) - `RUST_LOG`: log levels, for the app as a whole and/or for specific submodules/libraries. See [`env_logger`](https://docs.rs/env_logger/latest/env_logger/)'s diff --git a/src/config.rs b/src/config.rs index 8f7cedb..15b324c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,7 @@ pub struct Config { pub cohost_email: String, pub cohost_password: String, pub cohost_page: String, + pub cohost_draft: bool, } const VAR_PREFIX: &str = "SCREENCAP_BOT_"; @@ -42,5 +43,6 @@ pub fn load() -> Config { cohost_email: expect_var("COHOST_EMAIL"), cohost_password: expect_var("COHOST_PASSWORD"), cohost_page: expect_var("COHOST_PAGE"), + cohost_draft: parse_var("COHOST_DRAFT").unwrap_or(false), } } diff --git a/src/main.rs b/src/main.rs index 32cc716..e4b6f30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,8 +11,6 @@ mod config; mod shows; mod video; -use crate::shows::EpisodeNumber; - lazy_static! { static ref RETRY_INTERVAL: Duration = Duration::from_secs(30); } @@ -79,15 +77,7 @@ async fn post_random_screencap( })?; let (num, file) = episodes.iter().choose(rng).unwrap(); - let descriptor = format!( - "{}{}", - title, - match num { - EpisodeNumber::Standalone => String::new(), - EpisodeNumber::SingleSeason(n) => format!(" episode {}", n), - EpisodeNumber::MultiSeason(season, ep) => format!(" season {} episode {}", season, ep), - } - ); + let descriptor = shows::display_show_episode(title, show, *num); info!("Selected: {} - {}", descriptor, file.display()); @@ -139,7 +129,7 @@ async fn post_random_screencap( content_warnings: vec![descriptor], attachments: vec![attachment], tags, - draft: false, + draft: conf.cohost_draft, adult_content: false, headline: String::new(), markdown: String::new(), diff --git a/src/shows/mod.rs b/src/shows/mod.rs index 275f5b6..191282d 100644 --- a/src/shows/mod.rs +++ b/src/shows/mod.rs @@ -15,11 +15,13 @@ pub struct Show { pub path: PathBuf, #[serde(default)] pub tags: Vec, + #[serde(default)] + pub parts: HashMap, } pub type Shows = HashMap; -#[derive(Debug, Eq, Hash, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub enum EpisodeNumber { Standalone, SingleSeason(u32), @@ -33,6 +35,21 @@ pub fn load>(shows_file: P) -> anyhow::Result { .context("Failed to parse YAML from shows file") } +pub fn display_show_episode(title: &str, show: &Show, episode: EpisodeNumber) -> String { + match episode { + EpisodeNumber::Standalone => title.to_string(), + EpisodeNumber::SingleSeason(n) => format!("{} episode {}", title, n), + EpisodeNumber::MultiSeason(season, ep) => format!( + "{} {} episode {}", + title, + show.parts + .get(&season) + .unwrap_or(&format!("season {}", season)), + ep + ), + } +} + impl Show { pub fn episodes(&self) -> anyhow::Result { let path = &self.path;