diff options
author | mounderfod <mounderfod@gmail.com> | 2023-07-19 14:40:29 +0200 |
---|---|---|
committer | mounderfod <mounderfod@gmail.com> | 2023-07-19 14:40:29 +0200 |
commit | 328f2998b27ef189dac893d081d3626854fc20d4 (patch) | |
tree | 921f13ceb69f5bdadc9645326b2b924167be08f9 /news.py | |
download | gopherhole-328f2998b27ef189dac893d081d3626854fc20d4.tar.gz |
Initial commit
Diffstat (limited to 'news.py')
-rw-r--r-- | news.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/news.py b/news.py new file mode 100644 index 0000000..6894d6d --- /dev/null +++ b/news.py @@ -0,0 +1,34 @@ +import os +import urllib.parse +import html2text +import requests +from dotenv import load_dotenv +from pituophis import Item + +load_dotenv() + +def get_news(): + results = [] + data = requests.get(f"https://content.guardianapis.com/search?api-key={os.getenv('GUARDIAN_API')}&page-size=50").json() + results += [Item( + itype="0", + text=i['webTitle'], + path=f"/newstxt?article={urllib.parse.quote(i['id'], safe='')}", + host=os.getenv("HOSTNAME") + ) for i in data['response']['results']] + return results + + +def get_newstxt(article): + path = urllib.parse.unquote(article) + data = requests.get(f"https://content.guardianapis.com/{path}?api-key={os.getenv('GUARDIAN_API')}&show-fields=body").json() + h = html2text.HTML2Text() + h.use_automatic_links = True + h.images_to_alt = True + h.ignore_tables = True + h.ignore_links = True + h.emphasis_mark = "" + h.strong_mark = "" + text = f"{data['response']['content']['webTitle']}\n" + ("=" * 60) + "\n" + text += h.handle(data['response']['content']['fields']['body']) + return text \ No newline at end of file |