-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
193 lines (165 loc) · 6.65 KB
/
main.py
File metadata and controls
193 lines (165 loc) · 6.65 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
import asyncio
import json
import os
import subprocess
import time
from sys import platform
from src.discordrpc import DiscordRPC
from src.fetching import getVlcStatus
from src.front.simpleTerm import fetchThose
from src.lastfm import LastFM
import src.util.print as print
from src.itunesArt import fetchAlbumArt
class VlcApp:
def __init__(self, config):
self.config = config
self.lastTrackID = None
self.currentArt = "vlc"
self.playedTime = 0
self.lastPosition = 0
self.lastHeartbeat = 0
self.scrobbled = False
self.previousState = None
self.trackStartTime = None
self.vlcProcess = None
def startup(self):
path = ""
if platform == "win32":
if os.path.exists("C:\\Program Files\\VideoLAN\\VLC\\vlc.exe"):
path = "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe"
elif os.path.exists("C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe"):
path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe"
elif platform == "darwin":
if os.path.exists("/Applications/VLC.app/Contents/MacOS/VLC"):
path = "/Applications/VLC.app/Contents/MacOS/VLC"
elif os.path.exists("/Applications/VLC.app"):
path = "/Applications/VLC.app"
elif platform == "linux":
if os.path.exists("/usr/bin/vlc"):
path = "/usr/bin/vlc"
elif os.path.exists(
"/var/lib/flatpak/app/org.videolan.VLC/x86_64/stable/active/export/bin/org.videolan.VLC"
):
path = "/var/lib/flatpak/app/org.videolan.VLC/x86_64/stable/active/export/bin/org.videolan.VLC"
elif os.path.exists("/snap/bin/vlc"):
path = "/snap/bin/vlc"
if path:
self.vlcProcess = subprocess.Popen([path, "--extraintf=http"])
else:
print.error("VLC not found or unsupported platform.")
async def run(self):
self.startup()
self.rpc = DiscordRPC(self.config["discordID"])
self.lfm = LastFM(
self.config["lfmKey"],
self.config["lfmSecret"],
self.config["lfmUser"],
self.config["lfmPass"],
)
await self.rpc.connect()
await self.fetcherLoop()
async def fetcherLoop(self):
await asyncio.sleep(0.1)
while True:
if self.vlcProcess and self.vlcProcess.poll() is not None:
print("VLC closed. Exiting...")
await self.rpc.clear()
os._exit(0)
status = getVlcStatus(
self.config["vlcPassword"],
self.config["vlcPort"]
)
if isinstance(status, dict):
fetchThose(
status["title"],
status["artist"],
status["album"],
status["state"],
status["position"],
status["length"],
self.config["refreshRate"],
self.scrobbled,
status["lyrics"],
)
else:
os.system("cls" if os.name == "nt" else "clear")
print(status)
if not isinstance(status, dict):
await asyncio.sleep(self.config["refreshRate"])
continue
state = status["state"]
position = int(status["position"])
length = int(status["length"])
trackID = f"{status['title']} - {status['artist']}"
# funky discord RPC shit
if self.previousState == "playing" and state == "paused":
await self.rpc.updateStatus(status, self.currentArt, True)
elif self.previousState == "paused" and state == "playing":
await self.rpc.updateStatus(status, self.currentArt, False)
elif state == "stopped":
await self.rpc.updateStatus(status, self.currentArt, True)
if state == "playing":
if trackID != self.lastTrackID:
self.currentArt = fetchAlbumArt(
status["artist"],
status["title"]
)
self.lfm.updateNowPlaying(
status["artist"],
status["title"],
status["album"]
)
await self.rpc.updateStatus(status, self.currentArt, False)
self.lastHeartbeat = time.time()
self.lastTrackID = trackID
self.scrobbled = False
self.playedTime = 0
self.lastPosition = position
self.trackStartTime = int(time.time()) - position
if time.time() - self.lastHeartbeat >= 15:
self.lfm.updateNowPlaying(
status["artist"],
status["title"],
status["album"]
)
self.lastHeartbeat = time.time()
if self.lastTrackID == trackID:
delta = position - self.lastPosition
if 0 <= delta <= 3:
self.playedTime += delta
elif abs(delta) > 5:
self.lastPosition = position
self.lastPosition = position
if (
not self.scrobbled
and length > 30
and self.trackStartTime is not None
):
threshold = min(length / 2, 240)
if self.playedTime >= threshold:
self.lfm.scrobble(
status["artist"],
status["title"],
status["album"],
self.trackStartTime
)
self.scrobbled = True
if state == "paused" and self.previousState == "playing":
pass
self.previousState = state
await asyncio.sleep(self.config["refreshRate"])
def loadConfig():
if os.path.exists("config.json"):
with open("config.json", "r") as f:
config = json.load(f)
elif os.path.exists("VLC.UTILS.CONFIG.json"):
with open("VLC.UTILS.CONFIG.json", "r") as f:
config = json.load(f)
else:
print.fatal("Config not found.")
exit()
return config
if __name__ == "__main__":
config = loadConfig()
app = VlcApp(config)
asyncio.run(app.run())