Skip to content

Instantly share code, notes, and snippets.

@bananos
Last active March 16, 2016 16:11
Show Gist options
  • Select an option

  • Save bananos/69f5726fe4b85edea04e to your computer and use it in GitHub Desktop.

Select an option

Save bananos/69f5726fe4b85edea04e to your computer and use it in GitHub Desktop.
Latest RSS news from console
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
import pytz
import feedparser
from urlparse import urlparse
from time import mktime, time
from datetime import datetime
# 8 hours
TM_DELTA = 3600 * 12
FEEDS = [
'http://news.finance.ua/ru/rss',
'http://www.epravda.com.ua/rss/',
]
def main():
"""
Simple news reader
"""
articles = []
for f_url in FEEDS:
d = {'feed': {}}
i = 0
while (not d['feed']) and i<10:
d = feedparser.parse(f_url)
i = i+1
uu = urlparse(f_url)
for e in d.entries:
articles.append({
"date": datetime.fromtimestamp(mktime(e.published_parsed)+3600*2),
"tm": mktime(e.published_parsed),
"title": e['title'],
"src": uu.netloc
})
# sort by pub_date
sorted_articles = sorted(articles, key=lambda k: k['tm'])
now = time()
for a in sorted_articles:
if now - a['tm'] < TM_DELTA:
print "%s | %s / %s" % (a['date'].strftime("%H:%M"), a['title'].encode('utf-8'), a['src'])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment