|
| 1 | +# coding: utf-8 |
| 2 | + |
1 | 3 | import logging
|
2 | 4 | import shortener
|
3 | 5 | import timeago
|
|
9 | 11 | from helper import development
|
10 | 12 | from apis.telegram import send_message
|
11 | 13 |
|
| 14 | +TWO_HOURS = datetime.timedelta(hours=2) |
| 15 | +TWO_DAYS = datetime.timedelta(days=2) |
12 | 16 |
|
13 | 17 | class StoryPost(ndb.Model):
|
14 | 18 | title = ndb.StringProperty()
|
@@ -76,8 +80,18 @@ def add(cls, story):
|
76 | 80 | published = datetime.datetime.fromtimestamp(story.get('time'))
|
77 | 81 | ago = timeago.format(now, published)
|
78 | 82 |
|
| 83 | + # Add 🔥 emoji if story is hot and gained required score in less than 2 hours, |
| 84 | + # or add ❄️ if it took it more than 2 days |
| 85 | + status_emoji = '' |
| 86 | + delta = now - published |
| 87 | + if delta <= TWO_HOURS: |
| 88 | + status_emoji = '🔥 ' |
| 89 | + elif delta >= TWO_DAYS: |
| 90 | + status_emoji = '❄️ ' |
| 91 | + |
79 | 92 | # Add title
|
80 |
| - message = '<b>{title}</b> (Score: {score}+ {ago})\n\n'.format(ago=ago, **story) |
| 93 | + message = '<b>{title}</b> ({status_emoji}Score: {score}+ {ago})\n\n'.format( |
| 94 | + ago=ago, status_emoji=status_emoji, **story) |
81 | 95 |
|
82 | 96 | # Add link
|
83 | 97 | message += '<b>Link:</b> {}\n'.format(short_url)
|
@@ -107,8 +121,8 @@ def add(cls, story):
|
107 | 121 | if result and result.get('ok'):
|
108 | 122 | telegram_message_id = result.get('result').get('message_id')
|
109 | 123 | post = cls(id=story_id, title=story.get('title'), url=story.get('url'),
|
110 |
| - score=story.get('score'), text=story.get('text'), |
111 |
| - short_url=short_url, short_hn_url=short_hn_url, |
112 |
| - message=message, telegram_message_id=telegram_message_id) |
| 124 | + score=story.get('score'), text=story.get('text'), |
| 125 | + short_url=short_url, short_hn_url=short_hn_url, |
| 126 | + message=message, telegram_message_id=telegram_message_id) |
113 | 127 | post.put()
|
114 | 128 | post.add_memcache()
|
0 commit comments