Added possibility to fetch album art from HTTP server
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
#!/bin/python
|
#!/bin/python
|
||||||
import curses
|
import curses
|
||||||
import os
|
import os
|
||||||
|
import posixpath
|
||||||
|
import requests
|
||||||
from mpd import MPDClient
|
from mpd import MPDClient
|
||||||
import ffmpeg
|
import ffmpeg
|
||||||
import pixcat
|
import pixcat
|
||||||
@ -14,16 +16,19 @@ config = configparser.ConfigParser()
|
|||||||
config.read(os.path.expanduser("~/.config/miniplayer/config"))
|
config.read(os.path.expanduser("~/.config/miniplayer/config"))
|
||||||
|
|
||||||
if "player" not in config.sections():
|
if "player" not in config.sections():
|
||||||
config["player"] = {"music_directory": "~/Music",
|
config["player"] = {"font_width": 11,
|
||||||
"font_width": 11,
|
|
||||||
"font_height": 24,
|
"font_height": 24,
|
||||||
"image_method": "pixcat",
|
|
||||||
"album_art_only": False,
|
"album_art_only": False,
|
||||||
"volume_step": 5,
|
"volume_step": 5,
|
||||||
"auto_close": False,
|
"auto_close": False,
|
||||||
"show_playlist": True,
|
"show_playlist": True,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if "art" not in config.sections():
|
||||||
|
config["art"] = {"music_directory": "~/Music",
|
||||||
|
"image_method": "pixcat",
|
||||||
|
}
|
||||||
|
|
||||||
if "mpd" not in config.sections():
|
if "mpd" not in config.sections():
|
||||||
config["mpd"] = {"host": "localhost",
|
config["mpd"] = {"host": "localhost",
|
||||||
"port": "6600",
|
"port": "6600",
|
||||||
@ -59,6 +64,7 @@ for key, action in default_bindings.items():
|
|||||||
keybindings[key] = action
|
keybindings[key] = action
|
||||||
|
|
||||||
player_config = config["player"]
|
player_config = config["player"]
|
||||||
|
art_config = config["art"]
|
||||||
mpd_config = config["mpd"]
|
mpd_config = config["mpd"]
|
||||||
|
|
||||||
|
|
||||||
@ -71,17 +77,13 @@ IMAGERATIO = (player_config.getint("font_width", 11),
|
|||||||
player_config.getint("font_height", 24)
|
player_config.getint("font_height", 24)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Music directory
|
|
||||||
MUSICDIR = player_config.get("music_directory", "~/Music")
|
|
||||||
MUSICDIR = os.path.expanduser(MUSICDIR)
|
|
||||||
|
|
||||||
# MPD config
|
# MPD config
|
||||||
MPDHOST = mpd_config.get("host", "localhost")
|
MPDHOST = mpd_config.get("host", "localhost")
|
||||||
MPDPORT = mpd_config.getint("port", 6600)
|
MPDPORT = mpd_config.getint("port", 6600)
|
||||||
MPDPASS = mpd_config.get("pass", False)
|
MPDPASS = mpd_config.get("pass", False)
|
||||||
|
|
||||||
# What to use to draw images
|
# What to use to draw images
|
||||||
IMAGEMETHOD = player_config.get("image_method", "pixcat")
|
IMAGEMETHOD = art_config.get("image_method", "pixcat")
|
||||||
|
|
||||||
# Volume step
|
# Volume step
|
||||||
VOLUMESTEP = player_config.getint("volume_step", 5)
|
VOLUMESTEP = player_config.getint("volume_step", 5)
|
||||||
@ -314,14 +316,51 @@ class Player:
|
|||||||
|
|
||||||
self.screen_height, self.screen_width = window_height, window_width
|
self.screen_height, self.screen_width = window_height, window_width
|
||||||
|
|
||||||
|
|
||||||
def getAlbumArt(self, song_file):
|
def getAlbumArt(self, song_file):
|
||||||
|
"""
|
||||||
|
A function that fetches the album art and saves
|
||||||
|
it to self.album_art_loc
|
||||||
|
"""
|
||||||
|
http_base_url = art_config.get("http_base_url")
|
||||||
|
|
||||||
|
if http_base_url:
|
||||||
|
self._getAlbumArtFromHttpServer(http_base_url, song_file)
|
||||||
|
else:
|
||||||
|
self._getAlbumArtFromFile(song_file)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def _getAlbumArtFromHttpServer(self, base_url, song_file):
|
||||||
|
"""
|
||||||
|
A function that fetches the album art from the configured
|
||||||
|
HTTP server, and saves it to self.album_art_loc
|
||||||
|
"""
|
||||||
|
|
||||||
|
album = os.path.dirname(song_file)
|
||||||
|
album_art_url = posixpath.join(base_url, album, "cover.jpg")
|
||||||
|
|
||||||
|
try:
|
||||||
|
album_art_resp = requests.get(album_art_url)
|
||||||
|
except requests.RequestException:
|
||||||
|
# If any exception occurs, simply give up and show default art.
|
||||||
|
self.drawDefaultAlbumArt()
|
||||||
|
|
||||||
|
if album_art_resp.ok:
|
||||||
|
with open(self.album_art_loc, "wb") as f:
|
||||||
|
f.write(album_art_resp.content)
|
||||||
|
else:
|
||||||
|
self.drawDefaultAlbumArt()
|
||||||
|
|
||||||
|
|
||||||
|
def _getAlbumArtFromFile(self, song_file):
|
||||||
"""
|
"""
|
||||||
A function that extracts the album art from song_file and
|
A function that extracts the album art from song_file and
|
||||||
saves it to self.album_art_loc
|
saves it to self.album_art_loc
|
||||||
"""
|
"""
|
||||||
|
music_dir = os.path.expanduser(
|
||||||
|
art_config.get("music_directory", "~/Music"))
|
||||||
|
|
||||||
song_file_abs = os.path.join(MUSICDIR, song_file)
|
song_file_abs = os.path.join(music_dir, song_file)
|
||||||
|
|
||||||
process = (
|
process = (
|
||||||
ffmpeg
|
ffmpeg
|
||||||
@ -332,6 +371,10 @@ class Player:
|
|||||||
try:
|
try:
|
||||||
process.run(quiet=True, overwrite_output=True)
|
process.run(quiet=True, overwrite_output=True)
|
||||||
except ffmpeg._run.Error:
|
except ffmpeg._run.Error:
|
||||||
|
self.drawDefaultAlbumArt()
|
||||||
|
|
||||||
|
|
||||||
|
def drawDefaultAlbumArt(self):
|
||||||
foregroundCol = "#D8DEE9"
|
foregroundCol = "#D8DEE9"
|
||||||
backgroundCol = "#262A33"
|
backgroundCol = "#262A33"
|
||||||
|
|
||||||
@ -413,8 +456,6 @@ class Player:
|
|||||||
self.selected_song = int(song["pos"])
|
self.selected_song = int(song["pos"])
|
||||||
|
|
||||||
self.album, self.artist, self.title = self.getSongInfo(song)
|
self.album, self.artist, self.title = self.getSongInfo(song)
|
||||||
self.last_song = song
|
|
||||||
|
|
||||||
self.getAlbumArt(song["file"])
|
self.getAlbumArt(song["file"])
|
||||||
self.last_song = song
|
self.last_song = song
|
||||||
|
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
[player]
|
[player]
|
||||||
music_directory = ~/Music
|
|
||||||
font_width = 11
|
font_width = 11
|
||||||
font_height = 24
|
font_height = 24
|
||||||
image_method = pixcat
|
|
||||||
volume_step = 5
|
volume_step = 5
|
||||||
auto_close = false
|
auto_close = false
|
||||||
album_art_only = false
|
album_art_only = false
|
||||||
show_playlist = true
|
show_playlist = true
|
||||||
|
|
||||||
|
[art]
|
||||||
|
image_method = pixcat
|
||||||
|
music_directory = ~/Music
|
||||||
|
# http_base_url = http://localhost:6667/cover-art
|
||||||
|
|
||||||
[mpd]
|
[mpd]
|
||||||
host = localhost
|
host = localhost
|
||||||
|
Reference in New Issue
Block a user