From ba12777444bc9f9b776cef7871aa259583465de7 Mon Sep 17 00:00:00 2001 From: Laszlo Zeke Date: Mon, 2 Apr 2018 23:50:56 +0200 Subject: [PATCH] Add /vodonly to omit ongoing streams from results --- README.md | 3 +++ TwitchRSS/twitchrss.py | 28 ++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2da87f9..a208839 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,9 @@ The engine is webapp2. A running version can be tried out at: https://twitchrss.appspot.com/vod/twitch +There is also a VOD only endpoint if you don't want to see ongoing streams which are known to break some readers: +https://twitchrss.appspot.com/vodonly/twitch + ### Deployment See how to deploy on [Google App Engine](https://cloud.google.com/appengine/docs/python/gettingstartedpython27/introduction). diff --git a/TwitchRSS/twitchrss.py b/TwitchRSS/twitchrss.py index 33c8302..5151cd3 100644 --- a/TwitchRSS/twitchrss.py +++ b/TwitchRSS/twitchrss.py @@ -48,6 +48,7 @@ class MainPage(webapp2.RequestHandler):

You can get RSS of broadcasts by subscribing to https://twitchrss.appspot.com/vod/<channel name>
For example: https://twitchrss.appspot.com/vod/riotgames

+ You can use the /vodonly handle to get only vods without ongoing streams. Not endorsed by Twitch.tv, just a fun project.
Project home

@@ -59,11 +60,14 @@ class MainPage(webapp2.RequestHandler): class RSSVoDServer(webapp2.RequestHandler): def get(self, channel): + self._get_inner(channel) + + def _get_inner(self, channel, add_live=True): userid_json = self.fetch_userid(channel) (channel_display_name, channel_id) = self.extract_userid(json.loads(userid_json)) channel_json = self.fetch_vods(channel_id) decoded_json = json.loads(channel_json) - rss_data = self.construct_rss(channel, decoded_json, channel_display_name) + rss_data = self.construct_rss(channel, decoded_json, channel_display_name, add_live) self.response.headers['Content-Type'] = 'application/rss+xml' self.response.write(rss_data) @@ -78,9 +82,9 @@ class RSSVoDServer(webapp2.RequestHandler): def fetch_or_cache_object(self, channel, key_prefix, url_template, cache_time): json_data = self.lookup_cache(channel, key_prefix) - if json_data == '': + if not json_data: json_data = self.fetch_json(channel, url_template) - if json_data == '': + if not json_data: self.abort(404) else: self.store_cache(channel, json_data, key_prefix, cache_time) @@ -126,7 +130,7 @@ class RSSVoDServer(webapp2.RequestHandler): def extract_userid(self, user_info): userlist = user_info.get('users') - if userlist is None or len(userlist) < 1: + if not userlist: logging.info('No such user found.') self.abort(404) # Get the first id in the list @@ -138,7 +142,7 @@ class RSSVoDServer(webapp2.RequestHandler): logging.warning('Userid is not found in %s' % user_info) self.abort(404) - def construct_rss(self, channel_name, vods_info, display_name): + def construct_rss(self, channel_name, vods_info, display_name, add_live=True): feed = Feed() # Set the feed/channel level properties @@ -150,10 +154,12 @@ class RSSVoDServer(webapp2.RequestHandler): # Create an item try: - if vods_info['videos'] is not None: + if vods_info['videos']: for vod in vods_info['videos']: item = {} if vod["status"] == "recording": + if not add_live: + continue link = "http://www.twitch.tv/%s" % channel_name item["title"] = "%s - LIVE" % vod['title'] item["category"] = "live" @@ -177,7 +183,13 @@ class RSSVoDServer(webapp2.RequestHandler): return feed.format_rss2_string() +class RSSVoDServerOnlyVoD(RSSVoDServer): + def get(self, channel): + self._get_inner(channel, add_live=False) + + app = webapp2.WSGIApplication([ Route('/', MainPage), - Route('/vod/', RSSVoDServer) -], debug=False) \ No newline at end of file + Route('/vod/', RSSVoDServer), + Route('/vodonly/', RSSVoDServerOnlyVoD) +], debug=False)