Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON export support #7

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 84 additions & 73 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ serde_json = "1.0"
clap = { version = "3.2.8", features = ["derive"] }
env_logger = "0.8.4"
log = "0.4"
data-encoding = "2.3.2"

[profile.release]
lto = true
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::module::TaskStatus;
use anyhow::{anyhow, Result};
use crate::module::Status;
use serde::{Deserialize, Serialize};
use ts_rs::TS;

Expand Down Expand Up @@ -50,7 +50,7 @@ pub struct NotifierConfig {
#[ts(export, export_to = "web/src/bindings/")]
pub struct NotifierDiscordConfig {
pub webhook_url: String,
pub notify_on: Vec<TaskStatus>,
pub notify_on: Vec<Status>,
}

#[derive(Clone, TS, Serialize, Deserialize, Debug)]
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ async fn main() -> Result<()> {
let config = Arc::new(RwLock::new(config));
let h_scraper = run_module!(bus, module::scraper::RSS::new(config.clone()));
let h_recorder = run_module!(bus, module::recorder::YTArchive::new(config.clone()));
let h_json = run_module!(bus, module::recorder::Json::new(config.clone()));
let h_notifier = run_module!(bus, module::notifier::Discord::new(config.clone()));
let h_webserver = run_module!(bus, module::web::WebServer::new(config.clone()));

Expand All @@ -129,6 +130,7 @@ async fn main() -> Result<()> {
futures::try_join!(
h_scraper,
h_recorder,
h_json,
h_notifier,
h_signal,
h_bus,
Expand Down
135 changes: 132 additions & 3 deletions src/module/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use self::recorder::YTAStatus;
use self::recorder::{JsonStatus, YTAStatus};
use crate::{config::Config, msgbus::BusTx};
use anyhow::Result;
use async_trait::async_trait;
Expand All @@ -18,6 +18,7 @@ pub enum Message {
ToRecord(Task),
ToNotify(Notification),
RecordingStatus(RecordingStatus),
MetadataStatus(MetadataStatus),
}

#[derive(Debug, Clone, TS, Serialize, Deserialize)]
Expand All @@ -36,7 +37,14 @@ pub struct Task {
#[ts(export, export_to = "web/src/bindings/")]
pub struct Notification {
pub task: Task,
pub status: TaskStatus,
pub status: Status,
}

#[derive(Debug, Clone, PartialEq, Serialize, TS)]
#[ts(export, export_to = "web/src/bindings/")]
pub enum Status {
Task(TaskStatus),
Playability(PlayabilityStatus),
}

#[derive(Debug, Clone, TS)]
Expand All @@ -46,6 +54,83 @@ pub struct RecordingStatus {
pub status: YTAStatus,
}

#[derive(Debug, Clone, TS)]
#[ts(export, export_to = "web/src/bindings/")]
pub struct MetadataStatus {
pub task: Task,
pub status: JsonStatus,
}

#[derive(Debug, Clone, PartialEq, TS)]
#[ts(export, export_to = "web/src/bindings/")]
pub enum PlayabilityStatus {
MembersOnly,
Privated,
Copyrighted,
Removed,
Unlisted,
OnLive,
Ok,
Offline,
LoginRequired,
Unknown,
}
impl Serialize for PlayabilityStatus {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(match self {
PlayabilityStatus::Ok => "ok",
PlayabilityStatus::OnLive => "live",
PlayabilityStatus::Removed => "removed",
PlayabilityStatus::Offline => "offline",
PlayabilityStatus::MembersOnly => "members_only",
PlayabilityStatus::Unknown => "unknown",
PlayabilityStatus::Privated => "privated",
PlayabilityStatus::Unlisted => "unlisted",
PlayabilityStatus::Copyrighted => "copyrighted",
PlayabilityStatus::LoginRequired => "login_required",
})
}
}

impl<'de> Deserialize<'de> for PlayabilityStatus {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
match &*s {
"ok" => Ok(PlayabilityStatus::Ok),
"live" => Ok(PlayabilityStatus::OnLive),
"removed" => Ok(PlayabilityStatus::Removed),
"offline" => Ok(PlayabilityStatus::Offline),
"members_only" => Ok(PlayabilityStatus::MembersOnly),
"unknown" => Ok(PlayabilityStatus::Unknown),
"privated" => Ok(PlayabilityStatus::Privated),
"unlisted" => Ok(PlayabilityStatus::Unlisted),
"copyrighted" => Ok(PlayabilityStatus::Copyrighted),
"login_required" => Ok(PlayabilityStatus::LoginRequired),
_ => Err(serde::de::Error::unknown_variant(
&s,
&[
"ok",
"live",
"removed",
"offline",
"members_only",
"unknown",
"privated",
"unlisted",
"copyrighted",
"login_required",
],
)),
}
}
}

#[derive(Debug, Clone, PartialEq, TS)]
#[ts(export, export_to = "web/src/bindings/")]
pub enum TaskStatus {
Expand All @@ -69,7 +154,7 @@ impl Serialize for TaskStatus {
}
}

impl<'de> serde::Deserialize<'de> for TaskStatus {
impl<'de> Deserialize<'de> for TaskStatus {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
Expand All @@ -88,6 +173,50 @@ impl<'de> serde::Deserialize<'de> for TaskStatus {
}
}

impl<'de> Deserialize<'de> for Status {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
match &*s {
"ok" => Ok(Status::Playability(PlayabilityStatus::Ok)),
"live" => Ok(Status::Playability(PlayabilityStatus::OnLive)),
"removed" => Ok(Status::Playability(PlayabilityStatus::Removed)),
"offline" => Ok(Status::Playability(PlayabilityStatus::Offline)),
"members_only" => Ok(Status::Playability(PlayabilityStatus::MembersOnly)),
"unknown" => Ok(Status::Playability(PlayabilityStatus::Unknown)),
"privated" => Ok(Status::Playability(PlayabilityStatus::Privated)),
"unlisted" => Ok(Status::Playability(PlayabilityStatus::Unlisted)),
"copyrighted" => Ok(Status::Playability(PlayabilityStatus::Copyrighted)),
"login_required" => Ok(Status::Playability(PlayabilityStatus::LoginRequired)),
"waiting" => Ok(Status::Task(TaskStatus::Waiting)),
"recording" => Ok(Status::Task(TaskStatus::Recording)),
"done" => Ok(Status::Task(TaskStatus::Done)),
"failed" => Ok(Status::Task(TaskStatus::Failed)),
_ => Err(serde::de::Error::unknown_variant(
&s,
&[
"ok",
"live",
"removed",
"offline",
"members_only",
"unknown",
"privated",
"unlisted",
"copyrighted",
"login_required",
"waiting",
"recording",
"done",
"failed",
],
)),
}
}
}

#[async_trait]
pub trait Module<T: Debug + Clone + Sync = Message> {
fn new(config: Arc<RwLock<Config>>) -> Self;
Expand Down
24 changes: 18 additions & 6 deletions src/module/notifier.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Message, Module, Notification, TaskStatus};
use super::{Message, Module, Notification, PlayabilityStatus, Status, TaskStatus};
use crate::msgbus::BusTx;
use crate::{config::Config, APP_NAME, APP_USER_AGENT};
use anyhow::Result;
Expand Down Expand Up @@ -83,10 +83,22 @@ impl Module for Discord {
}

let (title, color) = match status {
TaskStatus::Waiting => ("Waiting for Live", 0xebd045),
TaskStatus::Recording => ("Recording", 0x58b9ff),
TaskStatus::Done => ("Done", 0x45eb45),
TaskStatus::Failed => ("Failed", 0xeb4545),
Status::Task(TaskStatus::Waiting) => ("Waiting for Live", 0xebd045),
Status::Task(TaskStatus::Recording) => ("Recording", 0x58b9ff),
Status::Task(TaskStatus::Done) => ("Done", 0x45eb45),
Status::Task(TaskStatus::Failed) => ("Failed", 0xeb4545),
Status::Playability(PlayabilityStatus::Ok) => ("Available", 0x45eb45),
Status::Playability(PlayabilityStatus::MembersOnly) => ("Members Only", 0xeb4545),
Status::Playability(PlayabilityStatus::Copyrighted) => ("Copyrighted", 0xeb4545),
Status::Playability(PlayabilityStatus::Removed) => ("Removed", 0xeb4545),
Status::Playability(PlayabilityStatus::LoginRequired) => {
("Login Required", 0xeb4545)
}
Status::Playability(PlayabilityStatus::Unlisted) => ("Unlisted", 0xeb4545),
Status::Playability(PlayabilityStatus::Unknown) => ("Unknown", 0xeb4545),
Status::Playability(PlayabilityStatus::OnLive) => ("Live", 0xeb4545),
Status::Playability(PlayabilityStatus::Privated) => ("Privated", 0xeb4545),
Status::Playability(PlayabilityStatus::Offline) => ("Offline", 0xeb4545),
};
let timestamp = chrono::Utc::now().to_rfc3339();

Expand All @@ -105,7 +117,7 @@ impl Module for Discord {
footer: DiscordEmbedFooter {
text: APP_NAME.into(),
},
timestamp: timestamp,
timestamp,
thumbnail: DiscordEmbedThumbnail {
url: task.video_picture,
},
Expand Down
Loading