Explorar o código

used url_resolved for yamaha app

Thomas Hanika %!s(int64=3) %!d(string=hai) anos
pai
achega
2179c48d5a
Modificáronse 6 ficheiros con 50 adicións e 21 borrados
  1. 1 1
      setup.py
  2. 12 3
      ycast/my_filter.py
  3. 0 1
      ycast/my_stations.py
  4. 8 8
      ycast/radiobrowser.py
  5. 24 7
      ycast/server.py
  6. 5 1
      ycast/test_filter.py

+ 1 - 1
setup.py

@@ -40,6 +40,6 @@ setup(
         'onkyo',
         'denon'
     ],
-    install_requires=['requests', 'flask', 'PyYAML', 'Pillow', 'oyaml'],
+    install_requires=['requests', 'flask', 'PyYAML', 'Pillow'],
     packages=find_packages(exclude=['contrib', 'docs', 'tests'])
 )

+ 12 - 3
ycast/filter.py → ycast/my_filter.py

@@ -7,13 +7,18 @@ white_list = {}
 black_list = {}
 filter_dir = {}
 parameter_failed_list = {}
-
+count_used = 0
+count_hit = 0
 
 def init_filter():
     global white_list
     global black_list
     global parameter_failed_list
     global filter_dir
+    global count_used
+    global count_hit
+    count_used = 0
+    count_hit = 0
     filter_dir = generic.read_yaml_file(generic.get_var_path() + '/filter.yml')
     if filter_dir:
         white_list = filter_dir['whitelist']
@@ -30,9 +35,9 @@ def init_filter():
 
 def end_filter():
     if parameter_failed_list:
-        logging.info("Used filter parameter: %s", parameter_failed_list)
+        logging.info("(%d/%d) stations filtered by: %s", count_hit, count_used, parameter_failed_list)
     else:
-        logging.info("Used filter parameter: <nothing used>")
+        logging.info("(%d/%d) stations filtered by: <no filter used>")
 
 
 def parameter_hit(param_name):
@@ -46,6 +51,9 @@ def parameter_hit(param_name):
 
 
 def check_station(station_json):
+    global count_used
+    global count_hit
+    count_used = count_used + 1
     station_name = get_json_attr(station_json, 'name')
     if not station_name:
         # müll response
@@ -92,4 +100,5 @@ def check_station(station_json):
 #                                  station_name, param_name, valid_elements, val)
                     return False
 #    logging.debug("OK   '%s' passed", station_name)
+    count_hit = count_hit + 1
     return True

+ 0 - 1
ycast/my_stations.py

@@ -1,4 +1,3 @@
-import logging
 
 import ycast.vtuner as vtuner
 import ycast.generic as generic

+ 8 - 8
ycast/radiobrowser.py

@@ -7,7 +7,7 @@ import logging
 from ycast import __version__
 import ycast.vtuner as vtuner
 import ycast.generic as generic
-from ycast.filter import check_station, init_filter, end_filter
+from ycast.my_filter import check_station, init_filter, end_filter
 from ycast.generic import get_json_attr
 
 API_ENDPOINT = "http://all.api.radio-browser.info"
@@ -28,7 +28,11 @@ class Station:
         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.url = generic.get_json_attr(station_json, 'url_resolved')
+        if not self.url:
+            self.url = generic.get_json_attr(station_json, 'url')
+
         self.icon = generic.get_json_attr(station_json, 'favicon')
         self.tags = generic.get_json_attr(station_json, 'tags').split(',')
         self.countrycode = generic.get_json_attr(station_json, 'countrycode')
@@ -45,8 +49,9 @@ class Station:
 
     def get_playable_url(self):
         try:
-            playable_url_json = request('url/' + str(self.stationuuid))[0]
+            playable_url_json = request('url/' + str(self.stationuuid))
             self.url = playable_url_json['url']
+
         except (IndexError, KeyError):
             logging.error("Could not retrieve first playlist item for station with id '%s'", self.stationuuid)
 
@@ -141,7 +146,6 @@ def get_stations_by_country(country):
             cur_station = Station(station_json)
             station_cache[cur_station.id] = cur_station
             stations.append(cur_station)
-    logging.info("Stations (%d/%d)", len(stations), len(stations_list_json))
     end_filter()
     return stations
 
@@ -157,7 +161,6 @@ def get_stations_by_language(language):
             cur_station = Station(station_json)
             station_cache[cur_station.id] = cur_station
             stations.append(cur_station)
-    logging.info("Stations (%d/%d)", len(stations), len(stations_list_json))
     end_filter()
     return stations
 
@@ -172,7 +175,6 @@ def get_stations_by_genre(genre):
             cur_station = Station(station_json)
             station_cache[cur_station.id] = cur_station
             stations.append(cur_station)
-    logging.info("Stations (%d/%d)", len(stations), len(stations_list_json))
     end_filter()
     return stations
 
