Explorar el Código

using bas64 coded uuid, favicon refernced as favicon-url

Thomas Hanika hace 3 años
padre
commit
4d2a8b219f
Se han modificado 5 ficheros con 52 adiciones y 21 borrados
  1. 6 0
      examples/filter.yml.example
  2. 14 17
      ycast/filter.py
  3. 7 0
      ycast/generic.py
  4. 20 2
      ycast/radiobrowser.py
  5. 5 2
      ycast/station_icons.py

+ 6 - 0
examples/filter.yml.example

@@ -0,0 +1,6 @@
+blacklist:
+  favicon: ''
+whitelist:
+  countrycode: DE,US,NO,GB
+  languagecodes: en,no
+  lastcheckok: 1

+ 14 - 17
ycast/filter.py

@@ -8,6 +8,7 @@ black_list = {}
 filter_dir = {}
 parameter_failed_list = {}
 
+
 def init_filter():
     global white_list
     global black_list
@@ -18,27 +19,22 @@ def init_filter():
         white_list = filter_dir['whitelist']
         black_list = filter_dir['blacklist']
     else:
-        white_list = { 'lastcheckok': 1 }
+        white_list = {'lastcheckok': 1}
         black_list = {}
-        filter_dir = {}
-        filter_dir['whitelist'] = white_list
-        filter_dir['blacklist'] = black_list
+        filter_dir = {'whitelist': white_list, 'blacklist': black_list}
         generic.write_yaml_file(generic.get_var_path() + '/filter.yml', filter_dir)
 
-        filter_ex = {}
-        filter_ex['whitelist'] = {'lastcheckok': 1, 'countrycode': 'DE,US,NO,GB', 'languagecodes': 'en,no'}
-        filter_ex['blacklist'] = {'favicon': ''}
-        generic.write_yaml_file(generic.get_var_path() + '/filter.yml_example', filter_ex)
-
     parameter_failed_list.clear()
     return
 
+
 def end_filter():
     if parameter_failed_list:
         logging.info("Used filter parameter: %s", parameter_failed_list)
     else:
         logging.info("Used filter parameter: <nothing used>")
 
+
 def parameter_hit(param_name):
     count = 1
     old = None
@@ -48,6 +44,7 @@ def parameter_hit(param_name):
         count = old + 1
     parameter_failed_list[param_name] = count
 
+
 def check_station(station_json):
     station_name = get_json_attr(station_json, 'name')
     if not station_name:
@@ -59,7 +56,7 @@ def check_station(station_json):
         black_list_hit = False
         for param_name in black_list:
             unvalid_elements = black_list[param_name]
-            val =  get_json_attr(station_json, param_name)
+            val = get_json_attr(station_json, param_name)
             if not val == None:
                 # attribut in json vorhanden
                 if unvalid_elements:
@@ -71,15 +68,15 @@ def check_station(station_json):
                         black_list_hit = True
                 if black_list_hit:
                     parameter_hit(param_name)
-                    logging.debug("FAIL '%s' blacklist hit on '%s' '%s' == '%s'",
-                                  station_name, param_name, unvalid_elements, val)
+#                    logging.debug("FAIL '%s' blacklist hit on '%s' '%s' == '%s'",
+#                                  station_name, param_name, unvalid_elements, val)
                     return False
 
 # und verknüpft
     if white_list:
         white_list_hit = True
         for param_name in white_list:
-            val =  get_json_attr(station_json, param_name)
+            val = get_json_attr(station_json, param_name)
             if not val == None:
                 # attribut in json vorhanden
                 valid_elements = white_list[param_name]
@@ -88,11 +85,11 @@ def check_station(station_json):
                 else:
                     if val:
                         pos = valid_elements.find(val)
-                        white_list_hit = pos >=0
+                        white_list_hit = pos >= 0
                 if not white_list_hit:
                     parameter_hit(param_name)
-                    logging.debug("FAIL '%s' whitelist failed on '%s' '%s' == '%s'",
-                                  station_name, param_name, valid_elements, val)
+#                    logging.debug("FAIL '%s' whitelist failed on '%s' '%s' == '%s'",
+#                                  station_name, param_name, valid_elements, val)
                     return False
-    logging.debug("OK   '%s' passed", station_name)
+#    logging.debug("OK   '%s' passed", station_name)
     return True

+ 7 - 0
ycast/generic.py

@@ -35,6 +35,13 @@ def get_stationid_prefix(uid):
     return uid[:2]
 
 
+def get_stationid_without_prefix(uid):
+    if len(uid) < 4:
+        logging.error("Could not extract stationid (Invalid station id length)")
+        return None
+    return uid[3:]
+
+
 def get_cache_path(cache_name):
     cache_path = CACHE_PATH
     if cache_name:

+ 20 - 2
ycast/radiobrowser.py

@@ -1,3 +1,6 @@
+import base64
+import uuid
+
 import requests
 import logging
 
@@ -22,7 +25,8 @@ station_cache = {}
 class Station:
     def __init__(self, station_json):
         self.stationuuid = generic.get_json_attr(station_json, 'stationuuid')
-        self.id = generic.generate_stationid_with_prefix(generic.get_checksum(self.stationuuid), ID_PREFIX)
+        self.id = generic.generate_stationid_with_prefix(
+            base64.urlsafe_b64encode(uuid.UUID(self.stationuuid).bytes).decode(), ID_PREFIX)
         self.name = generic.get_json_attr(station_json, 'name')
         self.url = generic.get_json_attr(station_json, 'url')
         self.icon = generic.get_json_attr(station_json, 'favicon')
@@ -62,8 +66,22 @@ def request(url):
 
 
 def get_station_by_id(vtune_id):
+    global station_cache
+# decode
+    uidbase64 = generic.get_stationid_without_prefix(vtune_id)
+    uid = str(uuid.UUID(base64.urlsafe_b64decode(uidbase64).hex()))
     if station_cache:
-        return station_cache[vtune_id]
+        station = station_cache[vtune_id]
+        if station:
+            logging.debug('verify %s:%s', station.stationuuid, uid)
+            return station
+# no item in cache, do request
+    station_json = request('stations/byuuid?uuids=' + uid)
+    if station_json and len(station_json):
+        station = Station(station_json[0])
+        if station:
+            station_cache[station.id] = station
+        return station
     return None
 
 

+ 5 - 2
ycast/station_icons.py

@@ -16,7 +16,9 @@ def get_icon(station):
     cache_path = generic.get_cache_path(CACHE_NAME)
     if not cache_path:
         return None
-    station_icon_file = cache_path + '/' + station.id
+
+# make icon filename from favicon-adress
+    station_icon_file = cache_path + '/' + generic.get_checksum(station.icon) + '.jpg'
     if not os.path.exists(station_icon_file):
         logging.debug("Station icon cache miss. Fetching and converting station icon for station id '%s'", station.id)
         headers = {'User-Agent': generic.USER_AGENT + '/' + __version__}
@@ -26,7 +28,8 @@ def get_icon(station):
             logging.debug("Connection to station icon URL failed (%s)", err)
             return None
         if response.status_code != 200:
-            logging.debug("Could not get station icon data from %s (HTML status %s)", station.icon, response.status_code)
+            logging.debug("Could not get station icon data from %s (HTML status %s)",
+                          station.icon, response.status_code)
             return None
         try:
             image = Image.open(io.BytesIO(response.content))