-
Notifications
You must be signed in to change notification settings - Fork 11
/
scrobbyl.py
executable file
·69 lines (60 loc) · 1.71 KB
/
scrobbyl.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
#!/usr/bin/env python
import sys
import os
import subprocess
import json
import echonest
import time
def fingerprint(file):
platform = os.uname()[0]
if platform == "Darwin":
codegen = "./ext/codegen.Darwin"
path = ".:"+os.getenv("PATH")
elif platform == "Linux":
codegen = "./ext/codegen.Linux-i686"
path = os.getenv("PATH")
proclist = [codegen, os.path.abspath(file), "0", "20"]
p = subprocess.Popen(proclist, env={"PATH":path}, stdout=subprocess.PIPE)
code = p.communicate()[0]
return json.loads(code)
def main(file):
statusfile = os.path.expanduser("~/.scrobbyl")
lines = []
if os.path.exists(statusfile):
fp = open(statusfile, "r")
lines = fp.readlines()
fp.close()
lasttime = 0
lastartist = ""
lasttrack = ""
if len(lines) == 3:
lasttime = int(lines[0])
lastartist = lines[1]
lasttrack = lines[2]
f = fingerprint(file)
code = f[0]["code"]
song = echonest.fp_lookup(code)
echonest.pp(song)
if "response" in song and "status" in song["response"] \
and song["response"]["status"]["message"] == "Success" \
and len(song["response"]["songs"]) > 0:
track = song["response"]["songs"][0]["title"]
artist = song["response"]["songs"][0]["artist_name"]
now = time.time()
print (now-lasttime)
if now - lasttime < 100:
# Only scrobble if we've just been playing
if lasttrack != "" and lasttrack != track:
print "Last track was",lasttrack,"now",track,", scrobbling"
else:
print "same song"
else:
print "too long since we last did it,", now-lasttime
fp = open(statusfile, "w")
fp.write("%d\n%s\n%s" % (now, artist, track))
fp.close()
if __name__=="__main__":
if len(sys.argv) < 2:
print >>sys.stderr, "usage: %s <file>" % sys.argv[0]
sys.exit(0)
main(sys.argv[1])