SpiffyTitles: improve Reddit handler template tags

- {{age}} 'today', 'yesterday'
- {{title}} in commentTemplate is thread title
This commit is contained in:
kerozene 2015-12-13 14:48:39 +11:00
parent cf97950568
commit a58d571cc8
2 changed files with 14 additions and 8 deletions

View File

@ -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).""")))

View File

@ -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']: