-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblob-test_v3.py
141 lines (118 loc) · 4.81 KB
/
blob-test_v3.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from textblob import TextBlob
import requests
import time
import re
import newspaper
import datetime
from locale import getdefaultlocale
#######################
# UPDATES
# ---------------------
# * Nested everything in a big function
# * Function accepts keyword and day.
# * day is a datetime object
# * Only process at most one page (simpler and saves time)
#######################
# ISSUES
#---------------
# * Within-24-hour check only accurate to 2 days, cuz timezone is ass
# * Date is none at times (possibly with special unicode characters)
########################
# FUTURE
# ----------------------
# * Speed crawling up
# * If it is sped up, include more than one page search but still have a cap
def go(keyword, query_date): # day is a datetime object
def run(**params):
print(URL.format(**params))
response = requests.get(URL.format(**params))
#print(response.content, response.status_code)
return response.content
'''mention kmp/while, .find/regex thing'''
def start_substrings(string):
l = []
for m in re.finditer('<a href=', string):
start = int(m.start())
end = int(m.end())
if string[end+1:end+8]== "/url?q=":
l.append((int(m.start()), int(m.end())))
return l
def sentiment(text):
return TextBlob(text).sentiment.polarity
current_sum = 0.0
relevant_article_count = 0
''' mention language locale setting '''
locale_language = getdefaultlocale()[0][:2] # not sure if always works
keyword_lowercase = keyword.lower()
URL = "https://www.google.ca/search?q={query}&safe=strict&hl=en&as_drrb=b&authuser=0&source=lnt&tbs=cdr%3A1%2Ccd_min%3A{start}%2Ccd_max%3A{end}&tbm=nws"
search_string = "" # only for google news
split_keyword = keyword.split()
for i in range(len(split_keyword)):
search_string += split_keyword[i]
if i != len(split_keyword)-1:
search_string += '+'
string_query_date = query_date.isoformat()
html_page = run(query=search_string, start = string_query_date, end = string_query_date)
html_page = str(html_page)
url_indices = start_substrings(html_page)
for start, end in url_indices:
i = start
while html_page[i] != '&':
i += 1
PREFIX_LENGTH = 16
print(html_page[start+PREFIX_LENGTH:i])
if "http://www.forbes.com" in html_page[start+PREFIX_LENGTH:i]:
print("SKIPPING FORBES CUZ IT'S SHIT")
print("-------------------------------")
continue
article = newspaper.Article(url=html_page[start+PREFIX_LENGTH:i], language=locale_language)
article.download()
date = None
try:
article.parse()
date = str(article.publish_date)
except:
print("ERROR: DOWNLOAD FAILED")
print("----------------------------------")
continue
if date == 'None':
print("FAILURE: UNSUCESSFUL DOWNLOAD. PROOF: date=", date)
print("----------------------------------")
continue
else:
print("date is", date)
print("SUCESSFUL")
print("date", date, date.split()[0])
dtobj=datetime.datetime.strptime(date.split()[0], "%Y-%m-%d")
print("dtobj", dtobj)
time = date.split()[1]
print("time", time)
if "-" not in time and "+" not in time:
tzhour = 0
tzmin = 0
print(tzhour, tzmin)
else:
tzhour, tzmin = time.split("-")[0].split("+")[-1].split(":")
tzhour, tzmin = -int(tzhour), -int(tzmin) #if -4:00, add 4 to get UTC
print(tzhour, tzmin)
article_delta = datetime.timedelta(hours=tzhour, minutes = max(1, tzhour/max(abs(tzhour), 1))*abs(tzmin))
print(article_delta)
local_delta = datetime.datetime.utcnow()-datetime.datetime.now()
if query_date+local_delta-dtobj-article_delta > datetime.timedelta(days=1):
print("TOO OLD: published", dtobj, "query", query_date)
print("----------------------------------")
continue
print("WE'RE DOING IT BOYS")
full_text = str(article.text) # not sure if str() is needed
current_sum += sentiment(full_text)
print("sentiment is", sentiment(full_text))
print("publish date is", date)
print("----------------------------------")
relevant_article_count += 1
print("Article count is", str(relevant_article_count)+".")
rating = current_sum/max(relevant_article_count, 1)
print("The total rating is", str(current_sum)+".")
print("The rating for", keyword, "is", str(rating)+".")
'''TEST'''
day = datetime.datetime.strptime("2016-05-21", "%Y-%m-%d")
go("Apple", day)