@@ -187,7 +189,6 @@ def get_stations_by_votes(limit=DEFAULT_STATION_LIMIT):
             cur_station = Station(station_json)
             station_cache[cur_station.id] = cur_station
             stations.append(cur_station)
-    logging.info("Stations (%d/%d)", len(stations), len(stations_list_json))
     end_filter()
     return stations
 
@@ -202,6 +203,5 @@ def search(name, limit=DEFAULT_STATION_LIMIT):
             cur_station = Station(station_json)
             station_cache[cur_station.id] = cur_station
             stations.append(cur_station)
-    logging.info("Stations (%d/%d)", len(stations), len(stations_list_json))
     end_filter()
     return stations

+ 24 - 7
ycast/server.py

@@ -40,13 +40,13 @@ def check_my_stations_feature(config):
     my_stations_enabled = my_stations.set_config(config)
 
 
-def get_directories_page(subdir, directories, request):
+def get_directories_page(subdir, directories, request_obj):
     page = vtuner.Page()
     if len(directories) == 0:
         page.add(vtuner.Display("No entries found"))
         page.set_count(1)
         return page
-    for directory in get_paged_elements(directories, request.args):
+    for directory in get_paged_elements(directories, request_obj.args):
         vtuner_directory = vtuner.Directory(directory.displayname,
                                             url_for(subdir, _external=True, directory=directory.name),
                                             directory.item_count)
@@ -55,17 +55,18 @@ def get_directories_page(subdir, directories, request):
     return page
 
 
-def get_stations_page(stations, request):
+def get_stations_page(stations, request_obj):
     page = vtuner.Page()
     if len(stations) == 0:
         page.add(vtuner.Display("No stations found"))
         page.set_count(1)
         return page
-    for station in get_paged_elements(stations, request.args):
+    for station in get_paged_elements(stations, request_obj.args):
         vtuner_station = station.to_vtuner()
         if station_tracking:
-            vtuner_station.set_trackurl(request.host_url + PATH_ROOT + '/' + PATH_PLAY + '?id=' + vtuner_station.uid)
-        vtuner_station.icon = request.host_url + PATH_ROOT + '/' + PATH_ICON + '?id=' + vtuner_station.uid
+            vtuner_station.set_trackurl(
+                request_obj.host_url + PATH_ROOT + '/' + PATH_PLAY + '?id=' + vtuner_station.uid)
+        vtuner_station.icon = request_obj.host_url + PATH_ROOT + '/' + PATH_ICON + '?id=' + vtuner_station.uid
         page.add(vtuner_station)
     page.set_count(len(stations))
     return page
@@ -112,7 +113,7 @@ def get_station_by_id(stationid, additional_info=False):
 
 
 def vtuner_redirect(url):
-    if request and request.host and not re.search("^[A-Za-z0-9]+\.vtuner\.com$", request.host):
+    if request and request.host and not re.search(r"^[A-Za-z0-9]+\.vtuner\.com$", request.host):
         logging.warning("You are not accessing a YCast redirect with a whitelisted host url (*.vtuner.com). "
                         "Some AVRs have problems with this. The requested host was: %s", request.host)
     return redirect(url, code=302)
@@ -121,6 +122,7 @@ def vtuner_redirect(url):
 @app.route('/setupapp/<path:path>',
            methods=['GET', 'POST'])
 def upstream(path):
+    logging.debug('**********************:  %s', request.url)
     if request.args.get('token') == '0':
         return vtuner.get_init_token()
     if request.args.get('search'):
@@ -144,6 +146,7 @@ def upstream(path):
            defaults={'path': ''},
            methods=['GET', 'POST'])
 def landing(path=''):
+    logging.debug('**********************:  %s', request.url)
     page = vtuner.Page()
     page.add(vtuner.Directory('Radiobrowser', url_for('radiobrowser_landing', _external=True), 4))
     if my_stations_enabled:
@@ -158,6 +161,7 @@ def landing(path=''):
 @app.route('/' + PATH_ROOT + '/' + PATH_MY_STATIONS + '/',
            methods=['GET', 'POST'])
 def my_stations_landing():
+    logging.debug('**********************:  %s', request.url)
     directories = my_stations.get_category_directories()
     return get_directories_page('my_stations_category', directories, request).to_string()
 
@@ -165,6 +169,7 @@ def my_stations_landing():
 @app.route('/' + PATH_ROOT + '/' + PATH_MY_STATIONS + '/<directory>',
            methods=['GET', 'POST'])
 def my_stations_category(directory):
+    logging.debug('**********************:  %s', request.url)
     stations = my_stations.get_stations_by_category(directory)
     return get_stations_page(stations, request).to_string()
 
@@ -172,6 +177,7 @@ def my_stations_category(directory):
 @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/',
            methods=['GET', 'POST'])
 def radiobrowser_landing():
