-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwalyrics
executable file
·117 lines (99 loc) · 5.58 KB
/
walyrics
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
#!/usr/bin/python3
from PIL import Image, ImageDraw, ImageFont
from time import sleep
import gi
gi.require_version('Gtk', '3.0')
from dotenv import get_key
from os import system, getlogin
from gi.repository import Playerctl, GLib
import lyricsgenius
from random import randint
from imp import load_source
from requests import get
conf = load_source('config', f'/home/{getlogin()}/.config/walyrics/config.py')
system('clear')
apikey = get_key(dotenv_path=conf.envpath, key_to_get="GENIUS_APIKEY")
genius = lyricsgenius.Genius(apikey)
player = Playerctl.Player()
prev_song = ''
def on_metadata(player, metadata):
global prev_song
global conf
try:
image = Image.open(conf.wallpaper)
except FileNotFoundError:
system(f'notify-send "Wallpaper {conf.wallpaper} not found. Is it a path mistake??"')
exit(1)
if 'xesam:artist' in metadata.keys() and 'xesam:title' in metadata.keys() and metadata['xesam:title'] != prev_song:
if conf.show_thumbs == True:
thumb_data = get(metadata['mpris:artUrl'])
with open(f"/tmp/{metadata['mpris:artUrl'].split('/')[-1]}.png", "wb") as handle:
handle.write(thumb_data.content)
handle.close()
thumb = Image.open(f"/tmp/{metadata['mpris:artUrl'].split('/')[-1]}.png")
thumb = thumb.resize((conf.thumbsize, conf.thumbsize))
thumb.save(f"/tmp/{metadata['mpris:artUrl'].split('/')[-1]}.png")
prev_song = metadata['xesam:title']
song = genius.search_song(metadata['xesam:title'], metadata['xesam:artist'][0])
try:
lyrics = song.lyrics[len(metadata['xesam:title'])+7:-5] # lyrics len + 7 for first useless part "Lyrics" and Remove last 5 elements because of an error caused by the API [it was also adding Embed to lyrics]
if len(lyrics) <= conf.charlimit:
lyrics = song.lyrics[len(metadata['xesam:title'])+7:-5] # lyrics len + 7 for first useless part "Lyrics" and Remove last 5 elements because of an error caused by the API [it was also adding Embed to lyrics]
else:
lyrics = f"Song's lyrics length exceeds your desired maximum char size ({conf.charlimit})"
except AttributeError:
lyrics = f"Cannot fetch {metadata['xesam:title']}'s lyrics... It is probably because of it has not been added to Genius Lyrics's list yet."
draw = ImageDraw.Draw(image)
prev_lyrics = lyrics
fname = randint(1,conf.randlimit)
if conf.marquee != True:
draw.text((conf.offset_x, conf.offset_y), lyrics, font=ImageFont.truetype(conf.fontpath, conf.fontsize), fill=conf.fontcolor ,align='left')
if conf.show_thumbs == True:
thumb = Image.open(f"/tmp/{metadata['mpris:artUrl'].split('/')[-1]}.png")
image.paste(thumb, (image.width-conf.thumbsize-conf.offset_x, conf.offset_y))
if conf.persist_wallpapers == False:
system('rm /tmp/walyrics*.png')
image.save(f'/tmp/walyrics{fname}.png')
if conf.cinnamon != True:
system(f'feh --bg-fill /tmp/walyrics{fname}.png')
else:
system(f"gsettings set org.cinnamon.desktop.background picture-uri 'file:///tmp/walyrics{fname}.png'")
else:
image.save(f'{conf.persistent_wallpapers_dir}/walyrics{fname}.png')
if conf.cinnamon != True:
system(f'feh --bg-fill {conf.persistent_wallpapers_dir}/walyrics{fname}.png')
else:
system(f"gsettings set org.cinnamon.desktop.background picture-uri 'file://{conf.persistent_wallpapers_dir}/walyrics{fname}.png'")
else:
tempheight = image.height
for i in range(image.height // conf.fontsize):
del draw
image = Image.open(conf.wallpaper)
draw = ImageDraw.Draw(image)
draw.text((conf.offset_x, conf.offset_y - (i * conf.fontsize)), lyrics, font=ImageFont.truetype(conf.fontpath, conf.fontsize), fill=conf.fontcolor ,align='left')
if conf.show_thumbs == True:
thumb = Image.open(f"/tmp/{metadata['mpris:artUrl'].split('/')[-1]}.png")
image.paste(thumb, (image.width-conf.thumbsize-conf.offset_x, conf.offset_y))
del thumb
if conf.persist_wallpapers == False:
system('rm /tmp/walyrics*.png')
image.save(f'/tmp/walyrics{fname}.png')
if conf.cinnamon != True:
system(f'feh --bg-fill /tmp/walyrics{fname}.png')
else:
system(f"gsettings set org.cinnamon.desktop.background picture-uri 'file:///tmp/walyrics{fname}.png'")
del image
else:
image.save(f'{conf.persistent_wallpapers_dir}/walyrics{fname}.png')
if conf.cinnamon != True:
system(f'feh --bg-fill {conf.persistent_wallpapers_dir}/walyrics{fname}.png')
else:
system(f"gsettings set org.cinnamon.desktop.background picture-uri 'file://{conf.persistent_wallpapers_dir}/walyrics{fname}.png'")
del image
sleep(conf.marquee_sleep)
# system(f'feh --bg-fill {conf.wallpaper}')
if i == tempheight // conf.fontsize - 1:
i=0
player.connect('metadata', on_metadata)
main = GLib.MainLoop()
main.run()