-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinks.py
More file actions
28 lines (22 loc) · 724 Bytes
/
Copy pathlinks.py
File metadata and controls
28 lines (22 loc) · 724 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
from tinydb import TinyDB, Query
import random
from GoogleNews import GoogleNews
# setting up database
links_db = TinyDB("links.json")
Topic = Query()
# setting up google news api
googlenews = GoogleNews(lang="en")
googlenews.setperiod("3")
def return_random_link(topic):
links = links_db.search(Topic.topic == topic)
if len(links) == 0:
# return the google news link
print("fetching google link")
googlenews.search(topic)
google_links = googlenews.result()
return google_links[0]["link"]
else:
print("fetching link from database")
choice = random.randrange(0, len(links) - 1)
return links[choice]["link"]
print(return_random_link("latinx"))