-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrayAPI.py
61 lines (54 loc) · 2.03 KB
/
GrayAPI.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import requests as req
import urllib as urlib
import Article
class GrayNews:
HOME = "https://news.google.com/"
WORLD = "https://news.google.com/topics/CAAqJggKIiBDQkFTRWdvSUwyMHZNRGx1YlY4U0FtVnVHZ0pKVGlnQVAB"
TECHNOLOGY = "https://news.google.com/topics/CAAqJggKIiBDQkFTRWdvSUwyMHZNRGRqTVhZU0FtVnVHZ0pKVGlnQVAB"
SCIENCE = "https://news.google.com/topics/CAAqJggKIiBDQkFTRWdvSUwyMHZNRFp0Y1RjU0FtVnVHZ0pKVGlnQVAB"
def __init__(self, search='Google'):
self.search = search
self.madeReq = False
self.reqerr = None
self.lnk = "https://news.google.com/search?q="
self.lnk = "https://news.google.com/search?q="+urlib.parse.quote(str(self.search).encode('utf-8'))
if search == 'GB#HOME':
self.lnk = self.HOME
#if search == GrayNews.HOME:
#self.lnk = GrayNews.HOME
#elif search == GrayNews.WORLD:
#self.lnk = GrayNews.WORLD
#elif search == GrayNews.TECHNOLOGY:
#self.lnk = GrayNews.TECHNOLOGY
#elif search == GrayNews.SCIENCE:
#self.lnk = GrayNews.SCIENCE
#else:
#print(search)
def getSource(self):
if self.madeReq:
return self.lnk
else:
return -1
def getRequestError(self):
if self.reqerr == None:
return None
else:
return self.reqerr
def makeRequest(self):
try:
r = req.get(self.lnk)
self.code = r.text
self.madeReq = True
except Exception as err:
self.reqerr = err
self.madeReq = False
return False
def getArticles(self):
indent = '<div class=\"NiLAwe y6IFtc R7GTQ keNKEd j7vNaf nID9nc\">'
endent = '</article>'
self.out = []
while self.code.find(indent) != -1:
self.code = self.code[self.code.find(indent):]
self.out.append(Article.Article(self.code[:self.code.find(endent)]))
self.code = self.code[self.code.find(endent):]
return self.out