1 | #if 0 |
---|
2 | # ----------------------------------------------------------------------- |
---|
3 | # $Id: webradioinfo.py,v 1.7 2003/07/02 11:17:29 the_krow Exp $ |
---|
4 | # ----------------------------------------------------------------------- |
---|
5 | # $Log: webradioinfo.py,v $ |
---|
6 | # Revision 1.7 2003/07/02 11:17:29 the_krow |
---|
7 | # language is now part of the table key |
---|
8 | # |
---|
9 | # Revision 1.6 2003/06/30 13:17:18 the_krow |
---|
10 | # o Refactored mediainfo into factory, synchronizedobject |
---|
11 | # o Parsers now register directly at mmpython not at mmpython.mediainfo |
---|
12 | # o use mmpython.Factory() instead of mmpython.mediainfo.get_singleton() |
---|
13 | # o Bugfix in PNG parser |
---|
14 | # o Renamed disc.AudioInfo into disc.AudioDiscInfo |
---|
15 | # o Renamed disc.DataInfo into disc.DataDiscInfo |
---|
16 | # |
---|
17 | # Revision 1.5 2003/06/24 14:37:17 dischi |
---|
18 | # small fix |
---|
19 | # |
---|
20 | # Revision 1.4 2003/06/24 13:52:06 the_krow |
---|
21 | # 302 Handling is done by urllib so no further code for this is |
---|
22 | # needed. Thanks to den_RDC for setting up a server to test it. |
---|
23 | # |
---|
24 | # Revision 1.3 2003/06/24 13:06:46 the_krow |
---|
25 | # stream is being closed in fail-cases. |
---|
26 | # |
---|
27 | # Revision 1.2 2003/06/24 12:59:33 the_krow |
---|
28 | # Added Webradio. |
---|
29 | # Added Stream Type to mediainfo |
---|
30 | # |
---|
31 | # Revision 1.1 2003/06/23 22:23:16 the_krow |
---|
32 | # First Import. Not yet integrated. |
---|
33 | # |
---|
34 | # |
---|
35 | # ----------------------------------------------------------------------- |
---|
36 | # MMPython - Media Metadata for Python |
---|
37 | # Copyright (C) 2003 Thomas Schueppel, Dirk Meyer |
---|
38 | # |
---|
39 | # This program is free software; you can redistribute it and/or modify |
---|
40 | # it under the terms of the GNU General Public License as published by |
---|
41 | # the Free Software Foundation; either version 2 of the License, or |
---|
42 | # (at your option) any later version. |
---|
43 | # |
---|
44 | # This program is distributed in the hope that it will be useful, but |
---|
45 | # WITHOUT ANY WARRANTY; without even the implied warranty of MER- |
---|
46 | # CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
47 | # Public License for more details. |
---|
48 | # |
---|
49 | # You should have received a copy of the GNU General Public License along |
---|
50 | # with this program; if not, write to the Free Software Foundation, Inc., |
---|
51 | # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
52 | # |
---|
53 | # ----------------------------------------------------------------------- |
---|
54 | #endif |
---|
55 | |
---|
56 | import urlparse |
---|
57 | import string |
---|
58 | import urllib |
---|
59 | |
---|
60 | from mmpython import mediainfo |
---|
61 | import mmpython |
---|
62 | |
---|
63 | _print = mediainfo._debug |
---|
64 | |
---|
65 | # http://205.188.209.193:80/stream/1006 |
---|
66 | |
---|
67 | ICY_tags = { 'title': 'icy-name', |
---|
68 | 'genre': 'icy-genre', |
---|
69 | 'bitrate': 'icy-br', |
---|
70 | 'caption': 'icy-url', |
---|
71 | } |
---|
72 | |
---|
73 | class WebRadioInfo(mediainfo.MusicInfo): |
---|
74 | def __init__(self, url): |
---|
75 | mediainfo.MusicInfo.__init__(self) |
---|
76 | tup = urlparse.urlsplit(url) |
---|
77 | scheme, location, path, query, fragment = tup |
---|
78 | if scheme != 'http': |
---|
79 | self.valid = 0 |
---|
80 | return |
---|
81 | # Open an URL Connection |
---|
82 | fi = urllib.urlopen(url) |
---|
83 | |
---|
84 | # grab the statusline |
---|
85 | self.statusline = fi.readline() |
---|
86 | try: |
---|
87 | statuslist = string.split(self.statusline) |
---|
88 | except ValueError: |
---|
89 | # assume it is okay since so many servers are badly configured |
---|
90 | statuslist = ["ICY", "200"] |
---|
91 | |
---|
92 | if statuslist[1] != "200": |
---|
93 | self.valid = 0 |
---|
94 | if fi: |
---|
95 | fi.close() |
---|
96 | return |
---|
97 | |
---|
98 | self.valid = 1 |
---|
99 | self.type = 'audio' |
---|
100 | self.subtype = 'mp3' |
---|
101 | # grab any headers for a max of 10 lines |
---|
102 | linecnt = 0 |
---|
103 | tab = {} |
---|
104 | lines = fi.readlines(512) |
---|
105 | for linecnt in range(0,11): |
---|
106 | icyline = lines[linecnt] |
---|
107 | icyline = icyline.rstrip('\r\n') |
---|
108 | if len(icyline) < 4: |
---|
109 | break |
---|
110 | cidx = icyline.find(':') |
---|
111 | if cidx != -1: |
---|
112 | # break on short line (ie. really should be a blank line) |
---|
113 | # strip leading and trailing whitespace |
---|
114 | tab[icyline[:cidx].strip()] = icyline[cidx+2:].strip() |
---|
115 | if fi: |
---|
116 | fi.close() |
---|
117 | self.appendtable('ICY', tab) |
---|
118 | self.tag_map = { ('ICY', 'en') : ICY_tags } |
---|
119 | # Copy Metadata from tables into the main set of attributes |
---|
120 | for k in self.tag_map.keys(): |
---|
121 | map(lambda x:self.setitem(x,self.gettable(k[0],k[1]),self.tag_map[k][x]), self.tag_map[k].keys()) |
---|
122 | self.bitrate = string.atoi(self.bitrate)*1000 |
---|
123 | |
---|
124 | mmpython.registertype( 'text/plain', mediainfo.EXTENSION_STREAM, mediainfo.TYPE_MUSIC, WebRadioInfo ) |
---|
125 | |
---|