class TestNewStories(unittest.TestCase): def setUp(self): self.hn = HackerNews() def test_new_stories(self): new_stories = self.hn.new_stories() self.assertIsInstance(new_stories, list) self.assertIsNotNone(new_stories)
class TestNewStories(unittest.TestCase): def setUp(self): self.hn = HackerNews() def test_new_stories(self): new_stories = self.hn.new_stories(limit=10) self.assertIsInstance(new_stories, list) self.assertIsInstance(new_stories[0], Item) self.assertIsNotNone(new_stories) def test_new_stories_raw(self): new_stories = self.hn.new_stories(raw=True) self.assertIsInstance(new_stories, list) self.assertIsInstance(new_stories[0], str) self.assertIsNotNone(new_stories) def tearDown(self): self.hn.session.close()
def process(self, msg): """ `hn:` top\n `hn: last """ params = msg.extract_parameters(self.parameters) from hackernews import HackerNews hn = HackerNews() [ msg.reply( "{title} - {score} - {url}".format(**hn.get_item(s).__dict__)) for s in (hn.new_stories(int(params['limit'])) if params['hn'] == "last" else hn.top_stories(int(params['limit']))) ] return True
from time import sleep from json import dumps from kafka import KafkaProducer from hackernews import HackerNews producer = KafkaProducer(bootstrap_servers=['localhost:9092'], value_serializer=lambda x: dumps(x).encode('utf-8')) hn=HackerNews() latest_id=0 last_10=hn.new_stories(limit=10) # print(last_10) print("Will publish last 10 stories initially, then will incrementally publish new story as it is posted on site.") for item in last_10: title=item.title id=item.item_id by=item.by time=item.submission_time a=time.strftime("%m/%d/%Y, %H:%M:%S") if(id>latest_id): latest_id=id data={'id':id,'title':title,'by':by,'time':a} print('publishing {}'.format(data)) producer.send('story',value=data) print('\n') while(True): story=hn.new_stories(limit=1) if(len(story)==0 or story[0].item_id<=latest_id): continue