-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathskip.py
executable file
·172 lines (136 loc) · 3.99 KB
/
skip.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
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
#!/usr/bin/env python3
"""
Check what BBC radio station iTunes/Winamp is now playing.
Stop playing until the next track starts.
"""
import time
from sys import platform as _platform
import bbcrealtime
from bbcscrobbler import normalise_station, osascript, output
def winamp_now_playing():
"""
If not Windows, return None.
If Windows, return BBC station Winamp is now playing or None.
"""
global w
station = None
if _platform != "win32":
return None
else:
# Is Winamp playing?
if w.getPlayingStatus() == "playing":
# Is Winamp playing BBC Radio?
now_playing = w.getCurrentTrackName()
if "bbc" not in now_playing.lower():
print("Winamp: Not BBC")
else:
station = normalise_station(now_playing)
return station
def itunes_now_playing():
"""
If not Mac, return None.
If Mac, return BBC station iTunes is now playing or None.
"""
station = None
if _platform != "darwin":
return None
else:
# Is iTunes running?
count = int(
osascript(
"osascript "
"-e 'tell application \"System Events\"' "
"-e 'count (every process whose name is \"iTunes\")'"
" -e 'end tell'"
)
)
if count == 0:
output("iTunes: not running")
else:
# Is iTunes playing?
state = osascript(
"osascript "
"-e 'tell application \"iTunes\" to player state as string'"
)
if state != "playing":
output("iTunes: " + state)
else:
# Is iTunes playing BBC Radio?
now_playing = osascript(
"osascript "
"-e 'tell application \"iTunes\"' "
"-e 'set thisTitle to name of current track' "
"-e 'set output to thisTitle' "
"-e 'end tell'"
)
if "bbc" not in now_playing.lower():
output("iTunes: Not BBC")
else:
station = normalise_station(now_playing)
return station
def init():
if _platform == "win32":
global w
import winamp # http://www.shalabh.com/software/about_winamp_py.html
w = winamp.winamp()
def media_player_now_playing():
if _platform == "darwin":
return itunes_now_playing()
elif _platform == "win32":
return winamp_now_playing()
else:
return None
def itunes_stop():
if _platform == "darwin":
return osascript("osascript -e 'tell application \"iTunes\" to pause'")
def itunes_play():
if _platform == "darwin":
return osascript("osascript -e 'tell application \"iTunes\" to play'")
def media_player_stop():
global w
if _platform == "darwin":
return itunes_stop()
elif _platform == "win32":
w.stop()
else:
return None
def media_player_play():
global w
if _platform == "darwin":
return itunes_play()
elif _platform == "win32":
w.play()
else:
return None
def format_track(track):
out = "{} - {}".format(track["artist"], track["title"])
return out
def thing():
init()
station = media_player_now_playing()
print("Station:", station)
if station is None:
return
skip = bbcrealtime.nowplaying(station)
if skip is None:
return
skip = format_track(skip)
print(skip)
media_player_stop()
while True:
try:
time.sleep(5)
now = bbcrealtime.nowplaying(station)
now = format_track(now)
# print(now)
print(".", end="")
if now != skip:
print()
print(now)
break
except BaseException:
break
media_player_play()
if __name__ == "__main__":
thing()
# End of file