-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
252 lines (191 loc) · 7.51 KB
/
main.py
File metadata and controls
252 lines (191 loc) · 7.51 KB
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
import hashlib
import json
import os
import re
import time
from pathlib import Path
from typing import Optional
from urllib.parse import unquote
import requests
from rpc import DiscordRPC, Activity, ActivityType, Timestamp, Asset
CLIENT_ID = "1376645197450055691"
ARTWORK_API_ENDPOINT = "" # Set this to your server's public URL. If left empty, it will use the default artwork
CACHE_FILE = Path(__file__).parent / "artwork_cache.json"
AUDIO_EXTENSIONS = {".mp3", ".flac", ".wav", ".ogg", ".aac", ".opus"}
DEFAULT_ARTWORK = "vlc_logo"
UPDATE_INTERVAL = 5
artwork_cache: dict[str, str] = {}
def log(message: str) -> None:
print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] {message}")
def load_cache() -> None:
if CACHE_FILE.exists():
try:
with open(CACHE_FILE, "r", encoding="utf-8") as f:
artwork_cache.update(json.load(f))
except json.JSONDecodeError:
log(f"Error loading cache file {CACHE_FILE}. It may be corrupted.")
def save_cache() -> None:
try:
with open(CACHE_FILE, "w", encoding="utf-8") as f:
json.dump(artwork_cache, f)
except Exception as e:
log(f"Error saving cache file {CACHE_FILE}: {e}")
def get_file_hash(file_path: str) -> str:
try:
stat = os.stat(file_path)
cache_string = f"{os.path.basename(file_path)}:{stat.st_size}:{stat.st_mtime}"
return hashlib.md5(cache_string.encode("utf-8")).hexdigest()
except Exception as e:
log(f"Error getting file info {file_path}: {e}")
return ""
def upload_artwork(artwork_path: str) -> str:
if not ARTWORK_API_ENDPOINT or not artwork_path:
return DEFAULT_ARTWORK
try:
artwork_path = ["/", ""][os.name == "nt"] + unquote(
artwork_path.replace("file:///", "", 1)
)
if not os.path.isfile(artwork_path):
return DEFAULT_ARTWORK
file_hash = get_file_hash(artwork_path)
if file_hash in artwork_cache:
return artwork_cache[file_hash]
elif file_hash == "":
return DEFAULT_ARTWORK
with open(artwork_path, "rb") as img_file:
files = {"file": img_file}
response = requests.post(ARTWORK_API_ENDPOINT, files=files, timeout=10)
if response.status_code == 200:
if url := response.json().get("url"):
artwork_cache[file_hash] = url
save_cache()
return url
except Exception as e:
log(f"Error uploading artwork: {e}")
return DEFAULT_ARTWORK
def get_vlc_web_interface_config() -> tuple[str, str, str]:
config = {"host": "127.0.0.1", "port": "8080", "password": ""}
if os.name == "nt":
vlc_config_path = Path(os.environ.get("APPDATA", "")) / "vlc" / "vlcrc"
else:
vlc_config_path = Path.home() / ".config" / "vlc" / "vlcrc"
if not vlc_config_path.exists():
return config["host"], config["port"], config["password"]
with open(vlc_config_path, "r", encoding="utf-8") as f:
content = f.read()
for key in ["host", "port", "password"]:
if match := re.search(rf"^http-{key}=(.+)", content, re.MULTILINE):
config[key] = match.group(1).strip()
return config["host"], config["port"], config["password"]
def fetch_vlc_status(
host: str, port: str, password: str
) -> tuple[Optional[dict], Optional[str]]:
url = f"http://{host}:{port}/requests/status.json"
auth = ("", password) if password else None
try:
response = requests.get(url, auth=auth, timeout=5)
response.raise_for_status()
return response.json(), None
except requests.exceptions.ConnectionError:
return None, "Could not connect to VLC web interface"
except requests.exceptions.Timeout:
return None, "Request timed out while connecting to VLC"
except requests.exceptions.HTTPError as e:
if e.response.status_code == 401:
return None, "Authentication failed"
else:
return None, f"HTTP {e.response.status_code} - {e.response.reason}"
except Exception as e:
return None, str(e)
def is_audio_file(status: dict) -> bool:
if not (
filename := status.get("information", {})
.get("category", {})
.get("meta", {})
.get("filename")
):
return False
file_extension = os.path.splitext(filename)[1].lower()
return file_extension in AUDIO_EXTENSIONS
def extract_media_info(status: dict) -> tuple[str, str, str, int, int, bool, str]:
meta = status.get("information", {}).get("category", {}).get("meta", {})
title = meta.get("title", "Unknown Title")
artist = meta.get("artist", "Unknown Artist")
album = meta.get("album", "")
artwork_url = meta.get("artwork_url", "")
if title == "Unknown Title" and "filename" in meta:
filename = os.path.basename(meta["filename"])
name, _ = os.path.splitext(filename)
if " - " in name:
parts = name.split(" - ", 1)
if artist == "Unknown Artist":
artist = parts[0]
title = parts[1]
else:
title = name
length = int(status.get("length", 0))
time_position = int(status.get("time", 0))
is_playing = status.get("state") == "playing"
return title, artist, album, length, time_position, is_playing, artwork_url
def create_discord_activity(
media_info: tuple[str, str, str, int, int, bool, str],
) -> Activity:
title, artist, album, length, position, is_playing, artwork_url = media_info
timestamps = None
if is_playing and length > 0:
current_time = int(time.time())
start_time = current_time - position
end_time = start_time + length
timestamps = Timestamp(start=start_time, end=end_time)
assets = Asset(
large_image=upload_artwork(artwork_url),
large_text=album if album else "No Album",
small_image="playing" if is_playing else "paused",
small_text="Playing" if is_playing else "Paused",
)
activity = Activity(
details=title,
state=artist,
activity_type=ActivityType.LISTENING,
timestamps=timestamps,
assets=assets,
)
return activity
def update_discord_presence(
rpc: DiscordRPC, vlc_config: tuple[str, str, str]
) -> Optional[str]:
host, port, password = vlc_config
status, error = fetch_vlc_status(host, port, password)
if error:
rpc.clear_activity()
return f"Failed to fetch VLC status: {error}"
state = status.get("state")
if state not in ["playing", "paused"] or not is_audio_file(status):
rpc.clear_activity()
return
media_info = extract_media_info(status)
activity = create_discord_activity(media_info)
if not rpc.set_activity(activity.to_dict()):
return "Failed to update Discord rich presence"
def run_discord_rpc(client_id: str, update_interval: int) -> None:
rpc = DiscordRPC(client_id)
vlc_config = get_vlc_web_interface_config()
load_cache()
if not rpc.connect():
log("Failed to connect to Discord")
return
log("Connected to Discord RPC")
log(f"Monitoring VLC at {vlc_config[0]}:{vlc_config[1]}")
try:
while True:
if error := update_discord_presence(rpc, vlc_config):
log(error)
time.sleep(update_interval)
except KeyboardInterrupt:
log("Shutting down...")
finally:
rpc.clear_activity()
rpc.disconnect()
log("Disconnected from Discord")
if __name__ == "__main__":
run_discord_rpc(CLIENT_ID, UPDATE_INTERVAL)