Skip to content

Commit

Permalink
Represent source platforms by name instead of URL
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Apr 3, 2024
1 parent 03573f6 commit 49a6714
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 107 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Subscribe to updates from people you follow, from any platform to any platform.
- Live streaming
- [bilibili 直播 (live.bilibili.com)](https://live.bilibili.com/)

### Notification
### Notification target

- [Telegram](https://telegram.org/)

Expand All @@ -39,16 +39,16 @@ Suzume = { platform = "Telegram", id = 1145141919, token = "1234567890:AbCdEfGhi

[[subscription.Suzume]] # define a subscription with name `Suzume`
# specify the platform and parameters
platform = { url = "live.bilibili.com", uid = 6610851 }
platform = { name = "bilibili.live", uid = 6610851 }
# reference to notify defined above, notifications will be pushed when the status changed
notify = ["Suzume"]

[[subscription.Suzume]]
platform = { url = "twitter.com", username = "suzumiyasuzume" }
platform = { name = "Twitter", username = "suzumiyasuzume" }
notify = ["Suzume", "Personal"]

[[subscription.CookieBacon]] # define a subscription with name `CookieBacon`
platform = { url = "live.bilibili.com", uid = 14172231 }
platform = { name = "bilibili.live", uid = 14172231 }
interval = '30s' # optional, override the global interval value for this individual subscription
# use `Personal` as the notification target, but with the parameter `thread_id = 514` overridden
notify = [ { ref = "Personal", thread_id = 514 } ]
Expand Down
78 changes: 40 additions & 38 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ impl Config {

#[derive(Clone, Debug, PartialEq, Default, Deserialize)]
pub struct PlatformGlobal {
#[serde(rename = "twitter.com")]
pub twitter_com: Option<PlatformGlobalTwitterCom>,
#[serde(rename = "Twitter")]
pub twitter: Option<PlatformGlobalTwitter>,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct PlatformGlobalTwitterCom {
pub struct PlatformGlobalTwitter {
pub nitter_host: String,
}

#[derive(Debug, PartialEq, Deserialize)]
pub struct SubscriptionRaw {
pub platform: Platform,
pub platform: SourcePlatform,
#[serde(default, with = "humantime_serde")]
pub interval: Option<Duration>,
#[serde(rename = "notify")]
Expand All @@ -96,64 +96,64 @@ pub struct SubscriptionRaw {

#[derive(Debug, PartialEq)]
pub struct SubscriptionRef<'a> {
pub platform: &'a Platform,
pub platform: &'a SourcePlatform,
pub interval: Option<Duration>,
pub notify: Vec<Notify>,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
#[serde(tag = "url")]
#[serde(tag = "name")]
#[allow(clippy::enum_variant_names)]
pub enum Platform {
#[serde(rename = "live.bilibili.com")]
LiveBilibiliCom(PlatformLiveBilibiliCom),
#[serde(rename = "space.bilibili.com")]
SpaceBilibiliCom(PlatformSpaceBilibiliCom),
#[serde(rename = "twitter.com")]
TwitterCom(PlatformTwitterCom),
pub enum SourcePlatform {
#[serde(rename = "bilibili.live")]
BilibiliLive(SourcePlatformBilibiliLive),
#[serde(rename = "bilibili.space")]
BilibiliSpace(SourcePlatformBilibiliSpace),
#[serde(rename = "Twitter")]
Twitter(SourcePlatformTwitter),
// Yea! PRs for supports of more platforms are welcome!
}

impl fmt::Display for Platform {
impl fmt::Display for SourcePlatform {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Platform::LiveBilibiliCom(p) => write!(f, "{p}"),
Platform::SpaceBilibiliCom(p) => write!(f, "{p}"),
Platform::TwitterCom(p) => write!(f, "{p}"),
SourcePlatform::BilibiliLive(p) => write!(f, "{p}"),
SourcePlatform::BilibiliSpace(p) => write!(f, "{p}"),
SourcePlatform::Twitter(p) => write!(f, "{p}"),
}
}
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct PlatformLiveBilibiliCom {
pub struct SourcePlatformBilibiliLive {
pub uid: u64,
}

impl fmt::Display for PlatformLiveBilibiliCom {
impl fmt::Display for SourcePlatformBilibiliLive {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "live.bilibili.com:{}", self.uid)
write!(f, "bilibili.live:{}", self.uid)
}
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct PlatformSpaceBilibiliCom {
pub struct SourcePlatformBilibiliSpace {
pub uid: u64,
}

impl fmt::Display for PlatformSpaceBilibiliCom {
impl fmt::Display for SourcePlatformBilibiliSpace {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "space.bilibili.com:{}", self.uid)
}
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct PlatformTwitterCom {
pub struct SourcePlatformTwitter {
pub username: String,
}

impl fmt::Display for PlatformTwitterCom {
impl fmt::Display for SourcePlatformTwitter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "twitter.com:{}", self.username)
write!(f, "Twitter:{}", self.username)
}
}

Expand Down Expand Up @@ -340,32 +340,32 @@ mod tests {
r#"
interval = '1min'
[platform."twitter.com"]
[platform."Twitter"]
nitter_host = "https://nitter.example.com/"
[notify]
meow = { platform = "Telegram", id = 1234, thread_id = 123, token = "xxx" }
woof = { platform = "Telegram", id = 5678, thread_id = 900, token = "yyy" }
[[subscription.meow]]
platform = { url = "live.bilibili.com", uid = 123456 }
platform = { name = "bilibili.live", uid = 123456 }
interval = '30s'
notify = ["meow"]
[[subscription.meow]]
platform = { url = "twitter.com", username = "meowww" }
platform = { name = "Twitter", username = "meowww" }
notify = ["meow", "woof"]
[[subscription.meow]]
platform = { url = "twitter.com", username = "meowww2" }
platform = { name = "Twitter", username = "meowww2" }
notify = ["meow", "woof", { ref = "woof", id = 123 }]
"#,
)
.unwrap(),
Config {
interval: Duration::from_secs(60), // 1min
platform: Some(PlatformGlobal {
twitter_com: Some(PlatformGlobalTwitterCom {
twitter: Some(PlatformGlobalTwitter {
nitter_host: "https://nitter.example.com/".into()
})
}),
Expand All @@ -391,14 +391,14 @@ notify = ["meow", "woof", { ref = "woof", id = 123 }]
"meow".into(),
vec![
SubscriptionRaw {
platform: Platform::LiveBilibiliCom(PlatformLiveBilibiliCom {
platform: SourcePlatform::BilibiliLive(SourcePlatformBilibiliLive {
uid: 123456
}),
interval: Some(Duration::from_secs(30)),
notify_ref: vec![NotifyRef::Direct("meow".into())],
},
SubscriptionRaw {
platform: Platform::TwitterCom(PlatformTwitterCom {
platform: SourcePlatform::Twitter(SourcePlatformTwitter {
username: "meowww".into()
}),
interval: None,
Expand All @@ -408,7 +408,7 @@ notify = ["meow", "woof", { ref = "woof", id = 123 }]
],
},
SubscriptionRaw {
platform: Platform::TwitterCom(PlatformTwitterCom {
platform: SourcePlatform::Twitter(SourcePlatformTwitter {
username: "meowww2".into()
}),
interval: None,
Expand Down Expand Up @@ -437,7 +437,7 @@ interval = '1min'
meow = { platform = "Telegram", id = 1234, thread_id = 123, token = "xxx" }
[[subscription.meow]]
platform = { url = "live.bilibili.com", uid = 123456 }
platform = { name = "bilibili.live", uid = 123456 }
notify = ["meow"]
"#
)
Expand All @@ -448,7 +448,7 @@ notify = ["meow"]
interval = '1min'
[[subscription.meow]]
platform = { url = "live.bilibili.com", uid = 123456 }
platform = { name = "bilibili.live", uid = 123456 }
notify = ["meow"]
"#
)
Expand All @@ -464,7 +464,7 @@ interval = '1min'
meow = { platform = "Telegram", id = 1234, thread_id = 123, token = "xxx" }
[[subscription.meow]]
platform = { url = "live.bilibili.com", uid = 123456 }
platform = { name = "bilibili.live", uid = 123456 }
notify = ["meow", "woof"]
"#
)
Expand All @@ -484,7 +484,7 @@ meow = { platform = "Telegram", id = 1234, thread_id = 123, token = "xxx" }
woof = { platform = "Telegram", id = 5678, thread_id = 456, token = "yyy" }
[[subscription.meow]]
platform = { url = "live.bilibili.com", uid = 123456 }
platform = { name = "bilibili.live", uid = 123456 }
notify = ["meow", { ref = "woof", thread_id = 114 }]
"#,
)
Expand All @@ -497,7 +497,9 @@ notify = ["meow", { ref = "woof", thread_id = 114 }]
vec![(
"meow".into(),
SubscriptionRef {
platform: &Platform::LiveBilibiliCom(PlatformLiveBilibiliCom { uid: 123456 }),
platform: &SourcePlatform::BilibiliLive(SourcePlatformBilibiliLive {
uid: 123456
}),
interval: None,
notify: vec![
Notify::Telegram(NotifyTelegram {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod config;
mod notify;
mod platform;
pub mod prop;
mod source;
mod task;

use std::path::Path;
Expand Down
2 changes: 1 addition & 1 deletion src/notify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{future::Future, pin::Pin};
use spdlog::prelude::*;

use self::telegram::TelegramNotifier;
use crate::{config, platform::Notification};
use crate::{config, source::Notification};

pub trait Notifier: Send + Sync {
fn notify<'a>(
Expand Down
2 changes: 1 addition & 1 deletion src/notify/telegram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tokio::sync::Mutex;
use super::Notifier;
use crate::{
config,
platform::{
source::{
LiveStatus, Notification, NotificationKind, Post, PostAttachment, PostsRef, RepostFrom,
StatusSource,
},
Expand Down
22 changes: 11 additions & 11 deletions src/platform/live_bilibili_com.rs → src/source/bilibili_live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use serde::Deserialize;
use serde_json::{self as json, json};

use super::{
Fetcher, LiveStatus, PlatformName, Status, StatusKind, StatusSource, StatusSourceUser,
Fetcher, LiveStatus, SourcePlatformName, Status, StatusKind, StatusSource, StatusSourceUser,
};
use crate::config::PlatformLiveBilibiliCom;
use crate::config::SourcePlatformBilibiliLive;

const LIVE_BILIBILI_COM_API: &str =
const BILIBILI_LIVE_API: &str =
"https://api.live.bilibili.com/room/v1/Room/get_status_info_by_uids";

#[derive(Deserialize)]
Expand All @@ -31,24 +31,24 @@ struct BilibiliResponseDataRoom {
cover_from_user: String,
}

pub struct LiveBilibiliComFetcher {
params: PlatformLiveBilibiliCom,
pub struct BilibiliLiveFetcher {
params: SourcePlatformBilibiliLive,
}

impl Fetcher for LiveBilibiliComFetcher {
impl Fetcher for BilibiliLiveFetcher {
fn fetch_status(&self) -> Pin<Box<dyn Future<Output = anyhow::Result<Status>> + Send + '_>> {
Box::pin(self.fetch_status_impl())
}
}

impl fmt::Display for LiveBilibiliComFetcher {
impl fmt::Display for BilibiliLiveFetcher {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.params)
}
}

impl LiveBilibiliComFetcher {
pub fn new(params: PlatformLiveBilibiliCom) -> Self {
impl BilibiliLiveFetcher {
pub fn new(params: SourcePlatformBilibiliLive) -> Self {
Self { params }
}

Expand All @@ -64,7 +64,7 @@ impl LiveBilibiliComFetcher {
live_url: format!("https://live.bilibili.com/{}", data.room_id),
}),
source: StatusSource {
platform_name: PlatformName::LiveBilibiliCom,
platform_name: SourcePlatformName::BilibiliLive,
user: Some(StatusSourceUser {
display_name: data.uname,
profile_url: format!("https://space.bilibili.com/{}", self.params.uid),
Expand All @@ -78,7 +78,7 @@ async fn fetch_bilibili_live_info(uid: u64) -> anyhow::Result<BilibiliResponseDa
let body = json!({ "uids": [uid] });

let resp = reqwest::Client::new()
.post(LIVE_BILIBILI_COM_API)
.post(BILIBILI_LIVE_API)
.json(&body)
.send()
.await
Expand Down
Loading

0 comments on commit 49a6714

Please sign in to comment.