-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.py
More file actions
182 lines (147 loc) · 7.87 KB
/
Copy pathserver.py
File metadata and controls
182 lines (147 loc) · 7.87 KB
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
import requests
import os
import json
import time
import re
import copy as copy
from datetime import datetime
from dataclasses import dataclass
import argparse
os.environ['BEARER_TOKEN'] = 'AAAAAAAAAAAAAAAAAAAAAGakUQEAAAAAucI%2FuyC5RvtNok3cESZ93ITkRxQ%3DegMxLb1Q1A67ZK5AJJcghTtmyOryBWouBG6TSpLAjDWRSDI3CQ'
#print(os.environ)
# To set your environment variables in your terminal run the following line:
#'BEARER_TOKEN'= AAAAAAAAAAAAAAAAAAAAAGakUQEAAAAAucI%2FuyC5RvtNok3cESZ93ITkRxQ%3DegMxLb1Q1A67ZK5AJJcghTtmyOryBWouBG6TSpLAjDWRSDI3CQ
bearer_token = os.environ.get("BEARER_TOKEN")
def create_url():
#return "https://api.twitter.com/2/tweets/sample/stream"
return "https://api.twitter.com/2/tweets/sample/stream?tweet.fields=created_at"
def bearer_oauth(r):
"""
Method required by bearer token authentication.
"""
r.headers["Authorization"] = f"Bearer {bearer_token}"
r.headers["User-Agent"] = "v2SampledStreamPython"
return r
import spacy
import re
nlp = spacy.load("en_core_web_sm", disable=['parser', 'ner'])
regex = r"""(?i)\b((?:https?:(?:/{1,3}|[a-z0-9%])|[a-z0-9.\-]+[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop
|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)/)(?:[^\s()<>{}\[\]]+|\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\))+(?:\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])|(?:(?<!@)[a-z0-9]+(?:[.\-][a-z0-9]+)*[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)\b/?(?!@)))"""
def clean_text(text):
if type(text) != str:
text = text.decode("utf-8")
doc = re.sub(regex, '', text, flags=re.MULTILINE) # remove URLs
sentences = []
for sentence in doc.split("\n"):
if len(sentence) == 0:
continue
sentences.append(sentence)
doc = nlp("\n".join(sentences))
doc = " ".join([token.lemma_.lower().strip() for token in doc
if (not token.is_stop)
and (not token.like_url)
and (not token.lemma_ == "-PRON-")
and (not len(token) < 4)])
return doc
def transform_json(twitter_jsons):
"""
Takes list of Json string and converts it to [{"timestamp": <datetime>, "content": <content>}]
"""
final = []
temp = []
for twitter_json in twitter_jsons:
json_response = json.loads(twitter_json)
#print(json.dumps(json_response, indent=4, sort_keys=True))
#print(json.dumps(json_response))
json_data = json_response['data']
json_temp = ({k: v for k, v in json_data.items() if k in ('created_at', 'text')})
temp = json_temp.values()
keys_values = json_temp.items()
temp = {str(key): str(value) for key, value in keys_values}
temp['text'] = ''.join(filter(lambda character:ord(character) < 0x100,temp['text']))
tweet = {}
tweet['timestamp'] = datetime.strptime(temp['created_at'], '%Y-%m-%dT%H:%M:%S.%fZ')
tweet['content'] = ' '.join(temp['text'].split())
#tweet['content'] = clean_text(tweet['content'])
tweet['content'] = re.sub('@[^\s]+', '', tweet['content']) # Remove usernames
tweet['content'] = re.sub(r'\d+', '', tweet['content']) # Remove numbers
tweet['content'] = re.sub(r'[^\w\s]','',tweet['content'])
tweet['content'] = tweet['content'].replace("'", '')
tweet['content'] = re.sub(r'http\S+', '', tweet['content'])
tweet['content'] = re.sub(r'RT', '', tweet['content'])
#tweet['content'] = temp['text'].translate(str.maketrans('', '', string.punctuation))
#tweet['content'] = ''.join(filter(lambda character: ord(character) < 0x100, tweet['content']))
if tweet['content'].strip() == '':
continue
else:
#final += temp.values()
final.append(tweet)
#print('tweet: timestamp:', tweet['timestamp'], ' content: ', tweet['content'])
return final
def write_transformed_tweets_to_file(transformed_tweets):
"""
Converts list of tweets in [{"timestamp": <datetime>, "content": <content>}] format to tweets.txt in "<YYYY-MM-DD-HH-MM-SS>, <tweet text>"
"""
with open('tweets.txt', 'a') as log:
for transformed_tweet in transformed_tweets:
num_written = log.write('{}\n'.format(str(transformed_tweet['timestamp'].strftime("%Y-%m-%d-%H-%M-%S")) + ', ' + str(transformed_tweet['content'])))
if num_written == 0:
raise Exception(
"Cannot write to file. Disk full?")
def parse_from_twitter_stream(url):
"""
Transforms tweets from twitter sample stream into [{"timestamp": <datetime>, "content": <content>}]
"""
response = requests.request("GET", url, auth=bearer_oauth, stream=True)
print(response.status_code)
if response.status_code != 200:
raise Exception(
"Request returned an error: {} {}".format(
response.status_code, response.text
)
)
twitter_jsons = []
i = 0
for response_line in response.iter_lines():
if response_line:
twitter_jsons.append(response_line)
i += 1
if i > 100:
break;
result = transform_json(twitter_jsons)
print("done")
return result
def main():
url = create_url()
timeout = 0
parser = argparse.ArgumentParser(description='Transform twitter sample stream')
parser.add_argument('--filename', dest='file', type=argparse.FileType('r'))
args = parser.parse_args()
print(args.file)
transformed_json = []
while True:
print("Looping")
if args.file:
jsons = []
length = 0
for line in args.file:
length += 1
jsons.append(line)
args.file.seek(0)
transformed_json = transform_json(jsons)
print("json read complete. Lines Read: " + str(length))
break
else:
try:
transformed_json = parse_from_twitter_stream(url)
except Exception:
print("Error accessing twitter api. Sleeping 60 seconds and trying again")
time.sleep(60)
continue
try:
write_transformed_tweets_to_file(transformed_json)
except Exception:
print("Error writing tweets to file.")
time.sleep(10)
if __name__ == "__main__":
main()