def getwindowtitle():
if sys.platform == "win32":
spotify = win32gui.FindWindow('SpotifyMainWindow', None)
windowname = win32gui.GetWindowText(spotify)
elif sys.platform == "darwin":
windowname = ''
try:
command = "osascript getCurrentSong.AppleScript"
windowname = subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8")
except Exception:
pass
else:
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 "—" in windowname:
windowname = windowname.replace("—", "-")
if "Spotify - " in windowname:
windowname = windowname.strip("Spotify - ")
return(windowname)
评论列表
文章目录