-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.py
154 lines (136 loc) · 4.87 KB
/
console.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
import login
import radio
import init
import info
import sys
import time
import os
import logging
import playlist
import HTMLParser
import random
logger = logging.getLogger("console")
ERROR_WAIT = 5
def play_track(state, track):
is_hq = info.is_vip(state)
if is_hq:
try:
url = track.get_hq_location(state)
except Exception as e:
print("WARNING: error occoured when fetching high quality source: %r" % e)
url = track.location
is_hq = False
else:
url = track.location
print("Listening to: %s by %s from album %s%s #%s" %
(track.title, track.artist, track.album_name, " [HQ]" if is_hq else "",
track.song_id))
try:
info.add_stat(state, info.STAT_BEGIN, track.song_id)
except Exception as e:
print("WARNING: error occoured when reporting stat: %r" % e)
# os.system("mplayer -prefer-ipv4 -quiet -cache 10240 -cache-min 10 %s" % url)
os.system("mpv -audio-samplerate=96000 --really-quiet -cache 10240 %s" % url)
try:
info.add_stat(state, info.STAT_END, track.song_id)
except Exception as e:
print("WARNING: error occoured when reporting stat: %r" % e)
def play_guessed_list(state):
guessed_list = playlist.get_guess_list(state)
guessed_list.visit_player(state)
while True:
if state['random']:
random.shuffle(guessed_list.tracks)
for track in guessed_list.tracks:
play_track(state, track)
try:
info.record_play(state, track.song_id, "guess", info.is_vip(state), None)
except:
logger.exception("fail to record")
time.sleep(1)
def play_radio(state, radio_id):
radio.visit_radio(state, radio_id)
while True:
try:
tracks = radio.get_radio_list(state, state['radio_type'], state['radio_id'])
except:
logger.exception("fail to get list of songs")
time.sleep(ERROR_WAIT)
continue
if state['random']:
random.shuffle(tracks)
for track in tracks:
play_track(state, track)
# type 10 -> play from radio
try:
info.record_play(state, track.song_id, None, info.is_vip(state), "10")
except:
logger.exception("fail to record")
time.sleep(1)
def select_radio_station(state):
fav_radio_page = 1
sel_radio = None
while True:
try:
print("Favourite radio stations, page %d:" % fav_radio_page)
fav_radios = radio.get_fav_radio(state, fav_radio_page)
if len(fav_radios) == 0:
raise Exception("no station available")
except Exception as e:
if fav_radio_page == 1:
print("can't retrieve list of favourite radio stations: %r" % e)
sys.exit(1)
else:
fav_radio_page -= 1
continue
idx = 1
unescaper = HTMLParser.HTMLParser()
for fav_radio in fav_radios:
print("Radio %d: %s fav by %s people" %
(idx, unescaper.unescape(fav_radio['radio_name']),
fav_radio['fav_count']))
print(" %s" % (unescaper.unescape(fav_radio['description'])))
idx += 1
sel = raw_input("Select radio station [1-%d], [n] for next page, " +
"[p] for prev page, [g] for guessed playlist, [r] for random:" %
(len(fav_radios)))
state['random'] = False
if 'r' in sel:
state['random'] = True
sel.replace('r', '')
if sel.isdigit():
sel = int(sel)
if sel < 1 or sel > len(fav_radios):
print("invalid selection: out of range")
continue
sel_radio = fav_radios[sel - 1]
break
else:
if sel[0].lower() == 'n':
fav_radio_page += 1
elif sel[0].lower() == 'p':
if fav_radio_page == 1:
print("invalid operation: at first page")
continue
fav_radio_page -= 1
elif sel[0].lower() == 'g':
return 'g'
else:
print("invalid selection")
print("Listening to radio %s (%s)" % (sel_radio['radio_id'], sel_radio['object_id']))
return sel_radio['radio_id']
def authenticate(state):
if not info.authenticated(state):
username = raw_input("username: ")
import getpass
password = getpass.getpass("password: ")
login.login_console(state, username, password)
if __name__ == '__main__':
logging.basicConfig(level=logging.WARN)
state = init.init()
authenticate(state)
radio_id = select_radio_station(state)
if radio_id == 'g':
play_guessed_list(state)
else:
play_radio(state, radio_id)