-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
131 lines (109 loc) · 3.14 KB
/
main.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
from dotenv import load_dotenv
import spotify
import nest_asyncio
import asyncio
import activity
import math
import signal
import sys
def handler(signum, frame):
res = input("Ctrl-c was pressed. Do you really want to exit? y/n ")
if res == 'y':
activity.clear_status()
sys.exit(0)
signal.signal(signal.SIGINT, handler)
load_dotenv()
nest_asyncio.apply()
notes = ['\U0001f3b5', '\U0001f3b6']
async def update_activity(timer):
artist = 2
song = 2
album = 0
progress = 0
sleep_time = 1.5
#artist
for i in range(artist):
oldTimer = timer
await asyncio.sleep(sleep_time)
timer = spotify.get_milliseconds()
if(timer == -1):
return oldTimer
if(math.fabs(oldTimer - timer) < 200):
activity.clear_status()
else:
await set_artists_as_activity()
#song name
for i in range(song):
oldTimer = timer
await asyncio.sleep(sleep_time)
timer = spotify.get_milliseconds()
if(timer == -1):
return oldTimer
if(math.fabs(oldTimer - timer) < 200):
activity.clear_status()
else:
await set_track_as_activity()
#album
for i in range(album):
oldTimer = timer
await asyncio.sleep(sleep_time)
timer = spotify.get_milliseconds()
if(timer == -1):
return oldTimer
if(math.fabs(oldTimer - timer) < 200):
activity.clear_status()
else:
await set_album_as_activity()
#progress
for i in range(progress):
oldTimer = timer
await asyncio.sleep(sleep_time)
timer = spotify.get_milliseconds()
if(timer == -1):
return oldTimer
if(math.fabs(oldTimer - timer) < 200):
activity.clear_status()
else:
await set_progress_as_activity()
return timer
async def thread():
timer = spotify.get_milliseconds()
i = 0
while True:
try:
timer = await update_activity(timer)
except Exception as e:
print(str(e))
activity.clear_status()
if(i == 100):
await spotify.refresh_access_token()
i = 0
i = i + 1
async def set_track_as_activity():
presence = spotify.get_current_track()
if(presence != None):
activity.set_status(presence, notes[1])
else:
activity.clear_status()
async def set_album_as_activity():
presence = spotify.get_album_name()
if(presence != None):
activity.set_status(presence, notes[1])
else:
activity.clear_status()
async def set_progress_as_activity():
presence = spotify.get_progress() + " / " + spotify.get_duration()
activity.set_status(presence, notes[1])
async def set_artists_as_activity():
presence = spotify.get_current_artists()
if(presence != None):
activity.set_status(presence, notes[1])
else:
activity.clear_status()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
task = loop.create_task(thread())
try:
loop.run_until_complete(task)
except asyncio.CancelledError:
pass