blob: 6894d6d17000e22c2bef12253a38e1c6a3e5cfeb (
plain) (
blame)
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
|
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
|