Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os as os
- import urllib
- import json # we are after all importing json
- from time import sleep
- def user_getLovedTracks(username):
- global songdic, api_key
- urlstart = "http://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&user="
- urllimit = "&limit="
- urlpage = "&page="
- urlend = "&api_key="+api_key+"&format=json"
- url = urlstart
- def download_it(url):
- dic = {}
- conn = urllib.urlopen(url)
- for line in conn: # comes as a unicode string
- dic = json.loads(line) # load json into dic
- try:
- for a in range(len(dic['lovedtracks']['track'])):
- try:
- song = dic['lovedtracks']['track'][a]['name']
- band = dic['lovedtracks']['track'][a]['artist']['name']
- if band not in songdic:
- songdic[band] = {}
- songdic[band]['song'] = [song]
- songdic[band]['count'] = 1
- else:
- songdic[band]['song'].append(song)
- songdic[band]['count'] = songdic[band]['count'] + 1
- except: pass
- except: pass
- url += username
- url += urlend
- download_it(url)
- def artist_gettopfans():
- ch = """
- ###############################################################
- # artist.getTopFans #
- # #
- ###############################################################
- Enter name of Artist : """
- global api_key
- urlstart = "http://ws.audioscrobbler.com/2.0/?method=artist.gettopfans&artist="
- urlend = "&autocorrect=1&api_key="+api_key+"&format=json"
- url = urlstart
- def download_it(url):
- dic = {}
- conn = urllib.urlopen(url)
- print "\nProcessing recommendations from..."
- for line in conn: # comes as a unicode string
- dic = json.loads(line) # load json into dic
- count = 0
- try:
- for f in range(len(dic['topfans']['user'])):
- if count == 10: break
- print dic['topfans']['user'][f]['name']
- username = dic['topfans']['user'][f]['name']
- user_getLovedTracks(username)
- sleep(0.7)
- count += 1
- return int(1)
- except: return int(0)
- os.system('clear')
- artist = raw_input(ch)
- url += artist
- url += urlend
- frog = download_it(url)
- return int(frog)
- def main(many):
- global songdic
- bruce = int(artist_gettopfans())
- while bruce < int(many):
- sheila = int(artist_gettopfans())
- bruce += sheila
- hist = {}
- for a in songdic:
- if songdic[a]['count'] not in hist:
- hist[songdic[a]['count']] = [a]
- else:
- hist[songdic[a]['count']].append(a)
- histkeys = hist.keys()
- histkeys.sort()
- histkeys.reverse()
- os.system('clear')
- print "----------------------------------------"
- print "Try listening to..."
- sleep(1)
- ax = 0 # artist
- tr = 0 # track
- print hist[histkeys[ax]][tr],"---",'"',songdic[hist[histkeys[ax]][tr]]['song'][0],'"'
- print "----------------------------------------"
- choosea = 0
- while choosea != "q":
- print "Enter 1 for another track by this artist"
- print "Enter 2 for a track from another artist"
- print "Enter q to quit"
- choosea = str(raw_input('> '))
- os.system('clear')
- if choosea == "1":
- try:
- tr += 1
- print "----------------------------------------"
- print hist[histkeys[ax]][0],"---",'"',songdic[hist[histkeys[ax]][0]]['song'][tr],'"'
- print "----------------------------------------"
- except:
- print "----------------------------------------"
- print "No more recommendations for this artist"
- print "----------------------------------------"
- elif choosea == "2":
- try:
- ax += 1
- tr = 0
- print "----------------------------------------"
- print hist[histkeys[ax]][0],"---",'"',songdic[hist[histkeys[ax]][0]]['song'][tr],'"'
- print "----------------------------------------"
- except:
- os.system('clear')
- print "----------------------------------------"
- print "No more recommendations"
- print "----------------------------------------"
- choosea = "q"
- d = raw_input('Press <enter> to continue')
- lola = """
- ########################################################################
- # _ _ __ ___ _ _____ _ #
- # | | ___ | | __ _ / /_ / _ \( )___ |_ _| __ __ _ ___| | __ #
- # | | / _ \| |/ _ | _ \ (_) |/ __| | || '__/ _ |/ __| |/ / #
- # | |__| (_) | | (_| | (_) \__, |\__ \ | || | | (_| | (__| < #
- # _|_____\___/|_|\__,_|\___/ /_/ |___/ |_||_| \__,_|\___|_|\_\ #
- # | _ \ ___ ___ ___ _ __ ___ _ __ ___ ___ _ __ __| | ___ _ __ #
- # | |_) / _ \/ __/ _ \| '_ _ \| '_ _ \ / _ \ '_ \ / _ |/ _ \ '__| #
- # | _ < __/ (_| (_) | | | | | | | | | | | __/ | | | (_| | __/ | #
- # |_| \_\___|\___\___/|_| |_| |_|_| |_| |_|\___|_| |_|\__,_|\___|_| #
- # #
- # This application recommends a track for you to listen to based upon #
- # the listening habits of Last-fm users. The users chosen will be fans #
- # of bands that you enter. Firstly you will input the number of bands #
- # you are going to enter, then the names of the bands. The rest is #
- # pretty easy to follow. #
- ########################################################################
- """
- api_key = str(raw_input('please enter your Last-fm api_key >'))
- many = "100000000"
- while (many != "q") or (many != "Q"):
- os.system('clear')
- songdic = {}
- print lola
- many = str(raw_input('Enter number of bands or enter q to quit > '))
- if many != "q":
- main(many)
- else: break
Advertisement
Add Comment
Please, Sign In to add comment