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 | self.api("category/{}/stations".format(self.catmap.get(cat, 0)), all=1)# per_page=200 won't work
]
# Extract rows
def unpack(self, r):
# find stream (might select on `bitrate` or `format` if available)
if len(r.get("streams", [])):
s = r["streams"][0]
else:
return {}
print r
# rename fields
return dict(
genre = " ".join(c["slug"] for c in r["categories"]),
title = r["name"],
playing = "{} {}".format(r.get("country"), r.get("description")),
homepage = ahttp.fix_url(r["website"]),
url = s["stream"],
format = s["content_type"],
bitrate = s["bitrate"],
# img = r["image"]["image"]["thumb"]["url"], # CDN HTTPS trip up requests.get
state = self.state_map[int(s["status"])] if s["status"] in [0,1,2] else "",
deleted = s.get("timedout", False),
)
state_map = ["gtk-media-pause", "gtk-media-next", "gtk-media-rewind"]
# Patch API url together, send request, decode JSON list
def api(self, method, **params):
params["token"] = conf.dirble_api_key or self.key
try:
# HTTP request and JSON decoding take a while |
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
|
>
>
| 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 | self.api("category/{}/stations".format(self.catmap.get(cat, 0)), all=1)# per_page=200 won't work
]
# Extract rows
def unpack(self, r):
# find stream
if len(r.get("streams", [])):
s = r["streams"][0]
# select "best" stream if there are alternatives
if len(r["streams"]) > 1:
for alt in r["streams"]:
# weight format with bitrate
cur_q = self.format_q.get( s["content_type"].strip(), "0.9") \
* s.get("bitrate", 32)
alt_q = self.format_q.get(alt["content_type"].strip(), "0.9") \
* alt.get("bitrate", 32)
# swap out for overall better score
if alt_q > cur_q:
s = alt
#log.DATA_BETTER_STREAM(s, "←FROM←", r)
# fix absent audio type
if s["content_type"] == "?":
s["content_type"] == "audio/mpeg"
else:
return {}
# rename fields
return dict(
genre = " ".join(c["slug"] for c in r["categories"]),
title = r["name"],
playing = "{} {}".format(r.get("country"), r.get("description")),
homepage = ahttp.fix_url(r["website"]),
url = s["stream"],
format = s["content_type"].strip(),
bitrate = s["bitrate"],
# img = r["image"]["image"]["thumb"]["url"], # CDN HTTPS trip up requests.get
state = self.state_map[int(s["status"])] if s["status"] in [0,1,2] else "",
deleted = s.get("timedout", False),
)
state_map = ["gtk-media-pause", "gtk-media-next", "gtk-media-rewind"]
format_q = {"?":0.75, "audio/mpeg":1.0, "audio/aac":1.25, "audio/aacp":1.35, "audio/ogg":1.50}
# Patch API url together, send request, decode JSON list
def api(self, method, **params):
params["token"] = conf.dirble_api_key or self.key
try:
# HTTP request and JSON decoding take a while |