Advertisement
jkonefal

Untitled

Nov 24th, 2023
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. import aiohttp
  2. import asyncio
  3. from genius_key2 import HEADERS
  4. import requests
  5.  
  6.  
  7. def make_query(album_id):
  8.     return {"id": album_id}
  9.  
  10.  
  11. def print_songs(album, album_name):
  12.     print(album_name, ":")
  13.     print()
  14.     song_list = album["album_appearances"]
  15.     for song in song_list:
  16.         print(song["song"]["full_title"])
  17.     print()
  18.     print("-------")
  19.     print()
  20.  
  21.  
  22. async def get_songs(album_ids_and_names):
  23.     async with aiohttp.ClientSession() as session:
  24.         URL = "https://genius-song-lyrics1.p.rapidapi.com/album/appearances/"
  25.         tasks = [
  26.             session.get(URL, headers=HEADERS, params=make_query(album_id))
  27.             for album_id, album_name in album_ids_and_names
  28.         ]
  29.         album_appearances = []
  30.         responses = await asyncio.gather(*tasks)
  31.         for response in responses:
  32.             album_appearances.append(await response.json())
  33.         for it, album in enumerate(album_appearances):
  34.             print_songs(album, album_ids_and_names[it][1])
  35.  
  36.  
  37. def get_albums(URL, querystring):
  38.     response = requests.get(URL, headers=HEADERS, params=querystring)
  39.     albums = response.json()["albums"]
  40.     album_ids_and_names = [
  41.         (album["api_path"].split("/")[2], album["full_title"]) for album in albums
  42.     ]
  43.     asyncio.run(get_songs(album_ids_and_names))
  44.  
  45.  
  46. if __name__ == "__main__":
  47.     URL = "https://genius-song-lyrics1.p.rapidapi.com/artist/albums/"
  48.     querystring = {"id": "1177", "per_page": "20", "page": "1"}
  49.     get_albums(URL, querystring)
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement