Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
BrainAxe committed Dec 22, 2016
0 parents commit 5bc1b0f
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Spotify_Lyrics
Downloads from [musixmatch](www.musixmatch.com) and displays lyrics to currently playing song in the Spotify desktop client in the Linux Terminal.


# running from source
If you want to run from source you need:

* Python 2.7 (probably any version of Python 2)
* pip install -r requirements.txt
* Open Terminal
* Type python and the location of the spotify_lyrics.py file
```python home/username/spotify_lyrics.py ```
* Now press Enter and see the magic!!!




# screenshot
Tested in linux mint 18

![ScreenShot](Screenshot.png)


# Contact me
* email: tanzimrizwan@gmail.com
Binary file added Screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
beautifulsoup4==4.5.1
requests==2.11.1
118 changes: 118 additions & 0 deletions spotify_lyrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import requests
import urllib2
import time
import os
import sys
import re

import subprocess
import dbus



def getlyrics(songname):
error = "Error: No lyrics Found."
artist = ""
song = ""
url = ""
if songname.count(" - ") == 1:
artist, song = songname.rsplit(" - ", 1)
if songname.count(" - ") == 2:
artist, song, garbage = songname.rsplit(" - ", 2)
if " / " in song:
song, garbage = song.rsplit(" / ", 1)
song = re.sub(' \(.*?\)', '', song, flags=re.DOTALL)



def lyrics_musixmatch(artist, song):
url = ""
try:
searchurl = "https://www.musixmatch.com/search/%s %s" % (artist, song)

searchurl = searchurl.replace(" ","%20")

searchresult = urllib2.urlopen(searchurl)
soup = BeautifulSoup(searchresult, 'html.parser')
li = soup.find('a', {"class": "title"})['href']
link = "https://www.musixmatch.com" + li
url = link
lyricspage = urllib2.urlopen(url)

soup = BeautifulSoup(lyricspage, 'html.parser')
lyrics = soup.text.split('"body":"')[1].split('","language"')[0]
lyrics = lyrics.replace("\\n", "\n")
lyrics = lyrics.replace("\\", "")

except Exception:
lyrics = error
return lyrics



lyrics = lyrics_musixmatch(artist, song)

lyrics = lyrics.replace("&amp;", "&")
lyrics = lyrics.replace("`", "'")
lyrics = lyrics.strip()
return lyrics




def getwindowtitle():

windowname = ''
session = dbus.SessionBus()
spotifydbus = session.get_object("org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2")
spotifyinterface = dbus.Interface(spotifydbus, "org.freedesktop.DBus.Properties")
metadata = spotifyinterface.Get("org.mpris.MediaPlayer2.Player", "Metadata")
try:
command = "xwininfo -tree -root"
windows = subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8")
spotify = ''
for line in windows.splitlines():
if '("spotify" "Spotify")' in line:
if " - " in line:
spotify = line
break
if spotify == '':
windowname = 'Spotify'
except Exception:
pass
if windowname != 'Spotify':
windowname = "%s - %s" %(metadata['xesam:artist'][0], metadata['xesam:title'])


if "Spotify - " in windowname:
windowname = windowname.strip("Spotify - ")
return(windowname)



def main():
os.system("clear")

oldsongname = ""

while True:
songname = getwindowtitle()
#print songname
if oldsongname != songname:
if songname != "Spotify":
oldsongname = songname
#os.system("clear")

print(songname+"\n")
lyrics = getlyrics(songname)
print(lyrics+"\n")
time.sleep(2)
time.sleep(1)




if __name__ == '__main__':
main()

0 comments on commit 5bc1b0f

Please sign in to comment.