File: lastfm.py

package info (click to toggle)
lastfmproxy 1.3b-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 240 kB
  • ctags: 75
  • sloc: python: 1,015; sh: 109; makefile: 9
file content (97 lines) | stat: -rw-r--r-- 3,123 bytes parent folder | download | duplicates (3)
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

import httpclient
import time
import string
import playlist
import sys

class lastfm:

    def __init__(self):
        self.version = "1.3.1.1"
        self.platform = "linux"
        self.host = "ws.audioscrobbler.com"
        self.port = 80
        self.info = None
        self.playlist = playlist.playlist()
        self.debug = 0

    def parselines(self, str):
        res = {}
        vars = string.split(str, "\n")
        numerrors = 0
        for v in vars:
            x = string.split(string.rstrip(v), "=", 1)
            if len(x) == 2:
                res[x[0]] = x[1]
            elif x != [""]:
                print "(urk?", x, ")"
                numerrors = numerrors + 1
            if numerrors > 5:
                print "Too many errors parsing response."
                return {}
        return res

    def connect(self, username, password):

        s = httpclient.httpclient(self.host, self.port)
        s.req("/radio/handshake.php?version=" + self.version + "&platform=" + self.platform + "&username=" + username + "&passwordmd5=" + password + "&language=en&player=lastfmproxy")

        self.info = self.parselines(s.response)


    def getplaylist(self):

        if self.debug:
            sys.stderr.write("Fetching playlist...\n")

        s = httpclient.httpclient(self.info["base_url"])
        s.req(self.info["base_path"] + "/xspf.php?sk=" + self.info["session"] + "&discovery=0&desktop=" + self.version)

        self.playlist.parse(s.response)

        # debug
        if self.debug:
            if len(self.playlist.data.tracks):
                f = open("playlist.xspf", "w")
                f.write(s.response)
                f.close()
            elif False:
                print "No playlist?? Using cached version instead..."
                f = open("playlist.xspf", "r")
                cache = f.read()
                f.close()
                self.playlist.parse(cache)
                self.playlist.pos = 0 #len(self.playlist.data.tracks) -1

        return len(self.playlist.data.tracks)

    def command(self, cmd):
        # commands = skip, love, ban, rtp, nortp

        if self.debug:
            sys.stderr.write("command " + cmd + "\n")

        s = httpclient.httpclient(self.info["base_url"], 80)
        s.req(self.info["base_path"] + "/control.php?command=" + cmd + "&session=" + self.info["session"])
        res = self.parselines(s.response)
        
        if not res.has_key("response") or res["response"] != "OK":
            sys.stderr.write("command " + cmd + " returned:" + repr(res) + "\n")
        
        return res

    def changestation(self, url):
        
        if self.debug:
            sys.stderr.write("changestation " + url + "\n")
        
        s = httpclient.httpclient(self.info["base_url"], 80)
        s.req(self.info["base_path"] + "/adjust.php?session=" + self.info["session"] + "&url=" + url)
        res = self.parselines(s.response)

        if not res.has_key("response") or res["response"] != "OK":
            sys.stderr.write("changestation " + url + " returned:" + repr(res) + "\n")
        
        return res