lolamontes69

Python/ Lola's Gig Guide (uses the last-fm API)

May 23rd, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.03 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. ques1 = """
  7. ######################################################
  8. #      Question one:                                 #
  9. #                                                    #
  10. #  Name your favourite band.                         #
  11. #                                                    #
  12. ######################################################
  13. Enter bandname: """
  14.  
  15. ques2 = """
  16. ######################################################
  17. #      Question two:                                 #
  18. #                                                    #
  19. #  Name another band.                                #
  20. #                                                    #
  21. ######################################################
  22. Enter bandname: """
  23.  
  24. ques3 = """
  25. ######################################################
  26. #      Question three:                               #
  27. #                                                    #
  28. #  Name your current location.                       #
  29. #                                                    #
  30. ######################################################
  31. Enter location: """
  32.  
  33. ques4 = """
  34. ######################################################
  35. #      Question four:                                #
  36. #                                                    #
  37. #  How far are you willing to travel?                #
  38. #                                                    #
  39. ######################################################
  40. Enter distance in km: """
  41.  
  42. ques5 = """
  43. ######################################################
  44. #      Question five:                                #
  45. #                                                    #
  46. #  How many days from today should I search within?  #
  47. #                                                    #
  48. ######################################################
  49. Enter number of days: """
  50.  
  51. def _wrap_with(code):
  52.     def inner(text, bold=False):
  53.         c = code
  54.         if bold:
  55.             c = "1;%s" % c
  56.         return "\033[%sm%s\033[0m" % (c, text)
  57.     return inner
  58.  
  59. def artist_gettoptags(api_key, artist):
  60.     urlstart = "http://ws.audioscrobbler.com/2.0/?method=artist.gettoptags&artist="
  61.     urlend = "&autocorrect=1&api_key="+api_key+"&format=json"
  62.     url = urlstart
  63.  
  64.     def download_it(url):
  65.         blue = _wrap_with('34')
  66.         print blue("loading...")
  67.         list1 = []
  68.         dic = {}
  69.         conn = urllib.urlopen(url)
  70.         for line in conn:              # comes as a unicode string
  71.             dic = json.loads(line)     # load json into dic
  72.         try:
  73.             os.system('clear')
  74.             count = 0
  75.             for f in range(len(dic['toptags']['tag'])):
  76.                 list1.append(dic['toptags']['tag'][f]['name'])
  77.                 count += 1
  78.                 if count == 10: break
  79.         except: pass
  80.         return list1
  81.  
  82.     os.system('clear')
  83.     url += artist
  84.     url += urlend
  85.     list1 = download_it(url)
  86.     return list1
  87.  
  88.  
  89. def geo_getEvents(api_key, location, distance, page1, arttags):
  90.     urlstart = "http://ws.audioscrobbler.com/2.0/?method=geo.getEvents&location="+location+"&distance="+distance+"&page="
  91.     urlend = "&api_key="+api_key+"&format=json"
  92.     url = urlstart
  93.     if page1 == 0: page1 = 1
  94.     pages = int(page1)
  95.     def wrap(text, width):
  96.         return reduce(lambda line, word, width=width: '%s%s%s' %
  97.                       (line,' \n'[(len(line)-line.rfind('\n')-1 + len(word.split('\n',1)[0]) >= width)],word),text.split(' '))
  98.  
  99.     def print_results(dic):
  100.         white = _wrap_with('37')
  101.         cyan = _wrap_with('36')
  102.         blue = _wrap_with('34')
  103.         print cyan("Startdate :"),white(dic['startDate'])
  104.         print ""
  105.         print cyan("Event name:"),white(dic['title'])
  106.         print cyan("Headliner :"),white(dic['artists']['headliner'])
  107.         print cyan("with       "),white(wrap((str(dic['artists']['artist'])), 80))
  108.         print ""
  109.         print cyan("At        :"),white(dic['venue']['name'])
  110.         print cyan("Tel No    :"),white(dic['venue']['phonenumber'])
  111.         print cyan("Address   :"),white(dic['venue']['location']['street'])
  112.         print "           ",white(dic['venue']['location']['city'])
  113.         print "           ",white(dic['venue']['location']['postalcode'])
  114.         print "\n------------------------------------------------\n"
  115.         d = raw_input(blue('<enter> to continue'))
  116.         print "\n------------------------------------------------\n"
  117.  
  118.     def download_it(url):
  119.         blue = _wrap_with('34')
  120.         print blue("loading...")
  121.         dic = {}
  122.         conn = urllib.urlopen(url)
  123.         for line in conn:              # comes as a unicode string
  124.             dic = json.loads(line)     # load json into dic
  125.         try:
  126.             count = 0
  127.             for f in range(len(dic['events']['event'])):
  128.                 listA = []
  129.                 try:
  130.                     listA = dic['events']['event'][f]['tags']['tag']
  131.                     for a in listA:
  132.                         if a in arttags:
  133.                             print_results(dic['events']['event'][f])
  134.                             break
  135.                 except: pass
  136.         except: print "No more gigs found"
  137.  
  138.     os.system('clear')
  139.     for a in range(pages):
  140.         url1 = url
  141.         url1 += str(a+1)
  142.         url1 += urlend
  143.         download_it(url1)
  144.         sleep(1)
  145.  
  146. def func1():
  147.     os.system('clear')
  148.     one = str(raw_input(ques1))
  149.     os.system('clear')
  150.     two = str(raw_input(ques2))
  151.     os.system('clear')
  152.     three = str(raw_input(ques3))
  153.     os.system('clear')
  154.     four = str(raw_input(ques4))
  155.     os.system('clear')
  156.     five = str(raw_input(ques5))
  157.     os.system('clear')
  158.     return one, two, three, four, five
  159.    
  160.  
  161. def intro(api_key):
  162.     os.system('clear')
  163.     one, two, three, four, five = func1()
  164.     one = artist_gettoptags(api_key, one)
  165.     sleep(1)
  166.     two = artist_gettoptags(api_key, two)
  167.     arttags = one + two
  168.     sleep(1)
  169.     geo_getEvents(api_key, three, four, five, arttags)
  170.  
  171. ch0 = """\n#################################################\n#      """
  172. ch1 = """_          _        __   ___  _"""
  173. ch2 = """          #\n#     """
  174. ch3 = """| |    ___ | | __ _ / /_ / _ \( )___   """
  175. ch4 = """   #  \n#     """
  176. ch5 = """| |   / _ \| |/ _  |  _ \ (_) |/  __|   """
  177. ch5a = """  #  \n#     """
  178. ch6 = """| |__| (_) | | (_| | (_) \__, |\___ \ """
  179. ch6a = """   #  \n#    """
  180. ch7 = """ |_____\___/|_|\__,_|\___/  /_/ |__ _/   """
  181. ch8 = """  #  \n#   """
  182. ch9 = """ ____ _                     _     _  """
  183. ch10 = """       #  \n#  """
  184. ch11 = """ / ___(_) __ _    __ _ _   _(_) __| | ___ """
  185. ch12 = """   #  \n#  """
  186. ch13 = """| |  _| |/ _' |  / _' | | | | |/ _  |/ _ \ """
  187. ch14 = """  #  \n#  """
  188. ch15 = """| |_| | | (_| | | (_| | |_| | | (_| |  __/  """
  189. ch16 = """ #  \n#  """
  190. ch17 = """ \____|_|\__, |  \__, |\__,_|_|\__,_|\___| """
  191. ch18 = """  # \n#       """
  192. ch19 = """    |___/   |___/    LAST-FM       """
  193. ch20 = """      #  \n#                                               #  \n#################################################\n"""
  194. ch21 = """Please enter your last-fm api_key: """
  195.  
  196. red = _wrap_with('31')
  197. magenta = _wrap_with('35')
  198. cyan = _wrap_with('36')
  199.  
  200. ch = red(ch0)+magenta(ch1)+red(ch2)+magenta(ch3)+red(ch4)+magenta(ch5)+red(ch5a)+magenta(ch6)+red(ch6a)+magenta(ch7)+red(ch8)+magenta(ch9)+red(ch10)+magenta(ch11)+red(ch12)+magenta(ch13)+red(ch14)+magenta(ch15)+red(ch16)+magenta(ch17)+red(ch18)+magenta(ch19)+red(ch20)+cyan(ch21)
  201.  
  202. dh1 = """
  203. #################################################
  204. #                                               #  
  205. #  Answer the five following questions          #  
  206. #  and I will recommend you some gigs           #  
  207. #  using the Last-fm database...                #  
  208. #                                               #  
  209. #################################################
  210. Press <enter> to continue: """
  211.  
  212. os.system('clear')
  213. api_key = str(raw_input(ch))
  214. os.system('clear')
  215. d = str(raw_input(dh1))
  216. intro(api_key)
Advertisement
Add Comment
Please, Sign In to add comment