forked from mareksuscak/cs50
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
29 lines (21 loc) Β· 758 Bytes
/
Copy pathhelpers.py
File metadata and controls
29 lines (21 loc) Β· 758 Bytes
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
import feedparser
import urllib.parse
def lookup(geo):
"""Look up articles for geo"""
# Check cache
try:
if geo in lookup.cache:
return lookup.cache[geo]
except AttributeError:
lookup.cache = {}
# Replace special characters
escaped = urllib.parse.quote(geo, safe="")
# Get feed from Google
feed = feedparser.parse(f"https://news.google.com/news/rss/local/section/geo/{escaped}")
# If no items in feed, get feed from Onion
if not feed["items"]:
feed = feedparser.parse("http://www.theonion.com/feeds/rss")
# Cache results
lookup.cache[geo] = [{"link": item["link"], "title": item["title"]} for item in feed["items"]]
# Return results
return lookup.cache[geo]