lolamontes69

Python/ An interface to the last-fm API

May 22nd, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.87 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.  
  7.  
  8. def artist_getinfo(api_key):
  9.     urlstart = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist="
  10.     urlend = "&autocorrect=1&api_key="+api_key+"&format=json"
  11.     url = urlstart
  12.     ch1 = """
  13. ###############################################################
  14. #      method - artist.getInfo                                #
  15. #                                                             #
  16. ###############################################################
  17. Enter name of Artist : """
  18.  
  19.     def wrap(text, width):
  20.         """
  21.        A word-wrap function that preserves existing line breaks
  22.        and most spaces in the text. Expects that existing line
  23.        breaks are posix newlines (\n).
  24.        usage: print(wrap(msg,40))
  25.        """
  26.         return reduce(lambda line, word, width=width: '%s%s%s' %
  27.                       (line,' \n'[(len(line)-line.rfind('\n')-1 + len(word.split('\n',1)[0]) >= width)],word),text.split(' '))
  28.  
  29.     def download_it(url, loca):
  30.         print "loading..."
  31.         dic = {}
  32.         conn = urllib.urlopen(url)
  33.         try:
  34.             os.system('clear')
  35.             for line in conn:              # comes as a unicode string
  36.                 dic = json.loads(line)     # load json into dic
  37.             print "###############################################################"
  38.             print "Artist info for",loca
  39.             print "###############################################################"
  40.             print "about..."
  41.             content = dic['artist']['bio']['content']
  42.             print wrap(content.strip(),80)
  43.             print "###############################################################"
  44.             print "tags...\n"
  45.             for f in range(len(dic['artist']['tags']['tag'])):
  46.                 print dic['artist']['tags']['tag'][f]['name']
  47.             print "###############################################################"
  48.             print "similar artists...\n"
  49.             for f1 in range(len(dic['artist']['similar']['artist'])):
  50.                 print dic['artist']['similar']['artist'][f1]['name']
  51.             print "###############################################################"
  52.             print "Listeners: ",dic['artist']['stats']['listeners']
  53.             print "Playcount: ",dic['artist']['stats']['playcount']
  54.         except: print "No artist info found"
  55.  
  56.     os.system('clear')
  57.     loca = raw_input(ch1)
  58.     url += loca
  59.     url += urlend
  60.     download_it(url, loca)
  61.     ex = raw_input('\nPress <ENTER> to continue')
  62.  
  63. def artist_getsimilar(api_key):
  64.     urlstart = "http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist="
  65.     urllimit = "&limit="
  66.     urlend = "&autocorrect=1&api_key="+api_key+"&format=json"
  67.     url = urlstart
  68.     ch2 = """
  69. ###############################################################
  70. #      method - artist.getSimilar                             #
  71. #                                                             #
  72. #  1 : Limit the number of similar artists returned           #
  73. #  0 : Fetch the JSON                                         #
  74. #                                                             #
  75. ###############################################################
  76. first Enter name of Artist : """
  77.  
  78.     def download_it(url, loca):
  79.         print "loading..."
  80.         dic = {}
  81.         conn = urllib.urlopen(url)
  82.         for line in conn:              # comes as a unicode string
  83.             dic = json.loads(line)     # load json into dic
  84.         try:
  85.             os.system('clear')
  86.             print "###############################################################"
  87.             print "Similar Artists to",loca
  88.             print "###############################################################"
  89.             count = 0
  90.             for f in range(len(dic['similarartists']['artist'])):
  91.                 print dic['similarartists']['artist'][f]['name']
  92.                 count += 1
  93.                 if count == 10: break
  94.         except: print "No similar artists found"
  95.  
  96.     os.system('clear')
  97.     loca = raw_input(ch2)
  98.     url += loca
  99.     loopy = 0
  100.     while loopy == 0:
  101.         print "\nChoose an option from the menu above"
  102.         choosea = str(raw_input('> '))
  103.         if choosea == '0':
  104.             loopy = 1
  105.         elif choosea == '1':
  106.             limi = str(raw_input('Enter number of sim artists to get: '))
  107.             url += urllimit + limi
  108.     url += urlend
  109.     pnumber = 1
  110.     download_it(url, loca)
  111.     ex = raw_input('\nPress <ENTER> to continue')
  112.  
  113. def artist_gettoptags(api_key):
  114.     urlstart = "http://ws.audioscrobbler.com/2.0/?method=artist.gettoptags&artist="
  115.     urlend = "&autocorrect=1&api_key="+api_key+"&format=json"
  116.     url = urlstart
  117.     ch3 = """
  118. ###############################################################
  119. #      method - artist.getTopTags                             #
  120. #                                                             #
  121. ###############################################################
  122. first Enter name of Artist : """
  123.  
  124.     def download_it(url, loca):
  125.         print "loading..."
  126.         dic = {}
  127.         conn = urllib.urlopen(url)
  128.         for line in conn:              # comes as a unicode string
  129.             dic = json.loads(line)     # load json into dic
  130.         try:
  131.             os.system('clear')
  132.             print "###############################################################"
  133.             print "Top tags for",loca
  134.             print "###############################################################"
  135.             count = 0
  136.             for f in range(len(dic['toptags']['tag'])):
  137.                 print dic['toptags']['tag'][f]['name'],dic['toptags']['tag'][f]['count']
  138.                 count += 1
  139.                 if count == 10: break
  140.         except: print "No tags found"
  141.  
  142.     os.system('clear')
  143.     loca = raw_input(ch3)
  144.     url += loca
  145.     url += urlend
  146.     download_it(url, loca)
  147.     ex = raw_input('\nPress <ENTER> to continue')
  148.  
  149. def library_getartists(api_key):
  150.     urlstart = "http://ws.audioscrobbler.com/2.0/?method=library.getartists"
  151.     urllimit = "&limit="
  152.     urlpage = "&page="
  153.     urlend = "&api_key="+api_key+"&format=json&user="
  154.     url = urlstart
  155.     ch4 = """
  156. #################################################################
  157. #      method - library.getArtists                              #
  158. #                                                               #
  159. #  1 : The number of results to fetch per page. Defaults to 50. #
  160. #  2 : Specify number of pages to fetch: Default = 1.           #
  161. #  0 : Fetch the JSON                                           #
  162. #                                                               #
  163. #################################################################
  164. first Enter the users name: """
  165.  
  166.     def download_it(url, username):
  167.         print "loading..."
  168.         dic = {}
  169.         conn = urllib.urlopen(url)
  170.         for line in conn:              # comes as a unicode string
  171.             dic = json.loads(line)     # load json into dic
  172.         try:
  173.             os.system('clear')
  174.             print "###############################################################"
  175.             print "Artists from the library of",username
  176.             print "###############################################################"
  177.             count = 0
  178.             for f in range(len(dic['artists']['artist'])):
  179.                 print dic['artists']['artist'][f]['name'],dic['artists']['artist'][f]['playcount']
  180.                 count += 1
  181.                 if count == 10: break
  182.         except: print "No artists found"
  183.  
  184.     os.system('clear')
  185.     username = raw_input(ch4)
  186.     urlend += username
  187.     pages = 0
  188.     loopy = 0
  189.     while loopy == 0:
  190.         print "\nChoose an option from the menu above"
  191.         choosea = str(raw_input('> '))
  192.         if choosea == '0':
  193.             loopy = 1
  194.         elif choosea == '1':
  195.             limi = str(raw_input('Enter results per page: '))
  196.             url += urllimit + limi
  197.         elif choosea == '2':
  198.             pages = int(raw_input('Enter number of pages: '))
  199.  
  200.     if pages > 1:
  201.         for a in range(pages):
  202.             url1 = url
  203.             url1 += urlpage + str(a+1)
  204.             url1 += urlend
  205.             download_it(url1, username)
  206.             sleep(1)
  207.     else:
  208.         url += urlend
  209.         download_it(url, username)
  210.     ex = raw_input('\nPress <ENTER> to continue')
  211.  
  212. def tag_getsimilar(api_key):
  213.     urlstart = "http://ws.audioscrobbler.com/2.0/?method=tag.getsimilar&tag="
  214.     urlend = "&api_key="+api_key+"&format=json"
  215.     url = urlstart
  216.     ch5 = """
  217. ###############################################################
  218. #      method - tag.getSimilar                                #
  219. #                                                             #
  220. ###############################################################
  221. Enter tag : """
  222.  
  223.     def download_it(url, loca):
  224.         print "loading..."
  225.         dic = {}
  226.         conn = urllib.urlopen(url)
  227.         for line in conn:              # comes as a unicode string
  228.             dic = json.loads(line)     # load json into dic
  229.         try:
  230.             os.system('clear')
  231.             print "###############################################################"
  232.             print "Tags similar to",loca
  233.             print "###############################################################"
  234.             count = 0
  235.             for f in range(len(dic['similartags']['tag'])):
  236.                 print dic['similartags']['tag'][f]['name']
  237.                 count += 1
  238.                 if count == 10: break
  239.         except: print "No tags found"
  240.  
  241.     os.system('clear')
  242.     loca = raw_input(ch5)
  243.     url += loca
  244.     url += urlend
  245.     download_it(url, loca)
  246.     ex = raw_input('\nPress <ENTER> to continue')
  247.  
  248. def user_getfriends(api_key):
  249.     urlstart = "http://ws.audioscrobbler.com/2.0/?method=user.getfriends&user="
  250.     urllimit = "&limit="
  251.     urlpage = "&page="
  252.     urlend = "&api_key="+api_key+"&format=json"
  253.     url = urlstart
  254.     ch6 = """
  255. #################################################################
  256. #      method - user.getFriends                                 #
  257. #                                                               #
  258. #  1 : The number of results to fetch per page. Defaults to 50. #
  259. #  2 : Specify number of pages to fetch: Default = 1.           #
  260. #  0 : Fetch the JSON                                           #
  261. #                                                               #
  262. #################################################################
  263. first Enter the users name: """
  264.  
  265.     def download_it(url, username):
  266.         print "loading..."
  267.         dic = {}
  268.         conn = urllib.urlopen(url)
  269.         for line in conn:              # comes as a unicode string
  270.             dic = json.loads(line)     # load json into dic
  271.         try:
  272.             os.system('clear')
  273.             print "###############################################################"
  274.             print "Friends of",username
  275.             print "###############################################################"
  276.             count = 0
  277.             for f in range(len(dic['friends']['user'])):
  278.                 print dic['friends']['user'][f]['name'],dic['friends']['user'][f]['realname'],dic['friends']['user'][f]['age'],dic['friends']['user'][f]['country']
  279.                 count += 1
  280.                 if count == 10: break
  281.         except: print "No friends found"
  282.  
  283.     os.system('clear')
  284.     username = raw_input(ch6)
  285.     url += username
  286.     pages = 0
  287.     loopy = 0
  288.     while loopy == 0:
  289.         print "\nChoose an option from the menu above"
  290.         choosea = str(raw_input('> '))
  291.         if choosea == '0':
  292.             loopy = 1
  293.         elif choosea == '1':
  294.             limi = str(raw_input('Enter results per page: '))
  295.             url += urllimit + limi
  296.         elif choosea == '2':
  297.             pages = int(raw_input('Enter number of pages: '))
  298.  
  299.     if pages > 1:
  300.         for a in range(pages):
  301.             url1 = url
  302.             url1 += urlpage + str(a+1)
  303.             url1 += urlend
  304.             download_it(url1, username)
  305.             sleep(1)
  306.     else:
  307.         url += urlend
  308.         download_it(url, username)
  309.     ex = raw_input('\nPress <ENTER> to continue')
  310.  
  311. def user_getlovedtracks(api_key):
  312.     urlstart = "http://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&user="
  313.     urllimit = "&limit="
  314.     urlpage = "&page="
  315.     urlend = "&api_key="+api_key+"&format=json"
  316.     url = urlstart
  317.     ch7 = """
  318. #################################################################
  319. #      method - user.getLovedTracks                             #
  320. #                                                               #
  321. #  1 : The number of results to fetch per page. Defaults to 50. #
  322. #  2 : Specify number of pages to fetch: Default = 1.           #
  323. #  0 : Fetch the JSON                                           #
  324. #                                                               #
  325. #################################################################
  326. first Enter the users name: """
  327.  
  328.     def download_it(url, username):
  329.         print "loading..."
  330.         dic = {}
  331.         conn = urllib.urlopen(url)
  332.         for line in conn:              # comes as a unicode string
  333.             dic = json.loads(line)     # load json into dic
  334.         try:
  335.             os.system('clear')
  336.             print "###############################################################"
  337.             print "Loved tracks of",username
  338.             print "###############################################################"
  339.             count = 0
  340.             for f in range(len(dic['lovedtracks']['track'])):
  341.                 print dic['lovedtracks']['track'][f]['name'],dic['lovedtracks']['track'][f]['artist']['name']
  342.                 count += 1
  343.                 if count == 10: break
  344.         except: print "No loved tracks found"
  345.  
  346.     os.system('clear')
  347.     username = raw_input(ch7)
  348.     url += username
  349.     pages = 0
  350.     loopy = 0
  351.     while loopy == 0:
  352.         print "\nChoose an option from the menu above"
  353.         choosea = str(raw_input('> '))
  354.         if choosea == '0':
  355.             loopy = 1
  356.         elif choosea == '1':
  357.             limi = str(raw_input('Enter results per page: '))
  358.             url += urllimit + limi
  359.         elif choosea == '2':
  360.             pages = int(raw_input('Enter number of pages: '))
  361.  
  362.     if pages > 1:
  363.         for a in range(pages):
  364.             url1 = url
  365.             url1 += urlpage + str(a+1)
  366.             url1 += urlend
  367.             download_it(url1, username)
  368.             sleep(1)
  369.     else:
  370.         url += urlend
  371.         download_it(url, username)
  372.     ex = raw_input('\nPress <ENTER> to continue')
  373.  
  374. def gui(api_key):
  375.     ch = """
  376. ###############################################################
  377. #      options                                                #
  378. #                                                             #
  379. #  1 - artist.getInfo                                         #
  380. #  2 - artist.getSimilar                                      #
  381. #  3 - artist.getTopTags                                      #
  382. #  4 - library.getArtists                                     #
  383. #  5 - tag.getSimilar                                         #
  384. #  6 - user.getFriends                                        #
  385. #  7 - user.getLovedTracks                                    #
  386. #  0 - exit                                                   #
  387. ###############################################################
  388. Choose an option from the menu above: """
  389.     loopy = 0
  390.     while loopy == 0:
  391.         os.system('clear')
  392.         choosea = str(raw_input(ch))
  393.         if choosea == '0':
  394.             loopy = 1
  395.         elif choosea == '1':
  396.             artist_getinfo(api_key)
  397.         elif choosea == '2':
  398.             artist_getsimilar(api_key)
  399.         elif choosea == '3':
  400.             artist_gettoptags(api_key)
  401.         elif choosea == '4':
  402.             library_getartists(api_key)
  403.         elif choosea == '5':
  404.             tag_getsimilar(api_key)
  405.         elif choosea == '6':
  406.             user_getfriends(api_key)
  407.         elif choosea == '7':
  408.             user_getlovedtracks(api_key)
  409.         else:
  410.             print "#invalid option#"
  411.     os.system('clear')
  412.  
  413. def hello():
  414.     os.system('clear')
  415.     ch0 = """
  416. #####################################################
  417. #      _           _         __                     #
  418. #     | | ___  ___| |_      / _|_ __ ___ -69        #
  419. #     | |/ _ '/ __| __|____| |_|  _ ' _ \          #
  420. #     | | (_| \__ \ |_|____|  _| | | | | |          #
  421. #     |_|\__,_|___/\__|    |_| |_| |_| |_|          #
  422. #                                                   #
  423. #   An interface to the last-fm API: lolamontes69   #
  424. #                                                   #
  425. #####################################################
  426. Enter your api_key: """
  427.     api_key = str(raw_input(ch0))
  428.     gui(api_key)
  429.  
  430. hello()
Advertisement
Add Comment
Please, Sign In to add comment