Internet radio browser GUI for music/video streams from various directory services.

⌈⌋ branch:  streamtuner2


Changes On Branch xiphjson

Changes In Branch xiphjson Excluding Merge-Ins

This is equivalent to a diff from b9af78503d to d8ffa831d0

2014-05-04
23:14
merge Xiph plugin with JSON query back in, enable per default again check-in: 8a39e17e4b user: mario tags: trunk
23:11
more xiph category reordering Leaf check-in: d8ffa831d0 user: mario tags: xiphjson
18:27
Tie to new json cache API, update categories by themselves (no big "all" download anymore), and add new unwieldy categories check-in: ddb65cf0aa user: mario tags: xiphjson
2014-04-28
00:19
Adapted DMOZ retrieval to changed HTML check-in: a44d5a6e74 user: mario tags: trunk
2014-04-27
23:33
Experimental Xiph.org JSON API (doesn't work) check-in: a1c4d0960a user: mario tags: xiphjson
22:48
Fully replace ahttp.ajax with ahttp.get() wrapper check-in: b9af78503d user: mario tags: trunk
22:19
Python3 support back into trunk check-in: 9ecea4fb26 user: mario tags: trunk

Modified channels/xiph.py from [04e8c2b58d] to [489a9db0fc].

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
134
135
136

















137
138
139
140
141
142
143
144






145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160















161
162
163
164
165

















166
167
168
169
170
171












172
173
174
175


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

































194
195
196
197
























198
199


200
201
202




203
204
205
206
207




















208
209
210
211
212
213
214








215
216
217
218






219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242












































































































































243
244
245
246
247

248
249
250
251
252



253
254
255


256
257
258
259
260
261
262
263
264








265
266
267


268
269
270


271
272
273
274
275




276
277
278


279
280

281
282
283
284




285
286
287


288
289
290



291
292
293
294
295
296







297
298
299


300
301
302
303















































304
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
134
135
136
137
138
139
140





141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157






158
159
160
161
162
163
164
165
166
167
168
169




170
171


















172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204




205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228


229
230



231
232
233
234





235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254







255
256
257
258
259
260
261
262




263
264
265
266
267
268
























269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408





409





410
411
412



413
414









415
416
417
418
419
420
421
422



423
424



425
426





427
428
429
430



431
432


433




434
435
436
437



438
439



440
441
442






443
444
445
446
447
448
449



450
451




452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499




-
+




-
-

+
+
-
+











+



-
-
+
+











-
+

-
-
+
+






-
+

-
+






-
+




















-
-
-
-
-
-
-
-
-
-
-
-
+
-


-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+


-
-
-
+
+
+
+
+
+

-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+

-
-
-
-
-
-
-
-







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
-
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
-
-
-
-
-
+
+
+
-
-
-
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
+
+
-
-
-
+
+
-
-
-
-
-
+
+
+
+
-
-
-
+
+
-
-
+
-
-
-
-
+
+
+
+
-
-
-
+
+
-
-
-
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
#
# api: streamtuner2
# title: Xiph.org
# description: Xiph/ICEcast radio directory
# version: 0.1
# version: 0.3
#
#
# Xiph.org maintains the Ogg streaming standard and Vorbis audio compression
# format, amongst others. The ICEcast server is an alternative to SHOUTcast.
# But it turns out, that Xiph lists only MP3 streams, no OGG. And the directory
# is less encompassing than Shoutcast.
#
# It meanwhile provides a JSOL dump, which is faster to download and process.
# So we'll use that over the older yp.xml. (Sadly it also doesn't output
#
# homepage URLs, listeners, etc.)
#
#



# streamtuner2 modules
from config import conf
from mygtk import mygtk
import ahttp as http
from channels import *
from config import __print__, dbg
import json

# python modules
import re
from xml.sax.saxutils import unescape as entity_decode, escape as xmlentities
import xml.dom.minidom
#from xml.sax.saxutils import unescape as entity_decode, escape as xmlentities
#import xml.dom.minidom



          
# I wonder what that is for                                             ---------------------------------------
class xiph (ChannelPlugin):

        # desc
        api = "streamtuner2"
        module = "xiph"
        title = "Xiph.org"
        version = 0.1
        version = 0.3
        homepage = "http://dir.xiph.org/"
        base_url = "http://dir.xiph.org/"
        yp = "yp.xml"
        #base_url = "http://api.dir.xiph.org/"
        json_url = "http://api.include-once.org/xiph/cache.php"
        listformat = "url/http"
        config = [
           {"name":"xiph_min_bitrate", "value":64, "type":"int", "description":"minimum bitrate, filter anything below", "category":"filter"}
        ]

        # content
        categories = ["all", [],           ]
        categories = [ "pop", "top40" ]
        current = ""
        default = "all"
        default = "pop"
        empty = None
        
        
        # prepare category names
        def __init__(self, parent=None):
            
            self.categories = ["all"]
            self.categories = []
            self.filter = {}
            for main in self.genres:
                if (type(main) == str):
                    id = main.split("|")
                    self.categories.append(id[0].title())
                    self.filter[id[0]] = main
                else:
                    l = []
                    for sub in main:
                        id = sub.split("|")
                        l.append(id[0].title())
                        self.filter[id[0]] = sub
                    self.categories.append(l)
            
            # GUI
            ChannelPlugin.__init__(self, parent)


        # just counts genre tokens, does not automatically create a category tree from it
        def update_categories(self):
            g = {}
            for row in self.streams["all"]:
                for t in row["genre"].split():
                    if g.has_key(t):
                        g[t] += 1
                    else:
                        g[t] = 0
            g = [ [v[1],v[0]] for v in g.items() ]
            g.sort()
            g.reverse()
            for row in g:
                pass
            pass
                __print__( dbg.DATA, '        "' + row[1] + '", #' + str(row[0]) )


        # xml dom node shortcut to text content
        def x(self, entry, name):
            e = entry.getElementsByTagName(name)
            if (e):
                if (e[0].childNodes):
                    return e[0].childNodes[0].data
                    
        # convert bitrate string to integer
        # (also convert "Quality \d+" to pseudo bitrate)
        def bitrate(self, s):
            uu = re.findall("(\d+)", s)
            if uu:
                br = uu[0]
                if br > 10:
                    return int(br)
                else:
                    return int(br * 25.6)
            else:
                return 0

        # downloads stream list from shoutcast for given category
        # downloads stream list from xiph.org for given category
        def update_streams(self, cat, search=""):

            # there is actually just a single category to download,
            # all else are virtual
            if (cat == "all"):
            # With the new JSON cache API on I-O, we can load categories individually:
            params = {}
            if cat:
                params["cat"] = cat.lower()
            if search:
                params["search"] = search
            
                #-- get data
                yp = http.get(self.base_url + self.yp)
                
                #-- extract
                l = []
                __print__( dbg.DATA, "xml.dom.minidom parses yp.xml" )
                for entry in xml.dom.minidom.parseString(yp).getElementsByTagName("entry"):
                    bitrate = self.bitrate(self.x(entry, "bitrate"))
                    if conf.xiph_min_bitrate and bitrate and bitrate >= int(conf.xiph_min_bitrate):
                      l.append({
                        "title": str(self.x(entry, "server_name")),
                        "url": str(self.x(entry, "listen_url")),
                        "format": self.mime_fmt(str(self.x(entry, "server_type"))[6:]),
                        "bitrate": bitrate,
            #-- get data
            data = http.get(self.json_url, params=params)
            #__print__(dbg.DATA, data)
            
            #-- extract
            l = []
            __print__( dbg.PROC, "processing api.dir.xiph.org JSON (via api.include-once.org cache)" )
            data = json.loads(data)
            for e in data.values():
                __print__(dbg.DATA, e)
                bitrate = int(e["bitrate"])
                if conf.xiph_min_bitrate and bitrate and bitrate >= int(conf.xiph_min_bitrate):
                  l.append({
                    "title": e["stream_name"],
                    "url": e["listen_url"],
                    "format": e["type"],
                    "bitrate": int(e["bitrate"]),
                        "channels": str(self.x(entry, "channels")),
                        "samplerate": str(self.x(entry, "samplerate")),
                        "genre": str(self.x(entry, "genre")),
                        "playing": str(self.x(entry, "current_song")),
                        "listeners": 0,
                        "max": 0,              # this information is in the html view, but not in the yp.xml (seems pretty static, we might as well make it a built-in list)
                        "homepage": "",
                      })
                    "genre": e["genre"],
                    "playing": e["current_song"],
                    "listeners": 0,
                    "max": 0,
                    "homepage": (e["homepage"] if ("homepage" in e) else ""),
                  })
                
            # filter out a single subtree
            else:
                rx = re.compile(self.filter.get(cat.lower(), cat.lower()))
                l = []
                for i,row in enumerate(self.streams["all"]):
                    if rx.search(row["genre"]):
                        l.append(row)
            
            # send back the list 
            return l




        genres = [
              "pop",
              [
                  "top40",
                  "90s",
                  "80s",
                  "britpop",
                  "disco",
                  "urban",
                  "party",
                  "mashup",
                  "kpop",
                  "jpop",
                  "lounge",
                  "softpop",
                  "top",
            "scanner", #442
            "rock", #305
            [
              "metal|heavy", #36
            ],
                  "popular",
                  "schlager",
              ],
              "rock",
              [
                  "alternative",
                  "electro",
                  "country",
                  "mixed",
                  "metal",
                  "eclectic",
                  "folk",
                  "anime",
                  "hardcore",
                  "pure"
                  "jrock"
              ],
            "various", #286
            [
              "mixed", #96
            ],
            "pop", #221
            [
              "dance",
              [
                  "electronic",
                  "deephouse",
                  "dancefloor",
                  "elektro"
                  "eurodance"
                  "b",
                  "r",
              ],
              "hits",
              [
              "top40|top|40|top 40", #32
              "charts|hits", #20+4
              "80s", #68
              "90s", #20
                  "russian"
                  "hit",
              "disco", #17
              "remixes", #10
            ],
            "electronic|electro", #33
            [
              "dance", #161
              "house", #106
              "trance", #82
              "techno", #72
              "chillout", #16
              "lounge", #12
            ],
            "alternative", #68
            [
              "college", #32
              "student", #20
              "progressive", #20
            ],
                  "star"
              ],
              "radio",
              [
                  "live",
                  "community",
                  "student",
                  "internet",
                  "webradio",
              ],
              "classic",
              [
                   "classical",
                   "ebu",
                   "vivaldi",
                   "piano",
                   "opera",
                   "classix",
                   "chopin",
                   "renaissance",
                   "classique",
              ],
              "talk",
              [
                  "news",
                  "politics",
                  "medicine",
                  "health"
                  "sport",
                  "education",
                  "entertainment",
                  "podcast",
              ],
            "classical|classic", #58+20
            "live", #57
            "jazz", #42
            [
              "various",
              [
                  "hits",
                  "ruhit",
                  "mega"
              ],
              "house",
              [
                  "lounge",
                  "trance",
                  "techno",
                  "handsup",
                  "gay",
                  "breaks",
                  "dj",
              "electronica",
              ],
              "trance",
              [
                  "clubbing",
                  "electronical"
              ],
              "jazz",
              [
              "blues", #19
            ],
                  "contemporary"
              ],
            "talk|spoken|speak", #41
            [
              "news", #39
              "oldies",
              [
                  "golden",
                  "decades",
              "public", #12
              "info", #5
            ],
            "world|international", #25
            [
                  "info",
                  "70s",
                  "60s"
              ],
              "religious",
              [
                  "spiritual",
                  "inspirational",
                  "christian",
                  "catholic",
                  "teaching",
                  "christmas",
                  "gospel",
              ],
              "music",
              "unspecified",
              "misc",
              "adult",
              "indie",
              [
              "latin", #34
              "reggae", #12
              "indie", #12
              "folk", #9
              "schlager", #14
              "jungle", #13
              "country", #7
                  "reggae",
                  "blues",
                  "college",
                  "soundtrack"
              ],
              "mixed",
              [
                  "disco",
              "russian", #6
            ],
            "hip hop|hip|hop", #34
            [
                  "mainstream",
                  "soulfull"
              ],
              "funk",
              "hiphop",
              [
               "oldschool", #10
               "rap",
            ],
            "ambient", #34
            "adult", #33
           ## "music", #32
            "oldies", #31
            [
              "60s", #2
              "70s", #17
            ],
            "religious", #4
            [
              "christian|bible", #14
            ],
            "rnb|r&b", #12
            [
              "soul", #11
              "funk", #24
              "urban", #11
            ],
            "other", #25
            [
              "deep", #14
                  "rap",
                  "dubstep",
                  "hip",
                  "hop"
              ],
              "top",
              [
                  "urban"
              ],
              "musica",
              "ambient",
              [
                  "downtempo",
                  "dub"
              ],
              "promodj",
              "world",    # REGIONAL
              [
                  "france",
                  "greek",
                  "german",
                  "westcoast",
                  "bollywood",
                  "indian",
                  "nederlands",
                  "europa",
                  "italia",
                  "brazilian",
                  "tropical",
                  "korea",
                  "seychelles",
                  "black",
                  "japanese",
                  "ethnic",
                  "country",
                  "americana",
                  "western",
                  "cuba",
                  "afrique",
                  "paris",
                  "celtic",
                  "ambiance",
                  "francais",
                  "liberte",
                  "anglais",
                  "arabic",
                  "hungary",
                  "folklore"
                  "latin",
                  "dutch"
                  "italy"
              ],
              "artist",   # ARTIST NAMES
              [
                  "mozart",
                  "beatles",
                  "michael",
                  "nirvana",
                  "elvis",
                  "britney",
                  "abba",
                  "madonna",
                  "depeche",
              ],
              "salsa",
              "love",
              "la",
              "soul",
              "techno",
              [
                  "club",
                  "progressive",
                  "deep"
              "electro",
              ],
              "best",
              "100%",
              "rnb",
              "retro",
              "new",
              "smooth",
              [
                  "cool"
              ],
              "easy",
              [
                  "lovesongs",
                  "relaxmusic"
              ],
              "chillout",
              "slow",
              [
                  "soft"
              ],
              "mix",
              [
                  "modern"
              ],
              "punk",
              [
                  "ska"
              ],
              "international",
              "bass",
              "zouk",
              "video",
              [
                  "game"
              ],
              "hardstyle",
              "scanner",
              "chill",
              [
                  "out",
                  "trip"
              ],
              "drum",
              "roots",
              "ac",
              [
                  "chr",
                  "dc"
              ],
              "public",
              "contemporary",
              [
                  "instrumental"
              ],
              "minimal",
              "hot",
              [
                  "based"
              ],
              "free",
              [
                  "format"
              ],
              "hard",
              [
                  "heavy",
              "soft", #12
              "minimal", #12
              "eclectic", #12
              "drum", #12
              "bass", #12
                  "classicrock"
              "experimental", #11
              "hard", #10
              "funky", #10
              "downtempo", #10
              "slow", #9
              ],
              "reggaeton",
              "southern",
              "break", #9
              "electronica", #8
              "dub", #8
              "musica",
              "old",
              "retro", #7
              "punk", #7
              "psychedelic", #7
              "goa", #7
              "freeform", #7
              "c64", #7
              "breaks", #7
              "anime", #7
              "variety", #6
              "emisora",
              "img",
              "rockabilly",
              "charts",
              [
                  "best80",
                  "70er",
                  "80er",
              "psytrance", #6
              "island", #6
              "downbeat", #6
                  "60er"
                  "chart",
              "underground", #5
              "newage", #5
              "gothic", #5
              ],
              "other",
              "dnb", #5
              "club", #5
              "acid", #5
              "video", #4
              "trip", #4
              [
                  "varios"
              ],
              "soulful",
              "pure", #4
              "industrial", #4
              "groove", #4
              "listening",
              "vegyes",
              "gospel", #4
              "gadanie", #4
              "creative",
              "french", #4
              "dark", #4
              "chill", #4
              "age", #4
              "variety",
              "commons",
              [
                  "ccmusik"
              "wave", #3
              "vocal", #3
              "tech", #3
              ],
              "tech",
              "studio", #3
              "relax", #3
              "rave", #3
              [
                  "edm",
                  "prog"
              "hardcore", #3
              "breakbeat", #3
              "avantgarde", #3
              "swing", #2
              "soundtrack", #2
              "salsa", #2
              ],
              "minecraft",
              "animes",
              "goth",
              "technologie",
              "tout",
              "musical",
              "italian", #2
              "independant", #2
              "groovy", #2
              [
                  "broadway"
              "european", #2
              "darkwave", #2
            ],
        ]
              ],
              "romantica",
              "newage",
              "nostalgia",
              "oldschool",
              [
                  "00s"
              ],
              "wij",
              "relax",
              [
                  "age"
              ],
              "theatre",
              "gothic",
              "dnb",
              "disney",
              "funky",
              "young",
              "psychedelic",
              "habbo",
              "experimental",
              "exitos",
              "digital",
              "no",
              "industrial",
              "epic",
              "soundtracks",
              "cover",
              "chd",
              "games",
              "libre",
              "wave",
              "vegas",
              "comedy",
              "alternate",
              "instrumental",
              [
                  "swing"
              ],
              "ska",
              [
                  "punkrock",
                  "oi"
              ],
              "darkwave",
          ]