24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# HTTP retrieval happens in one batch, determined by the number of pages
# setting, rather than the global max_streams option.
#
from channels import *
import re
from config import conf, __print__, dbg
import ahttp as http
from pq import pq
# streams and gui
|
|
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# HTTP retrieval happens in one batch, determined by the number of pages
# setting, rather than the global max_streams option.
#
from channels import *
import re
from config import *
import ahttp as http
from pq import pq
# streams and gui
|
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
|
# Is there a next page?
if str(page+1) not in rx_pages.findall(html[-1]):
break
self.parent.status(float(page)/float(max_pages+1))
# Alternatively try regex or pyquery parsing
#__print__(dbg.HTTP, html)
for use_rx in [not conf.pyquery, conf.pyquery]:
try:
entries = (self.with_regex(html) if use_rx else self.with_dom(html))
if len(entries):
break
except Exception as e:
__print__(dbg.ERR, e)
continue
# fin
return entries
# Regex extraction
def with_regex(self, html):
__print__(dbg.PROC, "internet-radio, regex")
r = []
html = "\n".join(html)
# Break up into <tr> blocks before extracting bits
rx_tr = re.compile("""<tr[^>]*>(.+?)</tr>""", re.S)
rx_data = re.compile(r"""
playjp',\s*'(https?://[^'">]+)
.*? <h4.*?>([^<>]+)</
.*? <b>([^<>]*)</b>
(?: .*? href="(.*?)" )?
(?: .*? Genres:((?:</?a[^>]+>|\w+|\s+)+) )?
.*? (\d+)\s*Listeners
.*? (\d+)\s*Kbps
""", re.S|re.X)
for div in rx_tr.findall(html):
if div.find('id="pagination"') < 0:
#__print__(dbg.DATA, len(div))
uu = rx_data.search(div)
if uu:
(url, title, playing, homepage, genres, listeners, bitrate) = uu.groups()
# transform data
r.append({
"url": url,
"genre": self.strip_tags(genres or ""),
"homepage": http.fix_url(homepage or ""),
"title": (title or "").strip().replace("\n", " "),
"playing": (playing or "").strip().replace("\n", " "),
"bitrate": int(bitrate or 0),
"listeners": int(listeners or 0),
"format": "audio/mpeg", # there is no stream info on that, but internet-radio.org.uk doesn't seem very ogg-friendly anyway, so we assume the default here
})
else:
__print__(dbg.DATA, "Regex couldn't decipher entry:", div)
return r
# DOM traversing
def with_dom(self, html_list):
__print__(dbg.PROC, "internet-radio, dom")
rx_numbers = re.compile("(\d+)")
r = []
for html in html_list:
# the streams are arranged in table rows
doc = pq(html)
for dir in (pq(e) for e in doc("tr")):
|
|
|
|
|
|
|
|
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
|
# Is there a next page?
if str(page+1) not in rx_pages.findall(html[-1]):
break
self.parent.status(float(page)/float(max_pages+1))
# Alternatively try regex or pyquery parsing
#log.HTTP(html)
for use_rx in [not conf.pyquery, conf.pyquery]:
try:
entries = (self.with_regex(html) if use_rx else self.with_dom(html))
if len(entries):
break
except Exception as e:
log.ERR(e)
continue
# fin
return entries
# Regex extraction
def with_regex(self, html):
log.PROC("internet-radio, regex")
r = []
html = "\n".join(html)
# Break up into <tr> blocks before extracting bits
rx_tr = re.compile("""<tr[^>]*>(.+?)</tr>""", re.S)
rx_data = re.compile(r"""
playjp',\s*'(https?://[^'">]+)
.*? <h4.*?>([^<>]+)</
.*? <b>([^<>]*)</b>
(?: .*? href="(.*?)" )?
(?: .*? Genres:((?:</?a[^>]+>|\w+|\s+)+) )?
.*? (\d+)\s*Listeners
.*? (\d+)\s*Kbps
""", re.S|re.X)
for div in rx_tr.findall(html):
if div.find('id="pagination"') < 0:
#log.DATA(len(div))
uu = rx_data.search(div)
if uu:
(url, title, playing, homepage, genres, listeners, bitrate) = uu.groups()
# transform data
r.append({
"url": url,
"genre": self.strip_tags(genres or ""),
"homepage": http.fix_url(homepage or ""),
"title": (title or "").strip().replace("\n", " "),
"playing": (playing or "").strip().replace("\n", " "),
"bitrate": int(bitrate or 0),
"listeners": int(listeners or 0),
"format": "audio/mpeg", # there is no stream info on that, but internet-radio.org.uk doesn't seem very ogg-friendly anyway, so we assume the default here
})
else:
log.DATA("Regex couldn't decipher entry:", div)
return r
# DOM traversing
def with_dom(self, html_list):
log.PROC("internet-radio, dom")
rx_numbers = re.compile("(\d+)")
r = []
for html in html_list:
# the streams are arranged in table rows
doc = pq(html)
for dir in (pq(e) for e in doc("tr")):
|