-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlastfm.py
executable file
·133 lines (105 loc) · 4.34 KB
/
lastfm.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/python
# vbrfs - A real-time FLAC to mp3-vbr fuse filesystem written in python.
# scrobble.py - To upload your listens to last.fm
# Copyright (c) 2013,2014 Jonathan Wedell <[email protected]> (author)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from __future__ import print_function
import os
import webbrowser
from hashlib import md5
import requests
# Store the api_key and secret using ROT26 encryption to prevent theft
api_key = 'f9629d0d0a7d28285d07878c400db123'
secret = 'a58577f64c88d9dab10ec8214e1bb36a'
session_key = None
# Configuration parameters
request_timeout = 5
request_url = "http://ws.audioscrobbler.com/2.0/"
def add_secret(payload):
url = u""
for key in sorted(payload):
url += u"%s%s" % (key, payload[key])
url += secret
payload['api_sig'] = unicode(md5(url).hexdigest(), "utf-8")
return
def scrobble(tags, init_time):
payload = {'api_key': api_key, 'method': 'track.scrobble',
'sk': session_key}
# We can't scrobble if we don't know the artist and the track
try:
payload['artist'] = tags['artist']
payload['track'] = tags['title']
except KeyError:
return (1, "Missing artist or title. Artist: %s Title:"
"%s" % (tags.get('artist', "?"), tags.get('title', "?")))
# Add optional tags
if tags.get('album', None):
payload['album'] = tags['album']
if tags.get('tracknumber', None):
payload['tracknumber'] = tags['tracknumber']
if tags.get('duration', None):
payload['duration'] = int(tags['duration'])
# Subtract duration to get start time?
payload['timestamp'] = int(init_time)
# Finish formatting the request
add_secret(payload)
payload['format'] = 'json'
# Scrobble
try:
r = requests.post(request_url, params=payload, timeout=request_timeout)
except requests.Timeout:
return (-1, "Timeout occured while attempting to connect to last.fm")
# Process the request, look for errors
if r.status_code != 200:
return (r.status_code, r.text)
json = r.json()
# Check for an error message
try:
if json.get('error', None):
return (int(json['error']), json['message'])
except KeyError:
pass
finally:
return (200, json)
def initialize():
print("Authorizing VBRFS with last.fm...")
# First we need an auth token
payload = {'api_key': api_key, 'format': 'json', 'method': 'auth.gettoken'}
r = requests.get(request_url, params=payload, timeout=10)
token = r.json()['token']
# Then we send them to the website to approve
url = "http://www.last.fm/api/auth/?api_key=%s&token=%s" % (api_key, token)
webbrowser.open(url, new=1)
print("Please authorize VBRFS to scrobble using the page that should have "
"opened in your web browser.\nIf you do not see a page, please go "
"to: http://www.last.fm/api/auth/?api_key=%s&token=%s\n" %
(api_key, token))
raw_input("Press enter once you have authorized VBRFS: ")
# Then we can get the session key
payload = {'api_key': api_key, 'method': 'auth.getSession', 'token': token}
add_secret(payload)
payload['format'] = 'json'
r = requests.get(request_url, params=payload, timeout=10)
# Save the session key for future use
gen_session_key = r.json()['session']['key']
open(os.path.expanduser("~/.vbrfs_lastfm_key"), "w").write(gen_session_key)
print("Done!")
return gen_session_key
if (__name__ == '__main__' or
not os.path.isfile(os.path.expanduser("~/.vbrfs_lastfm_key"))):
session_key = initialize()
else:
session_key = open(os.path.expanduser("~/.vbrfs_lastfm_key"), "r").read()