Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
Implement a timer that prints out the top five newly submitted HN sto…
Browse files Browse the repository at this point in the history
…ries every 5 minutes
  • Loading branch information
marvinpinto committed Mar 18, 2016
1 parent b39a7f4 commit 5b3b7c3
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions charlesbot_hello_world/helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from charlesbot.util.parse import does_msg_contain_prefix
import asyncio
import aiohttp
from aiocron import crontab


class HelloWorld(BasePlugin):

def __init__(self):
super().__init__("HelloWorld")
self.load_config()
self.schedule_timer_message()

def load_config(self): # pragma: no cover
config_dict = configuration.get()
Expand Down Expand Up @@ -78,3 +80,35 @@ def print_top_n_hn_stories(self, number_of_stories, raw_story_ids):
return SlackAttachment(fallback=formatted_msg,
text=formatted_msg,
mrkdwn_in=["text"])

@asyncio.coroutine
def get_all_hn_new_stories(self):
hn_new_stories_url = "https://hacker-news.firebaseio.com/v0/newstories.json"
response = yield from aiohttp.get(hn_new_stories_url)
if not response.status == 200:
text = yield from response.text()
self.log.error("URL: %s" % url)
self.log.error("Response status code was %s" % str(response.status))
self.log.error(response.headers)
self.log.error(text)
response.close()
return []
return (yield from response.json())

def schedule_timer_message(self):
"Print out the top five newly submitted HN stories every 5 minutes"
timer = crontab('*/5 * * * *', func=self.send_timer_message, start=False)
timer.start()

@asyncio.coroutine
def send_timer_message(self):
raw_story_ids = yield from self.get_all_hn_new_stories()
return_attachment = yield from self.print_top_n_hn_stories(5, raw_story_ids)
yield from self.slack.api_call(
'chat.postMessage',
channel="#general",
attachments=return_attachment,
as_user=False,
username="Hacker News",
icon_url="https://s3-us-west-2.amazonaws.com/slack-files2/bot_icons/2016-03-18/27749445461_48.png"
)

0 comments on commit 5b3b7c3

Please sign in to comment.