-
Notifications
You must be signed in to change notification settings - Fork 0
/
colectorTuits.py
44 lines (34 loc) · 1.56 KB
/
colectorTuits.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
import requests
import re
import mySecrets
token = mySecrets.token_twitter
header = {}
header['Authorization'] = f"Bearer {token}"
def generate_file_content_tuits(name_file, query, mode = "w+"):
num_rows = 0
list_added_tweets = []
for i in range(3): # Para más posibilidades de encontrar tweets
if i == 1:
query = query + "&result_type=popular"
if i == 2:
query = query + "&result_type=recent"
r = requests.get(f'https://api.twitter.com/1.1/search/tweets.json?q={query}', headers = header)
json_ids = r.json()
file_twits = open("generated_files/" + name_file, mode)
for row in json_ids['statuses']:
id_tuit = row['id']
text = requests.get(f'https://api.twitter.com/2/tweets/{id_tuit}', headers=header).json()['data']['text']\
.replace("\xc3\xa1", 'a').replace("\xc3\xa9", 'e').replace("\xc3\xad", 'i')\
.replace("\xc3\xb3", 'o').replace("\xc3\xba", 'u').replace("\n", " . ")
text = re.sub("RT @..*: ", "", text) # Remove "retweet prefix"
text = re.sub("@[A-Za-z0-9]*", "", text) # Remove mentions
text = re.sub("http[://.A-Za-z0-9]*", "", text) # Remove url
text = text.replace("\n", " . ")
if list_added_tweets.__contains__(text):
# If the twwet already added
continue
num_rows += 1
list_added_tweets.append(text)
if text != '\n' and text != '':
file_twits.write(text + "\n")
return num_rows