Advertisement
Guest User

Resonate Coop API - Playlist info

a guest
May 11th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1. ## Python 3 script to get useful information from Resonate Playlists
  2. ## I use this to tlak about the radio show I produce
  3. # import urllib library
  4. from urllib.request import urlopen
  5.  
  6. # import json
  7. import json
  8. # store the URL in url as
  9. # parameter for urlopen
  10. # This is a Resonate tracklist id.
  11. url = "https://api.resonate.coop/v2/trackgroups/9d9f7bcb-6082-470a-b66c-ec7e15a13305"
  12.  
  13. # store the response of URL
  14. response = urlopen(url)
  15.  
  16. # storing the JSON response
  17. # from url in data
  18. data_json = json.loads(response.read())
  19.  
  20. titles = []
  21. artists= []
  22. creator_ids = []
  23.  
  24. items = data_json['data']['items']
  25. for item in items:
  26.     title = item['track']['title']
  27.     artist = item['track']['artist']
  28.     creator_id = item['track']['creator_id']
  29.     titles.append(title)
  30.     artists.append(artist)
  31.     creator_ids.append(creator_id)
  32.  
  33. # Comma seperated list of track,artist - easy to copy into a spreadsheet
  34. tracks = dict(zip(titles, artists))
  35. for key,value in tracks.items():
  36.     print("{},{}".format(key,value))
  37.    
  38. # 3 lists - just tracks, just artists, written list of artists comma seperated for text posts  
  39. print(*titles, sep = "\n")
  40. print(*artists, sep = "\n")
  41. print(*artists, sep = ", ")
  42.  
  43.  
  44. # We need to fetch info about each artists to get more information - We assume creator id = artist id
  45. artists2 = dict(zip(artists, creator_ids))
  46. twitter = []
  47. instagram = []
  48. youtube = []
  49. facebook = []
  50.  
  51. # Simple list of links to artist pages
  52. for key,value in artists2.items():
  53.     print("https://api.resonate.coop/v2/artists/{}".format(value))
  54.    
  55.    
  56. for key,value in artists2.items():
  57.     url = "https://api.resonate.coop/v2/artists/{}".format(value)
  58.     # Markdown of Artist with a link - useful for the webiste and Resonate community posts
  59.     print("[{}](https://stream.resonate.coop/artist/{})".format(key,value), end = ', ')
  60.  
  61.     # store the response of URL
  62.     response = urlopen(url)
  63.      
  64.     # storing the JSON response
  65.     # from url in data
  66.     data_json = json.loads(response.read())
  67.    
  68.     links = data_json['data']['links']
  69.     for link in links:
  70.         if "twitter" in link['href']:
  71.             twitter.append(link['href'])
  72.         elif "instagram" in link['href']:
  73.             instagram.append(link['href'])
  74.         elif "youtube" in link['href']:
  75.             youtube.append(link['href'])
  76.         elif "facebook" in link['href']:
  77.             facebook.append(link['href'])
  78.    
  79. # We can click on all the links to follow people
  80. print(*twitter, sep = "\n")
  81. print(*instagram, sep = "\n")
  82. print(*youtube, sep = "\n")
  83. print(*facebook, sep = "\n")
  84.  
  85. # This gives us a nice list we can copy directly into posts on these services
  86. print("instagram")
  87. for item in instagram:
  88.     item = item.replace("https://", "")
  89.     item = item.replace("www.", "")
  90.     item = item.replace("instagram.com/", "@")
  91.     print(item)
  92.    
  93. print("twitter")
  94. for item in twitter:
  95.     item = item.replace("https://", "")
  96.     item = item.replace("www.", "")
  97.     item = item.replace("twitter.com/", "@")
  98.     print(item)
  99.  
  100. print("facebook")
  101. for item in facebook:
  102.     item = item.replace("https://", "")
  103.     item = item.replace("www.", "")
  104.     item = item.replace("facebook.com/", "@")
  105.     print(item)
  106.  
  107. # print the json response
  108. #print(data_json)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement