1
# Copyright (c) 2001-2009 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
Chop up shoutcast stream into MP3s and metadata, if available.
8
from twisted.web import http
9
from twisted import copyright
12
class ShoutcastClient(http.HTTPClient):
14
Shoutcast HTTP stream.
16
Modes can be 'length', 'meta' and 'mp3'.
18
See U{http://www.smackfu.com/stuff/programming/shoutcast.html}
19
for details on the protocol.
22
userAgent = "Twisted Shoutcast client " + copyright.version
24
def __init__(self, path="/"):
26
self.got_metadata = False
31
def connectionMade(self):
32
self.sendCommand("GET", self.path)
33
self.sendHeader("User-Agent", self.userAgent)
34
self.sendHeader("Icy-MetaData", "1")
37
def lineReceived(self, line):
38
# fix shoutcast crappiness
39
if not self.firstLine and line:
40
if len(line.split(": ", 1)) == 1:
41
line = line.replace(":", ": ", 1)
42
http.HTTPClient.lineReceived(self, line)
44
def handleHeader(self, key, value):
45
if key.lower() == 'icy-metaint':
46
self.metaint = int(value)
47
self.got_metadata = True
49
def handleEndHeaders(self):
50
# Lets check if we got metadata, and set the
51
# appropriate handleResponsePart method.
53
# if we have metadata, then it has to be parsed out of the data stream
54
self.handleResponsePart = self.handleResponsePart_with_metadata
56
# otherwise, all the data is MP3 data
57
self.handleResponsePart = self.gotMP3Data
59
def handleResponsePart_with_metadata(self, data):
60
self.databuffer += data
61
while self.databuffer:
62
stop = getattr(self, "handle_%s" % self.metamode)()
66
def handle_length(self):
67
self.remaining = ord(self.databuffer[0]) * 16
68
self.databuffer = self.databuffer[1:]
69
self.metamode = "meta"
72
if len(self.databuffer) > self.metaint:
73
self.gotMP3Data(self.databuffer[:self.metaint])
74
self.databuffer = self.databuffer[self.metaint:]
75
self.metamode = "length"
79
def handle_meta(self):
80
if len(self.databuffer) >= self.remaining:
82
data = self.databuffer[:self.remaining]
83
self.gotMetaData(self.parseMetadata(data))
84
self.databuffer = self.databuffer[self.remaining:]
89
def parseMetadata(self, data):
91
for chunk in data.split(';'):
92
chunk = chunk.strip().replace("\x00", "")
95
key, value = chunk.split('=', 1)
96
if value.startswith("'") and value.endswith("'"):
98
meta.append((key, value))
101
def gotMetaData(self, metadata):
102
"""Called with a list of (key, value) pairs of metadata,
103
if metadata is available on the server.
105
Will only be called on non-empty metadata.
107
raise NotImplementedError, "implement in subclass"
109
def gotMP3Data(self, data):
110
"""Called with chunk of MP3 data."""
111
raise NotImplementedError, "implement in subclass"