Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More flake

  • Loading branch information...
commit 76c68fc8f0bbaee6228779c2122a78e28db0308d 1 parent c16f884
@kingosticks authored
Showing with 34 additions and 25 deletions.
  1. +26 −19 mopidy_tunein/translator.py
  2. +8 −6 mopidy_tunein/tunein.py
View
45 mopidy_tunein/translator.py
@@ -9,26 +9,29 @@
logger = logging.getLogger(__name__)
-TUNEIN_ID_PROGRAM = 'program'
-TUNEIN_ID_STATION = 'station'
-TUNEIN_ID_GROUP = 'group'
-TUNEIN_ID_TOPIC = 'topic'
-TUNEIN_ID_CATEGORY = 'category'
-TUNEIN_ID_REGION = 'region'
-TUNEIN_ID_PODCAST = 'podcast_category'
+TUNEIN_ID_PROGRAM = 'program'
+TUNEIN_ID_STATION = 'station'
+TUNEIN_ID_GROUP= 'group'
+TUNEIN_ID_TOPIC = 'topic'
+TUNEIN_ID_CATEGORY = 'category'
+TUNEIN_ID_REGION = 'region'
+TUNEIN_ID_PODCAST = 'podcast_category'
TUNEIN_ID_AFFILIATE = 'affiliate'
-TUNEIN_ID_STREAM = 'stream'
-TUNEIN_ID_UNKNOWN = 'unknown'
+TUNEIN_ID_STREAM = 'stream'
+TUNEIN_ID_UNKNOWN = 'unknown'
+
def unparse_uri(variant, identifier):
return b'tunein:%s:%s' % (variant, identifier)
+
def parse_uri(uri):
result = re.findall(r'^tunein:([a-z]+)(?::(\w+))?$', uri)
if result:
return result[0]
return None, None
+
def station_to_ref(station):
if station['type'] != 'audio':
logger.debug('Expecting station but got %s' % station['type'])
@@ -40,6 +43,7 @@ def station_to_ref(station):
name = name + ' [%s]' % station.get('subtext', '??')
return Ref.track(uri=uri, name=name)
+
def show_to_ref(show):
if show['item'] != 'show':
logger.debug('Expecting show but got %s' % show['item'])
@@ -47,10 +51,12 @@ def show_to_ref(show):
name = show.get('text', show['URL'])
return Ref.directory(uri=uri, name=name)
+
def category_to_ref(category):
uri = unparse_uri('category', category['key'])
return Ref.directory(uri=uri, name=category['text'])
+
def section_to_ref(category):
guide_id = category.get('guide_id', '??')
if get_id_type(guide_id) == TUNEIN_ID_REGION:
@@ -59,16 +65,18 @@ def section_to_ref(category):
uri = unparse_uri('section', guide_id)
return Ref.directory(uri=uri, name=category['text'])
+
def get_id_type(guide_id):
- return {'p' : TUNEIN_ID_PROGRAM,
- 's' : TUNEIN_ID_STATION,
- 'g' : TUNEIN_ID_GROUP,
- 't' : TUNEIN_ID_TOPIC,
- 'c' : TUNEIN_ID_CATEGORY,
- 'r' : TUNEIN_ID_REGION,
- 'f' : TUNEIN_ID_PODCAST,
- 'a' : TUNEIN_ID_AFFILIATE,
- 'e' : TUNEIN_ID_STREAM}.get(guide_id[0], TUNEIN_ID_UNKNOWN)
+ return {'p': TUNEIN_ID_PROGRAM,
+ 's': TUNEIN_ID_STATION,
+ 'g': TUNEIN_ID_GROUP,
+ 't': TUNEIN_ID_TOPIC,
+ 'c': TUNEIN_ID_CATEGORY,
+ 'r': TUNEIN_ID_REGION,
+ 'f': TUNEIN_ID_PODCAST,
+ 'a': TUNEIN_ID_AFFILIATE,
+ 'e': TUNEIN_ID_STREAM}.get(guide_id[0], TUNEIN_ID_UNKNOWN)
+
def mopidy_to_tunein_query(mopidy_query):
tunein_query = []
@@ -79,4 +87,3 @@ def mopidy_to_tunein_query(mopidy_query):
if field == 'any':
tunein_query.append(value)
return urllib.pathname2url(' '.join(tunein_query))
-
View
14 mopidy_tunein/tunein.py
@@ -59,6 +59,7 @@ def parse_m3u(data):
if not line.startswith('#') and line.strip():
yield line.strip()
+
def parse_pls(data):
# Copied from mopidy.audio.playlists
try:
@@ -73,6 +74,7 @@ def parse_pls(data):
for i in xrange(cp.getint(section, 'numberofentries')):
yield cp.get(section, 'file%d' % (i+1))
+
def parse_asx(data):
# Copied from mopidy.audio.playlists
# Mopidy doesn't support asx: mopidy/mopidy#687
@@ -100,6 +102,7 @@ def parse_asx(data):
# logger.debug('Failed to parse playlist')
# return results
+
def find_playlist_parser(extension, content_type):
extension_map = {'.asx': parse_asx,
'.m3u': parse_m3u,
@@ -148,15 +151,15 @@ def _filter_results(self, data, section_name='', map_func=None):
def categories(self, category=''):
if category == 'location':
- args = '&id=r0' # Annoying special case
+ args = '&id=r0' # Annoying special case
elif category == 'language':
args = '&c=lang'
- return [] # Tunein's API is a mess here, cba
+ return [] # Tunein's API is a mess here, cba
else:
args = '&c=' + category
results = self._tunein('Browse.ashx', args)
if (category == 'podcast'):
- results = self._filter_results(results) # More API fun please!
+ results = self._filter_results(results) # More API fun please!
return results
def locations(self, location):
@@ -192,7 +195,6 @@ def episodes(self, guide_id):
def _map_listing(self, listing):
# We've already checked 'guide_id' exists
- #url = 'http://opml.radiotime.com/Tune.ashx?id=%s' % listing['guide_id']
return {'text': listing.get('name', '???'),
'guide_id': listing['guide_id'],
'type': 'audio',
@@ -267,7 +269,7 @@ def _tunein(self, variant, args):
data = response.json()
if (data['head']['status'] != '200'):
raise requests.exceptions.HTTPError(data['head']['status'],
- data['head']['fault'])
+ data['head']['fault'])
return data['body']
except Exception as e:
logger.error('Tunein request failed: %s', e)
@@ -277,7 +279,7 @@ def _tunein(self, variant, args):
def _get_playlist(self, uri):
logger.debug('Playlist request: %s', uri)
try:
- # Defer downloading the body until know it's not a stream
+ # Defer downloading the body until know it's not a stream
response = requests.get(uri, timeout=self._timeout, stream=True)
response.raise_for_status()
content_type = response.headers.get('content-type', 'audio/mpeg')
Please sign in to comment.
Something went wrong with that request. Please try again.