├── constants.py ├── .gitignore ├── README.md ├── LICENSE ├── lyric-view.txt └── get-lyric.py /constants.py: -------------------------------------------------------------------------------- 1 | TOKEN = '' 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### Python ### 2 | # Byte-compiled / optimized / DLL files 3 | __pycache__/ 4 | *.py[cod] 5 | *$py.class 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lyrics-crawler 2 | [![Say Thanks!](https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg)](https://saythanks.io/to/willamesoares) 3 | 4 | Get the lyrics for the song currently playing on Spotify or search for any song passing its title and artist name in the command line. 5 | 6 | ### Setup 7 | In order to use this script you have to create your own [Genius API](https://docs.genius.com) Access Token and place it into the `constants.py` 8 | 9 | like this `TOKEN='YOUR_GENIUS_API_TOKEN'`. 10 | 11 | ### Default search 12 | `python3 get-lyric.py` 13 | 14 | ### Custom search 15 | `python3 get-lyric.py [song_title] [artist_name]` 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Willame Soares Barroso 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /lyric-view.txt: -------------------------------------------------------------------------------- 1 | Do You Know Where Your Children Are (Original Version) by Michael Jackson 2 | 3 | [Verse 1] 4 | Father comes home from work, and he is scared to death 5 | Mother cries for the kid, and the note she read 6 | Father runs to the table, he said "What's going on?" 7 | Mother cries desperately, "Our little baby's gone" 8 | 9 | [Chorus] 10 | Do you know where your children are? 11 | Because it's now 12 o'clock 12 | If they're somewhere out on the street 13 | Just imagine how scared they are 14 | Do you know where your children are? 15 | Because it's now 12 o'clock 16 | If they're somewhere out on the street 17 | Just imagine how scared they are 18 | 19 | [Verse 2] 20 | She wrote that she is tired of step daddy using her 21 | Saying that he'll buy her things, while sexually abusing her 22 | Just think that she's all alone somewhere out on the street 23 | How will this girl survive? She ain't got nothing to eat 24 | 25 | [Chorus] 26 | Do you know where your children are? 27 | Because it's now 12 o'clock 28 | If they're somewhere out on the street 29 | Just imagine how scared they are 30 | Do you know where your children are? 31 | Because it's now 12 o'clock 32 | If they're somewhere out on the street 33 | Just imagine how scared they are 34 | 35 | [Bridge] 36 | Save me (from this living hell) 37 | Save me (cause I, I wanna help) 38 | Save me (I'm tired and I'm mad) 39 | Save me (cause this, it ain't no joke) 40 | Save me (cause I'm out for blood) 41 | Save me (cause I don't wanna ignore) 42 | Save me (it's time I replied) 43 | Save me 44 | Save me 45 | 46 | [Verse 3] 47 | Now she's on the move, she's off to Hollywood 48 | She says she want to be a star, she heard the money's good 49 | She gets off from the train station, the man is waiting there 50 | "I'll show you where the money is, girl just let down your hair" 51 | He is taking her on the streets, off Sunset Boulevard 52 | She is selling her body hot, girl that will take you far 53 | The police come around the corner, somebody there they told 54 | He's arresting this little girl, she's only 12 years old 55 | 56 | [Chorus] 57 | Do you know where your children are? 58 | Because it's now 12 o'clock 59 | If they're somewhere out on the street 60 | Just imagine how scared they are 61 | Do you know where your children are? 62 | Because it's now 12 o'clock 63 | If they're somewhere out on the street 64 | Just imagine how scared they are 65 | 66 | Do you know where your children are? 67 | Because it's now 12 o'clock 68 | If they're somewhere out on the street 69 | Just imagine how scared they are 70 | Do you know where your children are? 71 | Because it's now 12 o'clock 72 | If they're somewhere out on the street 73 | Just imagine how scared they are 74 | 75 | Do you know where your children are? 76 | Because it's now 12 o'clock 77 | If they're somewhere out on the street 78 | Just imagine how scared they are 79 | Do you know where your children are? 80 | Because it's now 12 o'clock 81 | If they're somewhere out on the street 82 | Just imagine how scared they are 83 | 84 | Do you know where your children are? 85 | Because it's now 12 o'clock 86 | If they're somewhere out on the street 87 | Just imagine how scared they are 88 | Do you know where your children are? 89 | Because it's now 12 o'clock 90 | If they're somewhere out on the street 91 | Just imagine how scared they are 92 | 93 | Do you know where your children are? 94 | Because it's now 12 o'clock 95 | If they're somewhere out on the street 96 | 97 | -------------------------------------------------------------------------------- /get-lyric.py: -------------------------------------------------------------------------------- 1 | import sys, dbus, requests 2 | from bs4 import BeautifulSoup 3 | from constants import ( 4 | TOKEN 5 | ) 6 | 7 | defaults = { 8 | 'request': { 9 | 'token': TOKEN, 10 | 'base_url': 'https://api.genius.com' 11 | }, 12 | 'message': { 13 | 'search_fail': 'The lyrics for this song were not found!', 14 | 'wrong_input': 'Wrong number of arguments.\n' \ 15 | 'Use two parameters to perform a custom search ' \ 16 | 'or none to get the song currently playing on Spotify.' 17 | } 18 | } 19 | 20 | def get_current_song_info(): 21 | # kudos to jooon at stackoverflow http://stackoverflow.com/a/33923095 22 | session_bus = dbus.SessionBus() 23 | spotify_bus = session_bus.get_object('org.mpris.MediaPlayer2.spotify', 24 | '/org/mpris/MediaPlayer2') 25 | spotify_properties = dbus.Interface(spotify_bus, 26 | 'org.freedesktop.DBus.Properties') 27 | metadata = spotify_properties.Get('org.mpris.MediaPlayer2.Player', 'Metadata') 28 | 29 | return {'artist': metadata['xesam:artist'][0], 'title': metadata['xesam:title']} 30 | 31 | def request_song_info(song_title, artist_name): 32 | base_url = defaults['request']['base_url'] 33 | headers = {'Authorization': 'Bearer ' + defaults['request']['token']} 34 | search_url = base_url + '/search' 35 | data = {'q': song_title + ' ' + artist_name} 36 | response = requests.get(search_url, data=data, headers=headers) 37 | 38 | return response 39 | 40 | def scrap_song_url(url): 41 | page = requests.get(url) 42 | html = BeautifulSoup(page.text, 'html.parser') 43 | [h.extract() for h in html('script')] 44 | lyrics = html.find('div', class_='lyrics').get_text() 45 | 46 | return lyrics 47 | 48 | def main(): 49 | args_length = len(sys.argv) 50 | if args_length == 1: 51 | # Get info about song currently playing on Spotify 52 | current_song_info = get_current_song_info() 53 | song_title = current_song_info['title'] 54 | artist_name = current_song_info['artist'] 55 | elif args_length == 3: 56 | # Use input as song title and artist name 57 | song_info = sys.argv 58 | song_title, artist_name = song_info[1], song_info[2] 59 | else: 60 | print(defaults['message']['wrong_input']) 61 | return 62 | 63 | print('{} by {}'.format(song_title, artist_name)) 64 | 65 | # Search for matches in request response 66 | response = request_song_info(song_title, artist_name) 67 | json = response.json() 68 | remote_song_info = None 69 | 70 | for hit in json['response']['hits']: 71 | if artist_name.lower() in hit['result']['primary_artist']['name'].lower(): 72 | remote_song_info = hit 73 | break 74 | 75 | # Extract lyrics from URL if song was found 76 | if remote_song_info: 77 | song_url = remote_song_info['result']['url'] 78 | lyrics = scrap_song_url(song_url) 79 | 80 | write_lyrics_to_file(lyrics, song_title, artist_name) 81 | 82 | print(lyrics) 83 | else: 84 | print(defaults['message']['search_fail']) 85 | 86 | def write_lyrics_to_file (lyrics, song, artist): 87 | f = open('lyric-view.txt', 'w') 88 | f.write('{} by {}'.format(song, artist)) 89 | f.write(lyrics) 90 | f.close() 91 | 92 | if __name__ == '__main__': 93 | main() 94 | --------------------------------------------------------------------------------