-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
30 lines (23 loc) · 938 Bytes
/
search.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
import os
# the hide api key bs
from dotenv import load_dotenv
from googleapiclient.discovery import build
load_dotenv(".env")
my_api_key = os.getenv("SEARCH_PY_API_KEY")
cx_key = os.getenv("CX_KEY")
################################################################
resource = build("customsearch", "v1", developerKey=my_api_key).cse()
# search google with string and make sure has reddit in it
# return list of urls with reddit links
# they will do their magic with the reddit links
def return_links(search_string):
if not search_string.endswith(" reddit"):
search_string += " reddit"
result = resource.list(q=search_string, cx=cx_key).execute()
links = []
for item in result["items"]:
url = item["link"] # link is exact url
domain = item["displayLink"] # displayLink is domain
if domain.endswith("reddit.com") and "comments" in url:
links.append(url)
return links