Skip to content

Instantly share code, notes, and snippets.

@trevormunoz
Created April 18, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trevormunoz/d52501885dd3d91a1127e58fe6c794ef to your computer and use it in GitHub Desktop.
Save trevormunoz/d52501885dd3d91a1127e58fe6c794ef to your computer and use it in GitHub Desktop.
Genius API
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import os\n",
"import time\n",
"import requests\n",
"import json\n",
"import csv"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ARTISTS = ['The Roots',\n",
"'Eve',\n",
"'DJ Jazzy Jeff & The Fresh Prince',\n",
"'Ludacris',\n",
"'T.I.',\n",
"'Kanye West',\n",
"'Chance the Rapper',\n",
"'Common',\n",
"'Gucci Mane',\n",
"'Migos',\n",
"'OutKast',\n",
"'Twista',\n",
"'Crucial Conflict',\n",
"'Lupe Fiasco',\n",
"'Digital Underground',\n",
"'Tupac',\n",
"'Trouble Funk']"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"s = requests.Session()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"s.headers.update({'Authorization': 'Bearer {0}'.format(os.environ['GENIUS_ACCESS_TOKEN'])})"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def get_song(song_id):\n",
" r = s.get('http://api.genius.com/songs/{0}'.format(song_id))\n",
" return r.json() "
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def get_artist_songs(artist_id):\n",
" page = 1\n",
" all_songs = []\n",
" while True:\n",
" r = s.get('http://api.genius.com/artists/{0}/songs'.format(artist_id), params={\"page\": page})\n",
" resp = r.json()['response']\n",
" if 'songs' not in resp or len(resp['songs']) == 0:\n",
" break\n",
" for song in resp['songs']:\n",
" all_songs.append(get_song(song['id']))\n",
" time.sleep(.25)\n",
" page += 1\n",
" return all_songs"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for artist in artists:\n",
" # get artist_id\n",
" # songs = get_artist_songs(1421)\n",
" # song table: title, primary artist, featured artist, producer, lyrics (as external file?)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'meta': {'status': 200},\n",
" 'response': {'song': {'album': {'api_path': '/albums/20819',\n",
" 'artist': {'api_path': '/artists/492',\n",
" 'header_image_url': 'https://images.genius.com/1698cc02d9cd844c6872fbd1b424cb45.900x770x1.jpg',\n",
" 'id': 492,\n",
" 'image_url': 'https://images.genius.com/a4fc4d424642d59a40b43128491b23ed.586x586x1.jpg',\n",
" 'iq': 11866,\n",
" 'is_meme_verified': True,\n",
" 'is_verified': True,\n",
" 'name': 'Big Sean',\n",
" 'url': 'https://genius.com/artists/Big-sean'},\n",
" 'cover_art_url': 'https://s3.amazonaws.com/rapgenius/Detroit_by_Big_Sean_cover.jpg',\n",
" 'id': 20819,\n",
" 'name': 'Detroit',\n",
" 'url': 'https://genius.com/albums/Big-sean/Detroit'},\n",
" 'annotation_count': 55,\n",
" 'api_path': '/songs/87318',\n",
" 'current_user_metadata': {'excluded_permissions': ['follow',\n",
" 'award_transcription_iq',\n",
" 'remove_transcription_iq',\n",
" 'pyong',\n",
" 'edit_lyrics',\n",
" 'view_annotation_engagement_data',\n",
" 'publish',\n",
" 'unpublish',\n",
" 'edit_spotify_details',\n",
" 'hide',\n",
" 'unhide',\n",
" 'toggle_featured_video',\n",
" 'add_pinned_annotation_to',\n",
" 'add_community_annotation_to',\n",
" 'destroy',\n",
" 'mark_as_not_spam',\n",
" 'edit_spotify_annotations_for',\n",
" 'verify_lyrics',\n",
" 'edit_anything',\n",
" 'edit_album_appearances',\n",
" 'edit_any_media',\n",
" 'edit',\n",
" 'rename',\n",
" 'edit_tags',\n",
" 'watch_fact_track',\n",
" 'reindex',\n",
" 'view_lyrics_synchronization',\n",
" 'enable_media',\n",
" 'disable_media',\n",
" 'edit_lyrics_or_annotation_brackets',\n",
" 'see_editorial_indicators',\n",
" 'edit_youtube_url',\n",
" 'edit_soundcloud_url',\n",
" 'edit_spotify_uuid',\n",
" 'edit_apple_music_url',\n",
" 'edit_vevo_url',\n",
" 'moderate_annotations',\n",
" 'create_annotation',\n",
" 'see_short_id',\n",
" 'manage_chart_item'],\n",
" 'interactions': {'following': False, 'pyong': False},\n",
" 'iq_by_action': {},\n",
" 'permissions': ['see_pageviews', 'create_comment'],\n",
" 'relationships': {}},\n",
" 'custom_performances': [],\n",
" 'description': {'dom': {'children': [{'children': ['The 9th song on Big Sean’s “Detroit” mixtape. The song features Detroit rapper Royce Da 5\\'9\", James Fauntleroy, and Kendrick Lamar. As stated in the chorus, this song is about all the goals and accomplishments that he has yet to accomplish before death.'],\n",
" 'tag': 'p'}],\n",
" 'tag': 'root'}},\n",
" 'description_annotation': {'_type': 'referent',\n",
" 'annotatable': {'api_path': '/songs/87318',\n",
" 'client_timestamps': {'lyrics_updated_at': 1490931241,\n",
" 'updated_by_human_at': 1490931241},\n",
" 'context': 'Big Sean',\n",
" 'id': 87318,\n",
" 'image_url': 'https://s3.amazonaws.com/rapgenius/Detroit_by_Big_Sean_cover.jpg',\n",
" 'link_title': '100 by\\xa0Big\\xa0Sean (Ft.\\xa0James\\xa0Fauntleroy, Kendrick\\xa0Lamar & Royce\\xa0da 5’9”)',\n",
" 'title': '100',\n",
" 'type': 'Song',\n",
" 'url': 'https://genius.com/Big-sean-100-lyrics'},\n",
" 'annotations': [{'api_path': '/annotations/3693528',\n",
" 'authors': [{'attribution': 0.9302325581395349,\n",
" 'pinned_role': None,\n",
" 'user': {'api_path': '/users/173442',\n",
" 'avatar': {'medium': {'bounding_box': {'height': 400, 'width': 300},\n",
" 'url': 'https://images.rapgenius.com/avatars/medium/e248de16870686e81d9d2b60716283c6'},\n",
" 'small': {'bounding_box': {'height': 100, 'width': 100},\n",
" 'url': 'https://images.rapgenius.com/avatars/small/e248de16870686e81d9d2b60716283c6'},\n",
" 'thumb': {'bounding_box': {'height': 32, 'width': 32},\n",
" 'url': 'https://images.rapgenius.com/avatars/thumb/e248de16870686e81d9d2b60716283c6'},\n",
" 'tiny': {'bounding_box': {'height': 16, 'width': 16},\n",
" 'url': 'https://images.rapgenius.com/avatars/tiny/e248de16870686e81d9d2b60716283c6'}},\n",
" 'current_user_metadata': {'excluded_permissions': ['follow'],\n",
" 'features': [],\n",
" 'interactions': {'following': False},\n",
" 'permissions': []},\n",
" 'header_image_url': 'https://images.rapgenius.com/avatars/medium/e248de16870686e81d9d2b60716283c6',\n",
" 'human_readable_role_for_display': 'Editor',\n",
" 'id': 173442,\n",
" 'iq': 5198,\n",
" 'login': 'muvinco',\n",
" 'name': 'muvinco',\n",
" 'role_for_display': 'editor',\n",
" 'url': 'https://genius.com/muvinco'}},\n",
" {'attribution': 0.06976744186046512,\n",
" 'pinned_role': None,\n",
" 'user': {'api_path': '/users/699907',\n",
" 'avatar': {'medium': {'bounding_box': {'height': 400, 'width': 300},\n",
" 'url': 'https://images.genius.com/avatars/medium/b0eed8f0cb51f153db177e6849caf8cd'},\n",
" 'small': {'bounding_box': {'height': 100, 'width': 100},\n",
" 'url': 'https://images.genius.com/avatars/small/b0eed8f0cb51f153db177e6849caf8cd'},\n",
" 'thumb': {'bounding_box': {'height': 32, 'width': 32},\n",
" 'url': 'https://images.genius.com/avatars/thumb/b0eed8f0cb51f153db177e6849caf8cd'},\n",
" 'tiny': {'bounding_box': {'height': 16, 'width': 16},\n",
" 'url': 'https://images.genius.com/avatars/tiny/b0eed8f0cb51f153db177e6849caf8cd'}},\n",
" 'current_user_metadata': {'excluded_permissions': ['follow'],\n",
" 'features': [],\n",
" 'interactions': {'following': False},\n",
" 'permissions': []},\n",
" 'header_image_url': 'https://images.genius.com/avatars/medium/b0eed8f0cb51f153db177e6849caf8cd',\n",
" 'human_readable_role_for_display': 'Contributor',\n",
" 'id': 699907,\n",
" 'iq': 7664,\n",
" 'login': 'rappajanai',\n",
" 'name': 'rappajanai',\n",
" 'role_for_display': 'contributor',\n",
" 'url': 'https://genius.com/rappajanai'}}],\n",
" 'body': {'dom': {'children': [{'children': ['The 9th song on Big Sean’s “Detroit” mixtape. The song features Detroit rapper Royce Da 5\\'9\", James Fauntleroy, and Kendrick Lamar. As stated in the chorus, this song is about all the goals and accomplishments that he has yet to accomplish before death.'],\n",
" 'tag': 'p'}],\n",
" 'tag': 'root'}},\n",
" 'comment_count': 0,\n",
" 'community': True,\n",
" 'cosigned_by': [],\n",
" 'current_user_metadata': {'excluded_permissions': ['vote',\n",
" 'edit',\n",
" 'cosign',\n",
" 'uncosign',\n",
" 'destroy',\n",
" 'accept',\n",
" 'reject',\n",
" 'see_unreviewed',\n",
" 'clear_votes',\n",
" 'propose_edit_to',\n",
" 'pin_to_profile',\n",
" 'unpin_from_profile',\n",
" 'update_source',\n",
" 'set_brand',\n",
" 'clear_brand',\n",
" 'edit_custom_preview'],\n",
" 'interactions': {'cosign': False, 'pyong': False, 'vote': None},\n",
" 'iq_by_action': {},\n",
" 'permissions': ['create_comment']},\n",
" 'custom_preview': None,\n",
" 'has_voters': True,\n",
" 'id': 3693528,\n",
" 'pinned': False,\n",
" 'share_url': 'https://genius.com/3693528',\n",
" 'source': None,\n",
" 'state': 'accepted',\n",
" 'url': 'https://genius.com/3693528/Big-sean-100/100',\n",
" 'verified': False,\n",
" 'verified_by': None,\n",
" 'votes_total': 10}],\n",
" 'annotator_id': 173442,\n",
" 'annotator_login': 'muvinco',\n",
" 'api_path': '/referents/3693528',\n",
" 'classification': 'accepted',\n",
" 'fragment': '100',\n",
" 'id': 3693528,\n",
" 'is_description': True,\n",
" 'path': '/3693528/Big-sean-100/100',\n",
" 'range': {'content': '100'},\n",
" 'song_id': 87318,\n",
" 'url': 'https://genius.com/3693528/Big-sean-100/100',\n",
" 'verified_annotator_ids': []},\n",
" 'embed_content': \"<div id='rg_embed_link_87318' class='rg_embed_link' data-song-id='87318'>Read <a href='https://genius.com/Big-sean-100-lyrics'>“100” by Big\\xa0Sean</a> on Genius</div> <script crossorigin src='//genius.com/songs/87318/embed.js'></script>\",\n",
" 'featured_artists': [{'api_path': '/artists/471',\n",
" 'header_image_url': 'https://images.genius.com/01eb9c896e277a0601d927a72e4984a6.1000x667x1.jpg',\n",
" 'id': 471,\n",
" 'image_url': 'https://images.genius.com/6c9e8e0a0f289d6b227c97af52b00d7a.667x667x1.jpg',\n",
" 'iq': 10449,\n",
" 'is_meme_verified': True,\n",
" 'is_verified': True,\n",
" 'name': 'Royce da 5’9”',\n",
" 'url': 'https://genius.com/artists/Royce-da-59'},\n",
" {'api_path': '/artists/1421',\n",
" 'header_image_url': 'https://images.genius.com/9902b6817ea3eefd943974c493efd11f.900x500x1.png',\n",
" 'id': 1421,\n",
" 'image_url': 'https://images.genius.com/087d765fe979cd16c97c8adbc04fcbc6.615x615x1.jpg',\n",
" 'iq': 26891,\n",
" 'is_meme_verified': True,\n",
" 'is_verified': True,\n",
" 'name': 'Kendrick Lamar',\n",
" 'url': 'https://genius.com/artists/Kendrick-lamar'},\n",
" {'api_path': '/artists/17649',\n",
" 'header_image_url': 'https://images.genius.com/642afd8c972482b019f7f8a5efd26aad.500x391x1.jpg',\n",
" 'id': 17649,\n",
" 'image_url': 'https://images.genius.com/642afd8c972482b019f7f8a5efd26aad.500x391x1.jpg',\n",
" 'iq': 1378,\n",
" 'is_meme_verified': True,\n",
" 'is_verified': True,\n",
" 'name': 'James Fauntleroy',\n",
" 'url': 'https://genius.com/artists/James-fauntleroy'}],\n",
" 'featured_video': False,\n",
" 'full_title': '100 by\\xa0Big\\xa0Sean (Ft.\\xa0James\\xa0Fauntleroy, Kendrick\\xa0Lamar & Royce\\xa0da 5’9”)',\n",
" 'header_image_thumbnail_url': 'https://s3.amazonaws.com/rapgenius/Detroit_by_Big_Sean_cover.jpg',\n",
" 'header_image_url': 'https://s3.amazonaws.com/rapgenius/Detroit_by_Big_Sean_cover.jpg',\n",
" 'id': 87318,\n",
" 'lyrics_owner_id': 99090,\n",
" 'media': [{'provider': 'apple_music',\n",
" 'provider_id': '474877077',\n",
" 'type': 'audio',\n",
" 'url': 'https://itunes.apple.com/lookup?entity=song&id=474877077'},\n",
" {'provider': 'youtube',\n",
" 'start': 0,\n",
" 'type': 'video',\n",
" 'url': 'http://www.youtube.com/watch?v=7K61XatXCGQ'}],\n",
" 'path': '/Big-sean-100-lyrics',\n",
" 'primary_artist': {'api_path': '/artists/492',\n",
" 'header_image_url': 'https://images.genius.com/1698cc02d9cd844c6872fbd1b424cb45.900x770x1.jpg',\n",
" 'id': 492,\n",
" 'image_url': 'https://images.genius.com/a4fc4d424642d59a40b43128491b23ed.586x586x1.jpg',\n",
" 'iq': 11866,\n",
" 'is_meme_verified': True,\n",
" 'is_verified': True,\n",
" 'name': 'Big Sean',\n",
" 'url': 'https://genius.com/artists/Big-sean'},\n",
" 'producer_artists': [{'api_path': '/artists/23691',\n",
" 'header_image_url': 'https://s3.amazonaws.com/rapgenius/DJ%20DON%20CANNON.jpg',\n",
" 'id': 23691,\n",
" 'image_url': 'https://s3.amazonaws.com/rapgenius/DJ%20DON%20CANNON.jpg',\n",
" 'is_meme_verified': False,\n",
" 'is_verified': False,\n",
" 'name': 'DJ Don Cannon',\n",
" 'url': 'https://genius.com/artists/Dj-don-cannon'}],\n",
" 'pyongs_count': 28,\n",
" 'recording_location': None,\n",
" 'release_date': '2012-09-05',\n",
" 'song_art_image_thumbnail_url': 'https://s3.amazonaws.com/rapgenius/Detroit_by_Big_Sean_cover.jpg',\n",
" 'song_art_image_url': 'https://s3.amazonaws.com/rapgenius/Detroit_by_Big_Sean_cover.jpg',\n",
" 'song_relationships': [{'songs': [], 'type': 'samples'},\n",
" {'songs': [], 'type': 'sampled_in'},\n",
" {'songs': [], 'type': 'interpolates'},\n",
" {'songs': [], 'type': 'interpolated_by'},\n",
" {'songs': [], 'type': 'cover_of'},\n",
" {'songs': [], 'type': 'covered_by'},\n",
" {'songs': [], 'type': 'remix_of'},\n",
" {'songs': [], 'type': 'remixed_by'},\n",
" {'songs': [], 'type': 'live_version_of'},\n",
" {'songs': [], 'type': 'performed_live_as'}],\n",
" 'stats': {'accepted_annotations': 54,\n",
" 'contributors': 88,\n",
" 'hot': False,\n",
" 'iq_earners': 88,\n",
" 'pageviews': 120251,\n",
" 'transcribers': 2,\n",
" 'unreviewed_annotations': 0,\n",
" 'verified_annotations': 0},\n",
" 'title': '100',\n",
" 'url': 'https://genius.com/Big-sean-100-lyrics',\n",
" 'verified_annotations_by': [],\n",
" 'verified_contributors': [],\n",
" 'verified_lyrics_by': [],\n",
" 'writer_artists': []}}}"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"songs[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:base]",
"language": "python",
"name": "conda-env-base-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment