-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
discord.rs
151 lines (136 loc) · 4.83 KB
/
discord.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use discord_rich_presence::{
activity::{Activity, Assets, Button, Timestamps},
DiscordIpc, DiscordIpcClient,
};
use std::{thread::sleep, time::Duration};
use crate::{
trakt::{Trakt, TraktWatchingResponse},
utils::{get_watch_stats, log, MediaType},
};
pub struct Discord {
client: DiscordIpcClient,
}
#[derive(Default)]
pub struct Payload {
pub details: String,
pub state: String,
pub media: String,
pub link_imdb: String,
pub link_trakt: String,
pub img_url: String,
pub watch_percentage: String,
}
impl Discord {
pub fn new(discord_client_id: String) -> Discord {
Discord {
client: match DiscordIpcClient::new(&discord_client_id) {
Ok(client) => client,
Err(e) => {
log(&format!("Couldn't connect to Discord: {e}"));
panic!("Couldn't connect to Discord");
}
},
}
}
pub fn connect(&mut self) {
loop {
if self.client.connect().is_ok() {
break;
} else {
log("Failed to connect to Discord, retrying in 15 seconds");
sleep(Duration::from_secs(15));
}
}
}
pub fn close(&mut self) {
self.client.close().unwrap();
}
pub fn set_activity(
&mut self,
trakt_response: &TraktWatchingResponse,
trakt: &mut Trakt,
tmdb_token: String,
) {
let mut payload_data = Payload::default();
let img_url = match trakt_response.r#type.as_str() {
"movie" => {
let movie = trakt_response.movie.as_ref().unwrap();
payload_data.details = format!("{} ({})", movie.title, movie.year);
payload_data.state = format!(
"{:.1} ⭐️",
Trakt::get_movie_rating(trakt, movie.ids.slug.as_ref().unwrap().to_string())
);
payload_data.media = String::from("movies");
payload_data.link_imdb = format!(
"https://www.imdb.com/title/{}",
movie.ids.imdb.as_ref().unwrap()
);
payload_data.link_trakt = format!(
"https://trakt.tv/{}/{}",
payload_data.media,
movie.ids.slug.as_ref().unwrap()
);
let id_tmdb = movie.ids.tmdb.as_ref().unwrap();
trakt.get_poster(MediaType::Movie, id_tmdb.to_string(), tmdb_token, 0)
}
"episode" if trakt_response.episode.is_some() => {
let episode = trakt_response.episode.as_ref().unwrap();
let show = trakt_response.show.as_ref().unwrap();
payload_data.details = show.title.to_string();
payload_data.state =
format!("S{}E{} - {}", episode.season, episode.number, episode.title);
payload_data.media = String::from("shows");
payload_data.link_imdb = format!(
"https://www.imdb.com/title/{}",
show.ids.imdb.as_ref().unwrap()
);
payload_data.link_trakt = format!(
"https://trakt.tv/{}/{}",
payload_data.media,
show.ids.slug.as_ref().unwrap()
);
let id_tmdb = show.ids.tmdb.as_ref().unwrap();
trakt.get_poster(
MediaType::Show,
id_tmdb.to_string(),
tmdb_token,
episode.season,
)
}
_ => {
log(&format!("Unknown media type: {}", trakt_response.r#type));
return;
}
};
let img = match img_url {
Some(img) => img,
None => payload_data.media.to_string(),
};
let watch_time = get_watch_stats(trakt_response);
let payload = Activity::new()
.details(&payload_data.details)
.state(&payload_data.state)
.assets(
Assets::new()
.large_image(&img)
.small_image("trakt")
.small_text("Discrakt"),
)
.timestamps(
Timestamps::new()
.start(watch_time.start_date.timestamp())
.end(watch_time.end_date.timestamp()),
)
.buttons(vec![
Button::new("IMDB", &payload_data.link_imdb),
Button::new("Trakt", &payload_data.link_trakt),
]);
log(&format!(
"{} - {} | {}",
payload_data.details, payload_data.state, watch_time.watch_percentage
));
if self.client.set_activity(payload).is_err() {
self.connect();
}
}
}