-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlobsters-bisque.py
executable file
·60 lines (48 loc) · 1.75 KB
/
lobsters-bisque.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
import feedparser
import requests
import time
# Constants
LOBSTERS_FEED_URL = 'https://lobste.rs/rss'
LOBSTERS_MINIMUM_SCORE = 10
# Functions
def fetch_article_score(url):
response = requests.get(url + '.json')
return response.json()['score']
def fetch_all_articles(url=LOBSTERS_FEED_URL):
feed = feedparser.parse(url)
for entry in feed.entries:
yield {
'title' : entry.title,
'author' : entry.author.split('@')[0],
'link' : entry.comments,
'published': entry.published,
'timestamp': entry.published_parsed,
'guid' : entry.guid,
'score' : fetch_article_score(entry.comments),
}
time.sleep(0.5) # Work around rate limit
def write_articles_feed(articles):
print('''<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Lobsters</title>
<link>https://lobste.rs</link>
<description></description>''')
for article in sorted(articles, key=lambda a: a['timestamp'], reverse=True):
print('''<item>
<title>{article_title}</title>
<author>{article_author}</author>
<link>{article_link}</link>
<guid isPermaLink="false">{article_guid}</guid>
<pubDate>{article_published}</pubDate>
</item>'''.format(article_title = article['title'],
article_author = article['author'],
article_link = article['link'],
article_guid = article['guid'],
article_published = article['published']))
print('''</channel>
</rss>''')
# Main Execution
if __name__ == '__main__':
write_articles_feed(a for a in fetch_all_articles() if a['score'] > LOBSTERS_MINIMUM_SCORE)