lolamontes69

Python / Lola's Track Recommender (uses Last-fm API)

May 26th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.37 KB | None | 0 0
  1. import os as os
  2. import urllib
  3. import json                        # we are after all importing json
  4. from time import sleep
  5.  
  6. def user_getLovedTracks(username):
  7.     global songdic, api_key
  8.     urlstart = "http://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&user="
  9.     urllimit = "&limit="
  10.     urlpage = "&page="
  11.     urlend = "&api_key="+api_key+"&format=json"
  12.     url = urlstart
  13.  
  14.     def download_it(url):
  15.         dic = {}
  16.         conn = urllib.urlopen(url)
  17.         for line in conn:              # comes as a unicode string
  18.             dic = json.loads(line)     # load json into dic
  19.         try:
  20.             for a in range(len(dic['lovedtracks']['track'])):
  21.                 try:
  22.                     song = dic['lovedtracks']['track'][a]['name']
  23.                     band = dic['lovedtracks']['track'][a]['artist']['name']
  24.                     if band not in songdic:
  25.                         songdic[band] = {}
  26.                         songdic[band]['song'] = [song]
  27.                         songdic[band]['count'] = 1
  28.                     else:
  29.                         songdic[band]['song'].append(song)
  30.                         songdic[band]['count'] = songdic[band]['count'] + 1
  31.                 except: pass
  32.         except: pass
  33.  
  34.     url += username
  35.     url += urlend
  36.     download_it(url)
  37.  
  38.  
  39. def artist_gettopfans():
  40.     ch = """
  41. ###############################################################
  42. #      artist.getTopFans                                      #
  43. #                                                             #
  44. ###############################################################
  45. Enter name of Artist : """
  46.  
  47.     global api_key
  48.     urlstart = "http://ws.audioscrobbler.com/2.0/?method=artist.gettopfans&artist="
  49.     urlend = "&autocorrect=1&api_key="+api_key+"&format=json"
  50.     url = urlstart
  51.  
  52.     def download_it(url):
  53.         dic = {}
  54.         conn = urllib.urlopen(url)
  55.         print "\nProcessing recommendations from..."
  56.         for line in conn:              # comes as a unicode string
  57.             dic = json.loads(line)     # load json into dic
  58.             count = 0
  59.             try:
  60.                 for f in range(len(dic['topfans']['user'])):
  61.                     if count == 10: break
  62.                     print dic['topfans']['user'][f]['name']
  63.                     username = dic['topfans']['user'][f]['name']
  64.                     user_getLovedTracks(username)
  65.                     sleep(0.7)
  66.                     count += 1
  67.                 return int(1)
  68.             except: return int(0)
  69.     os.system('clear')
  70.     artist = raw_input(ch)
  71.     url += artist
  72.     url += urlend
  73.     frog = download_it(url)
  74.     return int(frog)
  75.  
  76. def main(many):
  77.     global songdic
  78.     bruce = int(artist_gettopfans())
  79.     while bruce < int(many):
  80.         sheila = int(artist_gettopfans())
  81.         bruce += sheila
  82.     hist = {}
  83.     for a in songdic:
  84.             if songdic[a]['count'] not in hist:
  85.                 hist[songdic[a]['count']] = [a]
  86.             else:
  87.                 hist[songdic[a]['count']].append(a)
  88.     histkeys = hist.keys()
  89.     histkeys.sort()
  90.     histkeys.reverse()
  91.     os.system('clear')
  92.     print "----------------------------------------"
  93.     print "Try listening to..."
  94.     sleep(1)
  95.     ax = 0   # artist
  96.     tr = 0   # track
  97.     print hist[histkeys[ax]][tr],"---",'"',songdic[hist[histkeys[ax]][tr]]['song'][0],'"'
  98.     print "----------------------------------------"
  99.     choosea = 0
  100.     while choosea != "q":
  101.         print "Enter 1 for another track by this artist"
  102.         print "Enter 2 for a track from another artist"
  103.         print "Enter q to quit"
  104.         choosea = str(raw_input('> '))
  105.         os.system('clear')
  106.         if choosea == "1":
  107.             try:
  108.                 tr += 1
  109.                 print "----------------------------------------"
  110.                 print hist[histkeys[ax]][0],"---",'"',songdic[hist[histkeys[ax]][0]]['song'][tr],'"'
  111.                 print "----------------------------------------"
  112.             except:
  113.                 print "----------------------------------------"
  114.                 print "No more recommendations for this artist"
  115.                 print "----------------------------------------"
  116.         elif choosea == "2":
  117.             try:
  118.                 ax += 1
  119.                 tr = 0
  120.                 print "----------------------------------------"
  121.                 print hist[histkeys[ax]][0],"---",'"',songdic[hist[histkeys[ax]][0]]['song'][tr],'"'
  122.                 print "----------------------------------------"
  123.             except:
  124.                 os.system('clear')
  125.                 print "----------------------------------------"
  126.                 print "No more recommendations"
  127.                 print "----------------------------------------"
  128.                 choosea = "q"
  129.                 d = raw_input('Press <enter> to continue')
  130.  
  131. lola = """
  132. ########################################################################
  133. #    _          _        __   ___  _      _____               _        #
  134. #   | |    ___ | | __ _ / /_ / _ \( )___ |_   _| __ __ _  ___| | __    #
  135. #   | |   / _ \| |/ _  |  _ \ (_) |/ __|   | || '__/ _  |/ __| |/ /    #
  136. #   | |__| (_) | | (_| | (_) \__, |\__ \  | || | | (_| | (__|   <     #
  137. #  _|_____\___/|_|\__,_|\___/  /_/ |___/   |_||_|  \__,_|\___|_|\_\   #
  138. # |  _ \ ___  ___ ___  _ __ ___  _ __ ___   ___ _ __   __| | ___ _ __  #
  139. # | |_) / _ \/ __/ _ \| '_   _ \| '_   _ \ / _ \ '_ \ / _  |/ _ \ '__| #
  140. # |  _ <  __/ (_| (_) | | | | | | | | | | |  __/ | | | (_| |  __/ |    #
  141. # |_| \_\___|\___\___/|_| |_| |_|_| |_| |_|\___|_| |_|\__,_|\___|_|    #
  142. #                                                                      #
  143. # This application recommends a track for you to listen to based upon  #
  144. # the listening habits of Last-fm users. The users chosen will be fans #
  145. # of bands that you enter. Firstly you will input the number of bands  #
  146. # you are going to enter, then the names of the bands. The rest is     #
  147. # pretty easy to follow.                                               #
  148. ########################################################################
  149. """
  150. api_key = str(raw_input('please enter your Last-fm api_key >'))
  151.  
  152. many = "100000000"
  153. while (many != "q") or (many != "Q"):
  154.     os.system('clear')
  155.     songdic = {}
  156.     print lola
  157.     many = str(raw_input('Enter number of bands or enter q to quit > '))
  158.     if many != "q":
  159.         main(many)
  160.     else: break
Advertisement
Add Comment
Please, Sign In to add comment