-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmitty.py
More file actions
122 lines (98 loc) · 3.53 KB
/
mitty.py
File metadata and controls
122 lines (98 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python3
# Import libraries
from mpd import MPDClient
import platform
import sys
import subprocess
import os
import ffmpeg
import hashlib
mm_Client = MPDClient()
mm_Client.timeout = None
mm_Client.idletimeout = None
mm_Client.connect("127.0.0.1", 6600)
global mm_Manner
def mm_Help():
help_text = "Help goes here"
def mm_AboutProgram():
about = "Mitty - MPD client that's like Periwinkle but not"
def mm_SummonNotification(title, subtitle, cover=None):
if mm_Manner == "dbus":
obj = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
obj = dbus.Interface(obj, "org.freedesktop.Notifications")
obj.Notify("",
0,
"", # Picture of the notification
title, # Title
subtitle, # Subtitle
[],
{"urgency": 0}, 10000) # Urgency
elif mm_Manner == "win10":
newToast = Toast()
try:
newToast.AddImage(ToastDisplayImage.fromPath(cover))
except TypeError:
pass
if subtitle=="":
newToast.text_fields = [title]
else:
newToast.text_fields = [title, subtitle]
newToast.on_activated = lambda _: print('Toast clicked!')
mm_Toast.show_toast(newToast)
def mm_SaveAlbumCover(filename, output):
try:
out, err = (
ffmpeg
.input(filename)
.output(output, an=None, vf='scale=256:256', vframes=1, format='image2', vcodec='png')
.run(capture_stdout=True, capture_stderr=True)
)
except ffmpeg.Error as e:
print("FFmpeg stdout:")
print(e.stdout.decode('utf-8'))
print("FFmpeg stderr:")
print(e.stderr.decode('utf-8'))
def mm_Refresh():
if mm_Client.currentsong() == {}:
return False
else:
return True
# Import different notification libraries for each operating system
if platform.system() == "Linux":
mm_Manner = "dbus"
mm_HomeDir = os.environ.get("HOME")
import dbus
elif platform.system() == "Windows" and platform.release() == "10" or platform.release() == "11":
mm_Manner = "win10"
mm_HomeDir = os.environ.get("USERPROFILE")
from windows_toasts import Toast, WindowsToaster, ToastDisplayImage
mm_Toast = WindowsToaster("Mitty")
else:
print("You're running on an unsupported platform. Exiting.")
exit()
mm_DirectoryAlbumCover=os.path.join(mm_HomeDir, "mitty")
mm_MusicDir=os.path.join(mm_HomeDir, "Music")
if os.path.exists(mm_DirectoryAlbumCover) == False:
os.mkdir(mm_DirectoryAlbumCover)
print(f"Directory created: {mm_DirectoryAlbumCover}")
try:
mm_CurrentSong = f"{mm_Client.currentsong()['file']}"
except KeyError:
mm_CurrentSong = ""
if mm_CurrentSong != "":
mm_CurrentSongDirectory = os.path.join(mm_MusicDir, mm_CurrentSong)
mm_CurrentSongDirectory = mm_CurrentSongDirectory.replace('\\', '/')
mm_HashedName = hashlib.sha256(mm_CurrentSong.encode('utf-8')).hexdigest()
mm_OutputToSave = os.path.join(mm_DirectoryAlbumCover, f"{mm_HashedName}.png")
if os.path.exists(mm_OutputToSave) == False:
mm_SaveAlbumCover(mm_CurrentSongDirectory, mm_OutputToSave)
else:
mm_CurrentSongDirectory, mm_OutputToSave = "", ""
if mm_Refresh():
mm_SummonNotification(
f"[{mm_Client.status()['state']}] {mm_Client.currentsong()['title']}", # Title
f"{mm_Client.currentsong()['artist']}\n{mm_Client.currentsong()['album']} ({mm_Client.currentsong()['date']})", # Subtitle
mm_OutputToSave
)
else:
mm_SummonNotification("Stopped", "")