-
Notifications
You must be signed in to change notification settings - Fork 12
/
pidora.py
174 lines (159 loc) · 6.52 KB
/
pidora.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
173
174
import json as libjson, os, re, requests, subprocess
current_dir = os.path.dirname(os.path.abspath(__file__)) + "/"
def process(command, new = False):
if new:
with open(os.devnull, "w") as fnull: result = subprocess.Popen(command, stdout = fnull, stderr = fnull)
else:
with open(os.devnull, "w") as fnull: result = subprocess.call(command, stdout = fnull, stderr = fnull)
return result
def getSongData(data):
if data["songData"] is not None:
jsonObj = data["songData"]
if jsonObj["title"].find("NPR News") != -1:
jsonObj["isSong"] = False
else:
jsonObj["isSong"] = True
return jsonObj
elif "pianobar" in data:
if data["pianobar"] is not None:
return libjson.loads('{"startup":true, "isSong":false}')
else:
return libjson.loads('{"startup":false, "isSong":false}')
else:
return False
def setSongData(data=dict(), newSongData=dict()):
data['songData'] = newSongData
return True
def writeMsg(msg):
open(current_dir + "msg", "w").write(msg)
def getExplanation(data):
songData = getSongData(data)
regex = re.compile("Track</h2>(.*?)</div>",re.IGNORECASE|re.DOTALL)
html = requests.get(songData["explainURL"]).text
string = html.replace("\t", "").replace("\n", "").replace('<div style="display: none;">', "")
r = regex.search(string)
if r is None:
return "We were unable to get the song's explanation. Sorry about that."
data = r.groups()[0].split("<br>")
traits = data[0:len(data)-1]
if data[len(data)-2].find("many other comedic similarities") == -1:
ending = "many other similarities as identified by the Music Genome Project"
else:
ending = "many other comedic similarities"
traits = data[0:len(data)-2]
return "We're playing this track because it features " + ", ".join(traits) + ", and " + ending +"."
def getStations(index):
listStations = open(current_dir + "stationList").read().split("|")
stationList = dict(index=index)
lo = index*10
if lo > len(listStations):
return dict(error="No stations in that range")
if len(listStations) < lo+10:
hi = len(listStations)
else:
hi = lo+10
stationList["back"] = index-1 if lo > 0 else None
stationList["next"] = index+1 if len(listStations) > hi else None
stations = []
for i in range(lo,hi):
station = listStations[i].split("=")
stations.append(station[1])
stationList["stations"] = stations
return stationList
def Control(command):
commands = dict(pause="p", next="n", love="+", ban="-", tired="t", volumedown="(", volumeup=")")
try:
open(current_dir + "ctl", "w").write(commands[command])
if command == "next":
writeMsg("Skipped")
elif command == "love":
writeMsg("Loved")
elif command == "ban":
writeMsg("Banned")
elif command == "tired":
writeMsg("Tired")
return True
except KeyError:
return False
def ChangeStation(id):
open(current_dir + "ctl", "w").write("s" + str(int(id)) + "\n")
writeMsg("Changed station")
return True
def CreateStation(type, meta):
if type == "quick":
if meta == "song":
open(current_dir + "ctl", "w").write("vs\n")
writeMsg("Station created")
return True
elif meta == "artist":
open(current_dir + "ctl", "w").write("va\n")
writeMsg("Station created")
return True
else:
return False
else:
return False
def api(data, json=None):
if json is None or json == "":
replyJSON = libjson.dumps(dict(method="NoJSON", id=None, response="bad"), indent=2)
json = libjson.loads(json)
if json["method"] == "GetSongInfo":
if os.path.exists(current_dir + "msg"):
msg = open(current_dir + "msg").read()
os.remove(current_dir + "msg")
else:
msg = None
songData = getSongData(data)
replyJSON = libjson.dumps(dict(method="GetSongInfo", msg=msg, id=json["id"], song=songData), indent=2)
elif json["method"] == "SetSongInfo":
if 'songData' in json:
if setSongData(data, json["songData"]):
replyJSON = libjson.dumps(dict(method="SetSongInfo", id=json["id"], response="ok"))
else:
replyJSON = libjson.dumps(dict(method="SetSongInfo", id=json["id"], response="bad"))
elif json["method"] == "GetExplanation":
replyJSON = libjson.dumps(dict(method="GetExplanation", id=json["id"], explanation=getExplanation(data)), indent=2)
elif json["method"] == "GetStationData":
replyJSON = libjson.dumps(dict(method="GetStationList", id=json["id"], stationData=getStations(json["index"])), indent=2)
elif json["method"] == "Control":
if Control(json["command"]):
replyJSON = libjson.dumps(dict(method="Control", id=json["id"], command=json["command"], response="ok"), indent=2)
else:
replyJSON = libjson.dumps(dict(method="Control", id=json["id"], command=json["command"], response="bad"), indent=2)
elif json["method"] == "CreateStation":
replyJSON = libjson.dumps(dict(method="CreateStation", id=json["id"], response="disabled - See issue #23"), indent=2) # see issue#23
if json["quick"]:
if CreateStation("quick", json["quick"]):
replyJSON = libjson.dumps(dict(method="CreateStation", id=json["id"], quick=json["quick"], response="ok"), indent=2)
else:
replyJSON = libjson.dumps(dict(method="CreateStation", id=json["id"], quick=json["quick"], response="bad"), indent=2)
else:
replyJSON = libjson.dumps(dict(method="CreateStation", id=json["id"], response="bad"), indent=2)
elif json["method"] == "ChangeStation":
if json["stationID"]:
if ChangeStation(json["stationID"]):
replyJSON = libjson.dumps(dict(method="ChangeStation", id=json["id"], stationID=json["stationID"], response="ok"), indent=2)
else:
replyJSON = libjson.dumps(dict(method="ChangeStation", id=json["id"], stationID=json["stationID"], response="bad"), indent=2)
else:
replyJSON = libjson.dumps(dict(method="ChangeStation", id=json["id"], response="bad"), indent=2)
elif json["method"] == "Pianobar.Start":
if(data["pianobar"] is None):
data["pianobar"] = process(["pianobar"], True)
replyJSON = json=libjson.dumps(dict(method="Pianobar.Start", id=json["id"], response="ok"), indent=2)
else:
replyJSON = json=libjson.dumps(dict(method="Pianobar.Start", id=json["id"], response="bad"), indent=2)
elif json["method"] == "Pianobar.Quit":
if(data["pianobar"]):
open(current_dir + "ctl", "w").write("q")
writeMsg("Shutdown")
os.remove(current_dir + "stationList")
data["songData"] = None
data["pianobar"].wait()
data["pianobar"] = None
replyJSON = libjson.dumps(dict(method="Pianobar.Quit", id=json["id"], response="ok"), indent=2)
else:
replyJSON = libjson.dumps(dict(method="Pianobar.Quit", id=json["id"], response="bad"), indent=2)
else:
replyJSON = libjson.dumps(dict(method="NoValidMethod", id=json["id"], response="bad"), indent=2)
return dict(data=data, json=replyJSON)