-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathnowplaying.py
More file actions
executable file
·102 lines (77 loc) · 2.55 KB
/
nowplaying.py
File metadata and controls
executable file
·102 lines (77 loc) · 2.55 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
#!/usr/bin/env python3
"""
Authors: Amr Hassan <amr.hassan@gmail.com>
and hugovk <https://github.com/hugovk>
# Hacked from https://github.com/hugovk/bbcscrobbler
"""
import argparse
import os
import time
from sys import platform as _platform
import pylast
API_KEY = "8fe0d07b4879e9cd6f8d78d86a8f447c"
API_SECRET = "debb11ad5da3be07d06fddd8fe95cc42"
SESSION_KEY_FILE = os.path.join(os.path.expanduser("~"), ".session_key")
last_output = None
def output(text):
text = str(text)
global last_output
if last_output == text:
return
else:
last_output = text
print(text)
# Windows:
if _platform == "win32":
if "&" in text:
text = text.replace("&", "^&") # escape ampersand
os.system("title " + text)
# Linux, OS X or Cygwin:
elif _platform in ["linux", "linux2", "darwin", "cygwin"]:
import sys
sys.stdout.write("\x1b]2;" + text + "\x07")
def duration(track):
return int(track.track.get_duration()) / 1000
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Loopy thing to show what a Last.fm user is now playing.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("user", nargs="?", default="hvk", help="User to check")
args = parser.parse_args()
network = pylast.LastFMNetwork(API_KEY, API_SECRET)
if not os.path.exists(SESSION_KEY_FILE):
skg = pylast.SessionKeyGenerator(network)
url = skg.get_web_auth_url()
print(f"Please authorize the scrobbler to scrobble to your account: {url}\n")
import webbrowser
webbrowser.open(url)
while True:
try:
session_key = skg.get_web_auth_session_key(url)
fp = open(SESSION_KEY_FILE, "w")
fp.write(session_key)
fp.close()
break
except pylast.WSError:
time.sleep(1)
else:
session_key = open(SESSION_KEY_FILE).read()
network.session_key = session_key
user = network.get_user(args.user)
out = "Tuned in to %s" % args.user
output(out)
print("-" * len(out))
playing_track = None
while True:
try:
new_track = user.get_now_playing()
# print(new_track)
# A new, different track
if new_track != playing_track:
playing_track = new_track
output(playing_track)
except Exception as e:
print("Error: %s" % repr(e))
time.sleep(15)
# End of file