From a58d571cc858513104cb49db48ed258b996db1c2 Mon Sep 17 00:00:00 2001 From: kerozene Date: Sun, 13 Dec 2015 14:48:39 +1100 Subject: [PATCH] SpiffyTitles: improve Reddit handler template tags - {{age}} 'today', 'yesterday' - {{title}} in commentTemplate is thread title --- config.py | 8 ++++---- plugin.py | 14 ++++++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/config.py b/config.py index fb5ae75..82b49fe 100644 --- a/config.py +++ b/config.py @@ -186,16 +186,16 @@ conf.registerChannelValue(SpiffyTitles.reddit, 'enabled', registry.Boolean(True, _("""Whether to add additional info about Reddit links."""))) conf.registerChannelValue(SpiffyTitles.reddit, 'linkThreadTemplate', - registry.String(u"/r/{{subreddit}}{% if title %} :: {{title}}{% endif %} :: {{score}} points ({{percent}}) :: {{comments}} comments :: Posted by {{author}} {{age}} ago{% if url %} :: {{url}} ({{domain}}){% endif %}", _("""Template used for Reddit title responses"""))) + registry.String(u"/r/{{subreddit}}{% if title %} :: {{title}}{% endif %} :: {{score}} points ({{percent}}) :: {{comments}} comments :: Posted {{age}} by {{author}}{% if url %} :: {{url}} ({{domain}}){% endif %}", _("""Template used for Reddit link thread title responses"""))) conf.registerChannelValue(SpiffyTitles.reddit, 'textThreadTemplate', - registry.String(u"/r/{{subreddit}}{% if extract %} :: {{extract}}{% endif %} :: {{score}} points ({{percent}}) :: {{comments}} comments :: Posted by {{author}} {{age}} ago", _("""Template used for Reddit title responses"""))) + registry.String(u"/r/{{subreddit}}{% if title %} :: {{title}}{% endif %}{% if extract %} :: {{extract}}{% endif %} :: {{score}} points ({{percent}}) :: {{comments}} comments :: Posted {{age}} by {{author}}", _("""Template used for Reddit text thread title responses"""))) conf.registerChannelValue(SpiffyTitles.reddit, 'commentTemplate', - registry.String(u"/r/{{subreddit}}{% if extract %} :: {{extract}}{% endif %} :: {{score}} points :: Posted by {{author}} {{age}} ago", _("""Template used for Reddit title responses"""))) + registry.String(u"/r/{{subreddit}}{% if extract %} :: {{extract}}{% endif %} :: {{score}} points :: Posted {{age}} by {{author}} on \"{{title}}\"", _("""Template used for Reddit comment title responses"""))) conf.registerChannelValue(SpiffyTitles.reddit, 'userTemplate', - registry.String(u"/u/{{user}}{% if gold %} :: (GOLD{% if mod %}, MOD{% endif %}){% endif %} :: Joined: {{created}} :: Link karma: {{link_karma}} :: Comment karma: {{comment_karma}}", _("""Template used for Reddit title responses"""))) + registry.String(u"/u/{{user}}{% if gold %} :: (GOLD{% if mod %}, MOD{% endif %}){% endif %} :: Joined: {{created}} :: Link karma: {{link_karma}} :: Comment karma: {{comment_karma}}", _("""Template used for Reddit user page title responses"""))) conf.registerChannelValue(SpiffyTitles.reddit, 'maxChars', registry.Integer(400, _("""Length of response (title/extract will be cut to fit)."""))) diff --git a/plugin.py b/plugin.py index bcac7fa..d99e0be 100644 --- a/plugin.py +++ b/plugin.py @@ -966,7 +966,9 @@ class SpiffyTitles(callbacks.Plugin): if response: try: if link_type == "thread": data = response[0]['data']['children'][0]['data'] - if link_type == "comment": data = response[1]['data']['children'][0]['data'] + if link_type == "comment": + data = response[1]['data']['children'][0]['data'] + data['title'] = response[0]['data']['children'][0]['data']['title'] if link_type == "user": data = response['data'] except KeyError as e: self.log.error("SpiffyTitles: KeyError parsing Reddit JSON response: %s" % (str(e))) @@ -979,9 +981,13 @@ class SpiffyTitles(callbacks.Plugin): today = datetime.datetime.now(pytz.UTC).date() created = datetime.datetime.fromtimestamp(data['created_utc'], pytz.UTC).date() age_days = (today - created).days - age = '{}d'.format(age_days % 365) - if age_days > 365: - age = '{}y, '.format(age_days / 365) + age + if age_days == 0: age = "today" + elif age_days == 1: age = "yesterday" + else: + age = '{}d'.format(age_days % 365) + if age_days > 365: + age = '{}y, '.format(age_days / 365) + age + age = age + " ago" if link_type == "thread": link_type = "linkThread" if data['is_self']: