-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
114 lines (87 loc) · 3.76 KB
/
app.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
import time
import env
import instagram
import spotify
class App:
def __init__(self):
self.instagram = instagram.Client()
self.spotify = spotify.Client()
self.running = True
self.lastTrackId = None
self.lastSetNoteId = None
self.playbackFailCount = 0
def deleteInstagramNote(
self,
onlyIfCreatedByThisScript=False
):
if self.lastSetNoteId is None:
# print("Instagram | No known note to delete")
if onlyIfCreatedByThisScript:
print("Instagram | No known note to delete")
#~ Let's still try to check if there is a note, corresponding to our note pattern
#~ If there is, we will delete it
#~ If there is not, we will just return
try:
note = self.instagram.getOurNote()
except:
return
if note.text.startswith("🎶"):
print("Instagram | Deleting pattern matching note")
self.instagram.deleteNote(note.id)
print("Instagram | Deleted note")
return
note = self.instagram.getOurNote()
if onlyIfCreatedByThisScript:
print("Instagram | Deleting note, if created by this script")
if note.id == self.lastSetNoteId:
self.instagram.deleteNote(note.id)
print("Instagram | Deleted note")
else:
print("Instagram | Note was not created by this script, skipping")
else:
print("Instagram | Deleting note")
self.instagram.deleteNote(note.id)
print("Instagram | Deleted note")
def start(self):
self.spotify.login()
self.instagram._login()
while self.running:
currentPlayback = self.spotify.getCurrentPlayback()
if not currentPlayback:
print("Spotify | Not playing / Not available")
self.wait(10)
self.playbackFailCount += 1
if self.playbackFailCount >= 3:
print("Spotify | Failed to get playback 3 times, deleting note")
self.deleteInstagramNote(onlyIfCreatedByThisScript=True)
self.playbackFailCount = 0
continue
if currentPlayback['is_playing'] is False:
print("Spotify | Not playing")
self.deleteInstagramNote(onlyIfCreatedByThisScript=True)
self.wait(60)
continue
if currentPlayback['item']['type'] != 'track':
raise Exception(f"Unsupported item type: {currentPlayback['item']['type']}")
track = currentPlayback['item']
timeLeft = track['duration_ms'] - currentPlayback['progress_ms']
if timeLeft <= 0:
# Sometimes, the progress_ms is higher than/equal to the duration_ms..
# So let's just avoid spamming Spotify API
timeLeft = 2000
if track['id'] == self.lastTrackId:
self.wait(timeLeft / 1000)
continue
self.lastTrackId = track['id']
print(f"Currently playing {track['name']} by {track['artists'][0]['name']}, next song in {timeLeft/1000}s")
self.instagram.setStatus(
f"🎶 {track['name']} by {track['artists'][0]['name']}",
audience=env.INSTAGRAM_DEFAULT_AUDIENCE
)
self.lastSetNoteId = self.instagram.getOurNote().id
self.wait(timeLeft / 1000)
def wait(self, seconds):
print(f"Waiting {seconds} seconds...")
time.sleep(seconds)
if __name__ == '__main__':
App().start()