Skip to content

Instantly share code, notes, and snippets.

@eduardostalinho
Created February 4, 2016 16:39
Show Gist options
  • Select an option

  • Save eduardostalinho/47ce14c0c2d1b2ac4dbd to your computer and use it in GitHub Desktop.

Select an option

Save eduardostalinho/47ce14c0c2d1b2ac4dbd to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import time, os, sys, asyncio
from collections import deque
import feedparser
class FeedBloom(object):
entries = deque()
_config_dir = os.path.join(os.environ.get('HOME'), '.config/feedbloom/')
feeds_file = os.path.join(_config_dir, 'feeds.txt')
def _get_urls(self):
with open(self.feeds_file, 'r') as fd:
self.urls = [f.strip() for f in fd.readlines()]
async def get_entries(self):
while True:
self._get_urls()
_entries = []
for url in self.urls:
content = feedparser.parse(url)
_entries += content.entries
entries = self.sort_entries(_entries)
self.limit_entries(entries)
def sort_entries(self, entries):
return sorted(entries, key=lambda e: e.updated_parsed, reverse=True)
def limit_entries(self, entries):
if entries[:10] != self.entries:
self.entries = deque(reversed(entries[:10]))
def format_time(self, time):
return '{time.tm_mday:0>2}/{time.tm_mon:0>2}/{time.tm_year:0>2} {time.tm_hour:0>2}:{time.tm_min:0>2}'.format(time=time)
def format_entry(self, entry):
return '{} - {}'.format(self.format_time(entry.updated_parsed), entry.title)
async def print(self):
while True:
print('{}'.format(self.format_entry(self.entries[0])), end="\r")
time.sleep(1)
self.entries.rotate()
def run(self):
loop = asyncio.get_event_loop()
loop.run_until_complete(self.get_entries())
self.print()
loop.run_forever()
if __name__ == '__main__':
feedbloom = FeedBloom()
feedbloom.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment