-
Notifications
You must be signed in to change notification settings - Fork 0
/
anipresence.py
executable file
·266 lines (222 loc) · 7.3 KB
/
anipresence.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
import time
from typing import Pattern, Union
from pypresence import Presence
import os
import re
import requests
import json
class MetaDataCache:
cache = {}
cache_path = None
def __init__(self, cache_path):
self.cache_path = cache_path
self.cache = {}
cache_dir = os.path.dirname(self.cache_path)
if not os.path.exists(cache_dir):
os.mkdir(cache_dir)
if os.path.exists(self.cache_path):
with open(self.cache_path, "r") as f:
self.cache = json.loads(f.read())
else:
self._write_cache()
def _write_cache(self):
with open(self.cache_path, "w+") as f:
f.write(json.dumps(self.cache, indent=4))
def get_cover_image_url(self, title, epcount, fallback="watching") -> str:
key = f"{title} -- {epcount}"
if key not in self.cache.keys():
self.cache[key] = self._get_cover_image_url(
title, epcount, fallback
)
self._write_cache()
return self.cache[key]
def _get_cover_image_url(self, title, epcount, fallback="watching") -> str:
if epcount is not None:
epcount = int(epcount)
query = """
query($title: String) {
Page(page: 1, perPage: 10) {
media(search: $title, type: ANIME) {
title {
romaji
english
}
episodes
coverImage {
medium
}
}
}
}
"""
variables = {"title": title}
url = "https://graphql.anilist.co"
print("Requesting", variables)
resp = requests.post(
url, json={"query": query, "variables": variables}
)
print(resp.text)
if not resp.status_code == 200:
return fallback
animes = list(resp.json()["data"]["Page"]["media"])
anime = None
if len(animes) > 1:
filtered_a = list(
filter(
lambda a: a["title"]["romaji"].lower() == title.lower()
and a["episodes"] == epcount,
animes,
)
)
filtered_b = list(
filter(
lambda a: a["title"]["romaji"].lower() == title.lower(),
animes,
)
)
filtered_c = list(
filter(lambda a: a["episodes"] == epcount, animes)
)
for li in [filtered_a, filtered_b, filtered_c]:
if len(li) == 1:
anime = li[0]
break
elif len(animes) == 1:
anime = animes[0]
if anime is not None:
return anime["coverImage"]["medium"]
return fallback
class AniPlayerRegex:
pattern: Pattern
has_epcount: bool
is_hyphenated: bool
def __init__(self, pattern_str, has_epcount, is_hyphenated):
self.pattern = re.compile(pattern_str)
self.has_epcount = has_epcount
self.is_hyphenated = is_hyphenated
class AniPresence:
anime = None
regexes = [
AniPlayerRegex(
r".*mpv.*--force-media-title=(?P<title>.*) \((?P<epcount>[0-9]+) "
r"episodes.*episode-(?P<ep>[^-]+).*",
has_epcount=True,
is_hyphenated=False,
),
AniPlayerRegex(
r".*mpv.*--force-media-title=(?P<title>.*)-"
r"episode-(?P<ep>[^-]+).*",
has_epcount=False,
is_hyphenated=True,
),
AniPlayerRegex(
r".*mpv.*--force-media-title=(?P<title>.*) "
r"Episode (?P<ep>[0-9]+).*",
has_epcount=False,
is_hyphenated=False,
),
]
CACHE_PATH = os.path.expanduser("~/.cache/anipresence/cover.json")
cache: MetaDataCache
mpv_pid = None
rpc: Union[Presence, None] = None
rpc_connected = False
def __init__(self, client_id):
if self.other_is_running():
print("Other instance already running.")
return
print("Initializing RPC...")
self.rpc = Presence(client_id)
self.rpc.connect()
self.rpc_connected = True
print("...done")
self.cache = MetaDataCache(self.CACHE_PATH)
def __del__(self):
if self.rpc is not None and self.rpc_connected:
self.rpc.clear()
self.rpc.close()
def get_anime(self):
if self.mpv_pid is not None and self.mpv_pid != "PID":
try:
mpv_pid = int(self.mpv_pid)
os.kill(mpv_pid, 0)
except OSError:
print("Our mpv died")
return None, None
ps = os.popen("ps aux").read()
for line in ps.splitlines():
pid = re.split(r"[ ]+", line)[1]
for regex in self.regexes:
if m := regex.pattern.fullmatch(line):
if self.mpv_pid is not None:
self.mpv_pid = pid
return (
(
m.group("title"),
m.group("ep"),
m.group("epcount") if regex.has_epcount else None,
),
regex.is_hyphenated,
)
self.mpv_pid = None
return None, None
def update(self):
anime_new, hyphenated = self.get_anime()
print(anime_new)
if anime_new is None or self.rpc is None:
return False
if self.anime == anime_new:
return True
self.anime = anime_new
title, ep, epcount = self.anime
if hyphenated:
title = " ".join([word.capitalize() for word in title.split("-")])
print(f"Watching {title} episode {ep}, epcount {epcount}")
ep_line = f"Episode {ep}"
if epcount is not None and epcount == "1":
ep_line = "Watching"
self.rpc.clear()
self.rpc.update(
details=f"{title}",
state=ep_line,
large_image=self.try_get_cover_image_url(title, epcount),
start=int(time.time()),
)
print(f"Updated RPC with {title} and ep {ep}")
return True
def try_update(self):
try:
print("Updating")
return self.update()
except Exception as e:
print(e)
return True
def loop(self):
if self.other_is_running():
return
while self.try_update():
time.sleep(2)
def other_is_running(self):
pid = os.getpid()
return (
"python3"
in os.popen(f"ps aux | grep anipresence | grep -v {pid}").read()
)
def try_get_cover_image_url(
self, title, epcount, fallback="watching"
) -> str:
try:
return self.cache.get_cover_image_url(title, epcount, fallback)
except Exception as e:
print(e)
return fallback
def main():
client_id = "908703808966766602"
try:
if a := AniPresence(client_id):
a.loop()
except ConnectionRefusedError:
print("Connection refused. Is Discord running?")
if __name__ == "__main__":
main()