-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
197 lines (115 loc) · 4.31 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import json
import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import os
import re
from lxml.html import parse
from http.client import BadStatusLine
import urllib
from urllib.request import urlopen
import requests
import sys
import datetime
credentials = {}
credentials['CONSUMER_KEY'] = 'vJsGsCMmiZapsIoEpYVahW76L'
credentials['CONSUMER_SECRET'] = 'SVHB6pHLVZPOoppBYywcsXlRkxDGBzdLufDwoingipDoEH66lX'
credentials['ACCESS_TOKEN'] = '3051117252-VLcksCEUJgk6kSRPjvkPqbZbT1jQC62cJHowgKq'
credentials['ACCESS_SECRET'] = '48VbkQrhuieXAVwQObdvVuIBGDT5rCYCKjuh7FhCdVbqC'
#Content-Type: text/html; charset=utf-8
FILE = open('file.json','a')
class listener(StreamListener):
def __init__(self, api=None):
super(listener, self).__init__()
self.num_tweets = 0
def on_connect(self):
print("Connection Established")
def on_status(self,status):
try:
link = re.search("(?P<url>https?://[^\s]+)", status.text)
if link is not None:
link = link.group("url")
str(link)
if (len(status.entities['urls']) > 0):
#print(status.entities['urls'][0]['expanded_url'])
link = status.entities['urls'][0]['expanded_url']
dict = {'screen_name': status.user.screen_name, 'tweet': status.text, 'Location': status.user.location, 'Link': link, 'Date': (status.created_at).strftime('%m/%d/%Y'),'Time': (status.created_at).strftime('%H:%M:%S') }
#if("coordinates" in status and status["coordinates"] != "null"):
# print(status["coordinates"])
print(status.coordinates)
print(status.geo)
#exit()
if self.num_tweets < 100:
FILE.write(json.dumps(dict))
FILE.write("\n")
self.num_tweets += 1
return True
else:
return False
except urllib.error.HTTPError:
print("There has been an error")
return True
def on_error(self,status_code):
if status_code == 420:
print("BIG BOI")
return False
auth = OAuthHandler(credentials['CONSUMER_KEY'], credentials['CONSUMER_SECRET'])
auth.set_access_token(credentials['ACCESS_TOKEN'],credentials['ACCESS_SECRET'])
api = tweepy.API(auth)
a = listener()
twitterStream = Stream(auth, a)
TP = twitterStream.filter(languages = ["en"], track = ["the","a"])
emptyTitle = False
data = []
with open('file.json', 'r+') as f:
for line in f:
#print(json.loads(line))
data.append(json.loads(line))
open('file.json', 'w').close()
for line in data:
link = line['Link']
#print(link)
if (link is not None):
if (len(link) > 23):
text = parse(urllib.request.urlopen(str(link)))
title = text.find(".//title").text
if title is not None:
line['title'] = title
FILE.write(json.dumps(line))
FILE.write("\n")
#with open('file.json', 'w') as g:
'''
for line in f:
data = json.loads(line)
data['title'] = "goodboy"
print(data)
f.seek(0)
f.write(json.dumps(data))
f.truncate()
print(json.loads(line))
data = json.loads(line)
link = data['Link']
print("HELLOOOOOOOO")
print(link)
print("HIIIIIIIIIII")
if (link is not None):
print(link)
if (len(link) < 23):
emptyTitle = True
text = parse(urllib.request.urlopen(str(link)))
title = text.find('.//title').text
print("JJJJJJJJJJ")
print(title)
print("KKKKKKKKKKK")
if emptyTitle == True:
title = ""
emptyTitle = False
if title is not None:
f.seek(0)
data['title'] = title
json.dump(data,f,indent = 4)
f.truncate()
'''
#if __name__ == "__main__":
# main()