-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
np.py
executable file
·113 lines (94 loc) · 2.98 KB
/
np.py
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
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import os
import shlex
import sys
import time
import pylast
from termcolor import colored # pip install termcolor
from mylast import lastfm_network, lastfm_username
# Show my now playing song, or that of a given username
# Prerequisites: mylast.py, pyLast
last_output = None
def output(text: str, type: str | None = None) -> None:
global last_output
if last_output == text:
return
else:
last_output = text
if type == "error":
print(colored(text, "red"))
else:
print(text)
def say(thing: str) -> None:
cmd = f"say {shlex.quote(str(thing))}"
os.system(cmd)
def is_track_loved(track: pylast.Track) -> str:
"""
Input: Track
If loved, return track string with a heart
else return track string
"""
text = f"{track.artist} - {colored(track.title, attrs=['bold'])}"
try:
if track:
loved = track.get_userloved()
if loved:
heart = colored("❤", "red")
return f"{text} {heart}"
except (
# KeyError,
# pylast.MalformedResponseError,
# pylast.NetworkError,
pylast.WSError,
) as e:
output("is_track_loved", "error")
output(f"Error: {repr(e)}", "error")
print(dir(e))
output(e.message, "error")
return text
def main() -> None:
parser = argparse.ArgumentParser(
description="Show my now playing song, or that of a given username",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"username",
nargs="?",
default=lastfm_username,
help="Show now playing of this username",
)
parser.add_argument("--loop", action="store_true", help="Loop until Ctrl-C")
parser.add_argument("--say", action="store_true", help="Announcertron 4000")
args = parser.parse_args()
if not args.loop:
now_playing = lastfm_network.get_user(args.username).get_now_playing()
if not now_playing:
return
output(is_track_loved(now_playing))
else:
last_played = None
while True:
try:
now_playing = lastfm_network.get_user(args.username).get_now_playing()
# output("last:", last_played)
# output("now: ", now_playing)
if now_playing != last_played:
last_played = now_playing
if now_playing:
output(is_track_loved(now_playing))
if args.say:
say(now_playing)
time.sleep(15)
except (
# KeyError,
pylast.MalformedResponseError,
pylast.NetworkError,
pylast.WSError,
) as e:
output(f"Error: {e}", "error")
except KeyboardInterrupt:
sys.exit()
if __name__ == "__main__":
main()