Skip to content

Commit

Permalink
Merge pull request #4 from cpiber/master
Browse files Browse the repository at this point in the history
Update to work with python3 as well, and less ressources
  • Loading branch information
felipemarinho97 authored Feb 23, 2022
2 parents e932525 + 3692754 commit 5746df8
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions musnify-mpd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# coding: utf-8
import ConfigParser
try:
from ConfigParser import ConfigParser
except ImportError:
from configparser import ConfigParser
import json
import os
import sys
Expand All @@ -22,14 +25,14 @@
print("Loading default config")
configFile = "/etc/musnify-mpd.config"

config = ConfigParser.ConfigParser()
config = ConfigParser()
config.read(configFile)

host = config.get("mpd","host")
port = config.get("mpd","port")
host = config.get("mpd","host", fallback=os.environ.get("MPD_HOST", "localhost"))
port = config.get("mpd","port", fallback=os.environ.get("MPD_PORT", 6600))
if config.has_option("apiKey", "lastfm"):
apiKey = config.get("apiKey", "lastfm")
musicLibrary = os.path.expanduser(config.get("mpd","musiclibrary")) + "/"
musicLibrary = os.path.expanduser(config.get("mpd","musiclibrary", fallback='~/Music')) + "/"

debug = False

Expand Down Expand Up @@ -65,6 +68,9 @@ def getCurrentSong(self):

def getStatus(self):
return self.client.status()["state"]

def waitForChange(self):
return self.client.idle("player")


class NotificationWrapper:
Expand Down Expand Up @@ -131,11 +137,14 @@ def downloadPixbufAlbumCover(url):
@staticmethod
def fetchLocalCover(path):
regex = re.compile(r'(album|cover|\.?folder|front).*\.(gif|jpeg|jpg|png)$', re.I | re.X)
for e in os.listdir(path):
if regex.match(e) != None:
if debug:
print("local cover found at " + path + e)
return Pixbuf.new_from_file(path + e)
try:
for e in os.listdir(path):
if regex.match(e) != None:
if debug:
print("local cover found at " + path + e)
return Pixbuf.new_from_file(path + e)
except:
pass
if debug:
print("Nothing found on local directory")
return False
Expand All @@ -152,7 +161,6 @@ def start(self):
song = ""

while True:
time.sleep(0.5)
actualStatus = mpd.getStatus()
actualSong = mpd.getCurrentSong()

Expand All @@ -170,6 +178,8 @@ def start(self):
self.handle(song)
if debug:
print(song)

mpd.waitForChange()

def handle(self, song):
localCoverPath = CoverArt.fetchLocalCover(musicLibrary + self._separa(song["file"]))
Expand Down Expand Up @@ -207,21 +217,23 @@ def help():
-d\t\tRun with debug mode enabled
""")

for i in range(len(sys.argv)):
if sys.argv[i] == "-h":
host = sys.argv[i + 1]
if sys.argv[i] == "-p":
port = sys.argv[i + 1]
if sys.argv[i] == "-d":
debug = True
if sys.argv[i] == "--help":
help()
exit()

if __name__ == "__main__":
for i in range(len(sys.argv)):
if sys.argv[i] == "-h":
host = sys.argv[i + 1]
if sys.argv[i] == "-p":
port = sys.argv[i + 1]
if sys.argv[i] == "-d":
debug = True
if sys.argv[i] == "--help":
help()
exit()

musnify = Musnify()

try:
musnify.start()
except KeyboardInterrupt:
pass
finally:
musnify.stop()

0 comments on commit 5746df8

Please sign in to comment.