-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeezer_rpc.py
147 lines (112 loc) · 4.81 KB
/
deezer_rpc.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
import pygetwindow as gw
import time
import re
from pypresence import Presence
import ctypes
# constants
discord_client_id = "1219930243205173298"
version = "v0.0.2"
# app initialization print
def print_app_initialization():
print(" ┃ 🪧 initializing app...")
time.sleep(1)
def get_valid_deezer_windows():
"""
filters windows to ensure only actual deezer app windows are selected.
returns:
list: a list of verified deezer windows.
"""
all_windows = gw.getWindowsWithTitle("") # get all windows
valid_deezer_windows = []
for window in all_windows:
title = window.title.lower()
# ensure it contains "deezer" but isn't a file explorer, vs code, or web browser
if "deezer" in title and not any(
x in title for x in ["explorer", "visual studio", "opera", "chrome", "firefox", "code", version]
):
valid_deezer_windows.append(window)
return valid_deezer_windows
def prompt_user_for_window(windows):
"""
prompts the user to choose a deezer window if multiple are detected.
args:
windows (list): a list of matching window objects.
returns:
window: the selected deezer window.
"""
print("\n ┃ 🔍 multiple valid deezer windows detected. please select one:")
for idx, window in enumerate(windows):
print(f" ┃ [{idx + 1}] {window.title}")
while True:
try:
choice = int(input(" ┃ ➡ enter the number of your choice: ")) - 1
if 0 <= choice < len(windows):
return windows[choice]
else:
print(" ┃ ⚠️ invalid choice. please enter a valid number.")
except ValueError:
print(" ┃ ⚠️ invalid input. please enter a number.")
def format_deezer_title(raw_title: str) -> str:
"""
cleans and formats the title of the current track from deezer.
removes extraneous parts such as track length and any curly brackets.
args:
raw_title (str): the title string from the deezer window.
returns:
str: the cleaned and formatted track title.
"""
title_parts = raw_title.split(" - ")
# remove the track duration if present
formatted_title = (" - ".join(title_parts[:-1]) if len(title_parts) > 1 else raw_title)
# remove any curly-bracketed text (e.g., album or artist information)
formatted_title = re.sub(r"\{.*?\}", "", formatted_title).strip()
return formatted_title
def update_discord_presence(deezer_window):
"""
connects to discord and updates the user's rich presence status based on the deezer window.
args:
deezer_window (window): the deezer window object to extract track information.
"""
rpc = Presence(discord_client_id)
rpc.connect()
print(" ┃ 🌟 updating discord rich presence...")
last_title = None # store last known song title
while True:
try:
window_title = deezer_window.title
clean_title = format_deezer_title(window_title)
# only update if the title has changed
if clean_title and clean_title != last_title:
rpc.update(details="🎧 Listening to deezer", state=f"🎶 {clean_title}")
print(f" ┃ 🎧 updated discord presence: {clean_title}")
last_title = clean_title # update last known title
time.sleep(1) # check title every second without flooding console
except Exception as error:
print(f" ┃ ❌ error updating discord presence: {error} ❌")
break
def main():
"""
main function to initialize deezer window detection and start discord presence update.
"""
ctypes.windll.kernel32.SetConsoleTitleW(f"Deezer RPC | {version}")
print(" ┃ 🔍 searching for deezer windows...")
deezer_windows = get_valid_deezer_windows()
if not deezer_windows:
print(" ┃ ⚠️ deezer window not detected. please open deezer and start playing a track.")
return
# ask user for auto or manual mode
while True:
mode_choice = input(" ┃ 🔄 select mode: [a]uto-detect / [m]anual selection: ").strip().lower()
if mode_choice in ['a', 'm']:
break
print(" ┃ ⚠️ invalid choice. please enter 'a' or 'm'.")
if mode_choice == 'a':
print(" ┃ 🚀 auto-detect mode selected. using first detected deezer window.")
selected_window = deezer_windows[0]
else:
selected_window = prompt_user_for_window(deezer_windows)
print(f" ┃ 🎶 using window: {selected_window.title}")
update_discord_presence(selected_window)
if __name__ == "__main__":
print_app_initialization()
main()