+    logging.debug('**********************:  %s', request.url)
     page = vtuner.Page()
     page.add(vtuner.Directory('Genres', url_for('radiobrowser_genres', _external=True),
                               len(radiobrowser.get_genre_directories())))
@@ -188,6 +194,7 @@ def radiobrowser_landing():
 @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_COUNTRY + '/',
            methods=['GET', 'POST'])
 def radiobrowser_countries():
+    logging.debug('**********************:  %s', request.url)
     directories = radiobrowser.get_country_directories()
     return get_directories_page('radiobrowser_country_stations', directories, request).to_string()
 
@@ -195,6 +202,7 @@ def radiobrowser_countries():
 @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_COUNTRY + '/<directory>',
            methods=['GET', 'POST'])
 def radiobrowser_country_stations(directory):
+    logging.debug('**********************:  %s', request.url)
     stations = radiobrowser.get_stations_by_country(directory)
     return get_stations_page(stations, request).to_string()
 
@@ -202,6 +210,7 @@ def radiobrowser_country_stations(directory):
 @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_LANGUAGE + '/',
            methods=['GET', 'POST'])
 def radiobrowser_languages():
+    logging.debug('**********************:  %s', request.url)
     directories = radiobrowser.get_language_directories()
     return get_directories_page('radiobrowser_language_stations', directories, request).to_string()
 
@@ -209,6 +218,7 @@ def radiobrowser_languages():
 @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_LANGUAGE + '/<directory>',
            methods=['GET', 'POST'])
 def radiobrowser_language_stations(directory):
+    logging.debug('**********************:  %s', request.url)
     stations = radiobrowser.get_stations_by_language(directory)
     return get_stations_page(stations, request).to_string()
 
@@ -216,6 +226,7 @@ def radiobrowser_language_stations(directory):
 @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_GENRE + '/',
            methods=['GET', 'POST'])
 def radiobrowser_genres():
+    logging.debug('**********************:  %s', request.url)
     directories = radiobrowser.get_genre_directories()
     return get_directories_page('radiobrowser_genre_stations', directories, request).to_string()
 
@@ -223,6 +234,7 @@ def radiobrowser_genres():
 @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_GENRE + '/<directory>',
            methods=['GET', 'POST'])
 def radiobrowser_genre_stations(directory):
+    logging.debug('**********************:  %s', request.url)
     stations = radiobrowser.get_stations_by_genre(directory)
     return get_stations_page(stations, request).to_string()
 
@@ -230,6 +242,7 @@ def radiobrowser_genre_stations(directory):
 @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_POPULAR + '/',
            methods=['GET', 'POST'])
 def radiobrowser_popular():
+    logging.debug('**********************:  %s', request.url)
     stations = radiobrowser.get_stations_by_votes()
     return get_stations_page(stations, request).to_string()
 
@@ -237,6 +250,7 @@ def radiobrowser_popular():
 @app.route('/' + PATH_ROOT + '/' + PATH_SEARCH + '/',
            methods=['GET', 'POST'])
 def station_search():
+    logging.debug('**********************:  %s', request.url)
     query = request.args.get('search')
     if not query or len(query) < 3:
         page = vtuner.Page()
@@ -252,6 +266,7 @@ def station_search():
 @app.route('/' + PATH_ROOT + '/' + PATH_PLAY,
            methods=['GET', 'POST'])
 def get_stream_url():
+    logging.debug('**********************:  %s', request.url)
     stationid = request.args.get('id')
     if not stationid:
         logging.error("Stream URL without station ID requested")
@@ -267,6 +282,7 @@ def get_stream_url():
 @app.route('/' + PATH_ROOT + '/' + PATH_STATION,
            methods=['GET', 'POST'])
 def get_station_info():
+    logging.debug('**********************:  %s', request.url)
     stationid = request.args.get('id')
     if not stationid:
         logging.error("Station info without station ID requested")
@@ -291,6 +307,7 @@ def get_station_info():
 @app.route('/' + PATH_ROOT + '/' + PATH_ICON,
            methods=['GET', 'POST'])
 def get_station_icon():
+    logging.debug('**********************:  %s', request.url)
     stationid = request.args.get('id')
     if not stationid:
         logging.error("Station icon without station ID requested")

+ 5 - 1
ycast/test_filter.py

@@ -3,7 +3,7 @@ import logging
 import unittest
 from io import StringIO
 
-import filter
+import my_filter
 import generic
 from ycast import radiobrowser
 
@@ -39,6 +39,10 @@ class MyTestCase(unittest.TestCase):
         logging.info("Used filter parameter", filter.parameter_failed_list)
 
 
+    def test_popular_station(self):
+        stations = radiobrowser.get_stations_by_votes()
+        logging.info("Stations (%d)", len(stations))
+
 if __name__ == '__main__':
     unittest.main()