动态读取mpd的歌曲目录设置

master
yutent 2023-08-22 20:43:40 +08:00
parent 5312642c3e
commit b27c816047
4 changed files with 45 additions and 23 deletions

18
main.py
View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3
import gi, sys, os, threading, time
import gi, sys, os, threading, time, re
# import dbus
# import dbus.service, dbus.mainloop.glib
from pprint import pprint as print
@ -16,6 +16,7 @@ from about_app import AboutWindow
from mpd.base import MPDClient
app_id = 'fun.wkit.sonist'
home_dir = os.getenv('HOME')
def run_async(func):
@ -27,6 +28,17 @@ def run_async(func):
return wrapper
def get_music_dir():
with open(f'{home_dir}/.mpd/mpd.conf', 'r') as f:
data = f.read()
matches = re.search('music_directory\s*"(.*)"', data).groups()
if len(matches) > 0:
return matches[0]
return '/data/music'
class Application(Gtk.Application):
__gsignals__ = {
@ -44,6 +56,8 @@ class Application(Gtk.Application):
self.mpd = MPDClient()
self.music_dir = get_music_dir()
self.connect('window-removed', self.on_window_removed)
@ -97,7 +111,7 @@ class Application(Gtk.Application):
self.about = AboutWindow()
self.add_window(self.window)
self.window.show_all()
self.about.show_all()
# self.about.show_all()
self.ping()

View File

@ -35,6 +35,8 @@ class ImageButton(Gtk.Button):
}}
#ImageButton:hover {{
background-color: rgba(255,255,255,.1);
outline: transparent;
box-shadow:none;
}}
"""
css_provider.load_from_data(style.encode('UTF-8'))

View File

@ -32,8 +32,10 @@ class Timebar(Gtk.Fixed):
self.curr = Gtk.Label()
self.duration = Gtk.Label()
self.curr.set_name('text')
self.duration.set_justify(Gtk.Justification.RIGHT)
self.duration.set_xalign(1)
self.duration.set_name('text')
self.slider.connect('change-value', self.on_timeupdate)

View File

@ -80,6 +80,7 @@ class SonistWindow(Gtk.Window):
# self.title_box = TextBox(256, 20)
self.title_box = Gtk.Label()
self.title_box.set_name('text')
self.title_box.set_text('mpd loading...')
layout.put(self.title_box, 27, 244)
@ -129,6 +130,9 @@ class SonistWindow(Gtk.Window):
background-size: 100% 100%;
background-position: center;
}}
#text {{
color: #f2f5fc;
}}
"""
# 加载CSS样式
css_provider = Gtk.CssProvider()
@ -241,32 +245,32 @@ class SonistWindow(Gtk.Window):
self.ctrl_box.toggle_mode_btn(mode = 'random')
# 更新歌曲信息
self.title_box.set_text("%s - %s" % (song.get('artist'), song.get('title')))
if played != 'stop':
# 更新歌曲信息
self.title_box.set_text("%s - %s" % (song.get('artist'), song.get('title')))
self.update_playtime()
self.update_playtime()
filepath = f"./album/{song['title']}.png"
songpath = f"/data/music/{song['file']}"
filepath = f"./album/{song['title']}.png"
songpath = f"{self.app.music_dir}/{song['file']}"
if os.path.isfile(filepath):
self.update_album(filepath)
else:
if os.path.isfile(filepath):
self.update_album(filepath)
else:
id3 = mutagen.File(songpath)
id3 = mutagen.File(songpath)
try:
if id3.tags.get('APIC:'):
pic = id3.tags['APIC:']
elif len(id3.pictures) > 0:
pic = id3.pictures[0]
try:
if id3.tags.get('APIC:'):
pic = id3.tags['APIC:']
elif len(id3.pictures) > 0:
pic = id3.pictures[0]
if pic is not None:
album = pic_to_pixbuf(pic)
self.update_album(album)
except:
pass
if pic is not None:
album = pic_to_pixbuf(pic)
self.update_album(album)
except:
pass
def update_album(self, filepath):