Skip to content

Commit

Permalink
Merge pull request #136 from Radiicall/activity-type-testing
Browse files Browse the repository at this point in the history
Set activity type for different media
  • Loading branch information
Radiicall authored Sep 28, 2024
2 parents 3fff9e5 + 0daeeb8 commit 7b17df0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 42 deletions.
33 changes: 15 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions jellyfin-rpc-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ time = "0.3"
serde_json = "1.0"

[dependencies.jellyfin-rpc]
version = "1.2.2"
#path = "../jellyfin-rpc"
#version = "1.2.2"
path = "../jellyfin-rpc"

[dependencies.clap]
features = ["derive"]
Expand Down
2 changes: 1 addition & 1 deletion jellyfin-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repository = "https://github.com/Radiicall/jellyfin-rpc"
keywords = ["jellyfin", "discord", "rich-presence"]

[dependencies]
discord-rich-presence = "0.2"
discord-rich-presence = { git = "https://github.com/vionya/discord-rich-presence" }
serde_json = "1.0"
log = "0.4"
url = "2.5"
Expand Down
18 changes: 10 additions & 8 deletions jellyfin-rpc/src/jellyfin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ impl Session {
artists
}

pub fn get_endtime(&self) -> Result<EndTime, SystemTimeError> {
pub fn get_time(&self) -> Result<PlayTime, SystemTimeError> {
match self.now_playing_item.media_type {
MediaType::Book => return Ok(EndTime::None),
MediaType::LiveTv => return Ok(EndTime::None),
MediaType::Book => return Ok(PlayTime::None),
MediaType::LiveTv => return Ok(PlayTime::None),
_ => {}
}

if self.play_state.is_paused
|| self.play_state.position_ticks.is_none()
|| self.now_playing_item.run_time_ticks.is_none()
{
return Ok(EndTime::Paused);
return Ok(PlayTime::Paused);
}

let ticks_to_seconds = 10000000;
Expand All @@ -101,18 +101,20 @@ impl Session {
.expect("Unreachable error")
/ ticks_to_seconds;

Ok(EndTime::Some(
Ok(PlayTime::Some(
SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() as i64
- position_ticks,
SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() as i64
+ (runtime_ticks - position_ticks),
))
}
}

#[derive(PartialEq)]
pub enum EndTime {
Some(i64),
None,
pub enum PlayTime {
Some(i64, i64),
Paused,
None
}

/// Contains information about buttons displayed in Discord
Expand Down
37 changes: 24 additions & 13 deletions jellyfin-rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use discord_rich_presence::activity::Button as ActButton;
use discord_rich_presence::activity::{ActivityType, Button as ActButton};
use discord_rich_presence::{
activity::{Activity, Assets, Timestamps},
DiscordIpc, DiscordIpcClient,
};
pub use error::JfError;
pub use jellyfin::{Button, MediaType};
use jellyfin::{EndTime, ExternalUrl, Item, RawSession, Session};
use jellyfin::{ExternalUrl, Item, PlayTime, RawSession, Session};
use log::debug;
use reqwest::header::{HeaderMap, AUTHORIZATION};
use std::str::FromStr;
Expand Down Expand Up @@ -133,15 +133,15 @@ impl Client {

let mut timestamps = Timestamps::new();

match session.get_endtime()? {
EndTime::Some(end) => timestamps = timestamps.end(end),
EndTime::None => (),
EndTime::Paused if self.show_paused => {
match session.get_time()? {
PlayTime::Some(start, end) => timestamps = timestamps.start(start).end(end),
PlayTime::None => (),
PlayTime::Paused if self.show_paused => {
assets = assets
.small_image("https://i.imgur.com/wlHSvYy.png")
.small_text("Paused");
}
EndTime::Paused => return Ok(String::new()),
PlayTime::Paused => return Ok(String::new()),
}

let buttons: Vec<Button>;
Expand Down Expand Up @@ -173,6 +173,14 @@ impl Client {
details += "‎‎‎";
}

match session.now_playing_item.media_type {
MediaType::Book => (),
MediaType::Music | MediaType::AudioBook => {
activity = activity.activity_type(ActivityType::Listening)
}
_ => activity = activity.activity_type(ActivityType::Watching),
}

activity = activity
.timestamps(timestamps)
.assets(assets)
Expand Down Expand Up @@ -511,10 +519,10 @@ impl Client {

let ancestors: Vec<Item> = self
.reqwest
.get(self.url.join(&format!(
"Items/{}/Ancestors",
session.now_playing_item.id
))?)
.get(
self.url
.join(&format!("Items/{}/Ancestors", session.now_playing_item.id))?,
)
.send()?
.json()?;

Expand Down Expand Up @@ -813,12 +821,15 @@ impl ClientBuilder {
/// ```
pub fn build(self) -> JfResult<Client> {
if self.url.is_empty() || self.usernames.is_empty() || self.api_key.is_empty() {
return Err(Box::new(JfError::MissingRequiredValues))
return Err(Box::new(JfError::MissingRequiredValues));
}

let mut headers = HeaderMap::new();

headers.insert(AUTHORIZATION, format!("MediaBrowser Token=\"{}\"", self.api_key).parse()?);
headers.insert(
AUTHORIZATION,
format!("MediaBrowser Token=\"{}\"", self.api_key).parse()?,
);
headers.insert("X-Emby-Token", self.api_key.parse()?);

Ok(Client {
Expand Down

0 comments on commit 7b17df0

Please sign in to comment.