-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexport.py
138 lines (115 loc) · 4.48 KB
/
export.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
#!/usr/bin/env python3
##
### This script was written by Matthew Davis in July 2017.
# It uses the Pandora library writte by Kevin Mehall <[email protected]> and Christopher Eby <[email protected]>
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
# import requests
import json
import pprint as pp
import pandora
import getpass
# from multiprocessing import Pool
from io import StringIO
class Exporter(object):
def __init__(self):
object.__init__(self)
self.pan = pandora.Pandora()
def login(self, userid, password):
client = pandora.data.client_keys[pandora.data.default_client_id]
print("Connecting ...")
self.pan.connect(client, userid, password)
print("Connected")
def getLikes(self):
print("Getting list of your stations")
stations = self.pan.get_stations()
print("Got list of station names")
print("Looking up thumbs for each station")
def getInfo(s):
print("#", end="", flush=True)
return s.get_info(extended=True)
fullData = [getInfo(s) for s in stations]
# This doesn't work, because something isn't picklable
# with Pool(4) as self.pandora:
# fullData = self.pandora.map(lambda s:s.get_info(extended=True), stations)
print("Got thumbs for each station")
print('Formatting results')
station_base = [s['music'] for s in fullData if 'music' in s]
# likes = {y:[x[y] for x in likes_raw if y in x] for y in ['artists','genres','songs']}
# dislikes = {y:[x[y] for x in dislikes_raw if y in x] for y in ['artists','genres','songs']}
neatData = {
'thumbsUp':{
'artists':[],
'songs':[],
'genres':[]
},
'thumbsDown':{
'artists':[],
'songs':[],
'genres':[]
}
}
for x in station_base:
if 'songs' in x:
songs = [{'name':s['artistName'],'artist':s['artistName']} for s in x['songs']]
neatData['thumbsUp']['songs'].extend(songs)
if 'genres' in x:
genres = [g['genreName'] for g in x['genres']]
neatData['thumbsUp']['genres'].extend(genres)
if 'artists' in x:
artists = [a['artistName'] for a in x['artists']]
neatData['thumbsUp']['artists'].extend(artists)
for direction in ['thumbsUp','thumbsDown']:
for station in fullData:
if 'feedback' in station:
for x in station['feedback'][direction]:
if 'songName' in x:
neatData[direction]['songs'].append({'name':x['songName'],'artist':x['artistName']})
else:
assert('artistName' in x)
neatData[direction]['artists'].append(x['artistName'])
self.neatData = neatData
self.fullData = fullData
def getJson(self, filename):
neatData = self.neatData
fullData = self.fullData
if filename == "full.json":
theData = fullData
elif filename == "neat.json":
theData = neatData
else:
raise Exception("bad json name")
io = StringIO()
json.dump(theData, io)
return io.getvalue()
def save(self, fullFileName='full.json', neatFileName = 'neat.json'):
neatData = self.neatData
fullData = self.fullData
# If you want the final output to be formatted a particular way
# do that here
print('Saving full neatData to file %s' % fullFileName)
with open(fullFileName, 'w') as fp:
json.dump(fullData, fp, indent=3)
print('saved full neatData to %s' % fullFileName)
print("Saving summarised neatData to file %s" % neatFileName)
with open(neatFileName, 'w') as fp:
json.dump(neatData, fp, indent=3)
print('saved summarised data to %s' % neatFileName)
MAIN="__main__"
if __name__ == MAIN:
exporter = Exporter()
user = input("Enter your username (which is probably an email address)\n")
pwd = getpass.getpass()
exporter.login(user, pwd)
exporter.getLikes()
exporter.save()
print('Done')