-
Notifications
You must be signed in to change notification settings - Fork 0
/
ATGT.py
59 lines (47 loc) · 1.58 KB
/
ATGT.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
from flask import Flask, request, redirect, session, render_template, url_for
from jinja2 import Template
import json
import pylast
import random
app = Flask(__name__)
@app.route("/")
def index():
print "someone connected"
return render_template('index.html')
@app.route("/getartists",methods=['POST'])
def getartists():
#pull the JSON out of the request
jsonlist = []
for key, value in request.form.iteritems():
jsonlist.append(key)
#if user tampered with the list or it was lost along the way
if len(jsonlist) != 1:
return "That's an odd number of JSON lists in your POST"
#get the first (and only) JSON and parse the list
artistsjson = jsonlist[0];
artists = json.loads(artistsjson)
#build the playlist from each artists' top songs
playlist = []
for artist in artists:
for track in getTopTracks(artist):
playlist.append(track)
#randomize playlist
random.shuffle(playlist)
#convert the playlist to JSON and send it
playlistdump = json.dumps(playlist)
return playlistdump
#lastfm vars
api_key = 'YourLastFMApiKey'
api_secret = 'YourLastFMApiSecret'
network = pylast.LastFMNetwork(api_key,api_secret)
def getTopTracks(artist, count=3):
global network
artist = network.get_artist(artist)
tracks = pylast.extract_items(artist.get_top_tracks())
buildlist = []
for track in tracks[:count]:
buildlist.append([str(track.artist),str(track.title),str(track.get_duration())])
return buildlist
if __name__ == "__main__":
app.config['DEBUG'] = False
app.run('0.0.0.0',80)