-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.py
More file actions
74 lines (56 loc) · 2.16 KB
/
fetch.py
File metadata and controls
74 lines (56 loc) · 2.16 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
import configparser
import os
import tweepy
from utils import write
def fetch_tweet_and_replies(tweet_id, reply_ids):
# print('fetch_tweet_and_replies')
create_folders(tweet_id)
api = authenticate()
tweet = api.get_status(tweet_id)
persist_tweet(tweet, str(tweet.id), "source-tweet")
structure = dict()
structure[tweet_id] = list()
for reply_id in reply_ids.split(','):
reply = api.get_status(reply_id.strip())
structure[tweet_id].append(reply_id)
persist_tweet(reply, str(tweet.id), "replies")
persist_structure(tweet_id, structure)
def create_folders(tweet_id):
# print('create_folders')
tweet_id_as_string = str(tweet_id)
tweet_dir = "resources/dataset/rumoureval-data/random-rumours/" + tweet_id_as_string
src_tweet_dir = tweet_dir + "/source-tweet"
replies_tweet_dir = tweet_dir + "/replies"
try:
os.makedirs(tweet_dir)
os.makedirs(src_tweet_dir)
os.makedirs(replies_tweet_dir)
except FileExistsError:
return
def authenticate():
# print('authenticate')
config = configparser.ConfigParser()
config.read('config/twitter.ini')
consumer_key = config.get('Twitter', 'consumer_key')
consumer_secret = config.get('Twitter', 'consumer_secret')
access_key = config.get('Twitter', 'access_key')
access_secret = config.get('Twitter', 'access_secret')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
return tweepy.API(auth)
def persist_tweet(tweet, parent_tweet_id, folder):
# print('persist_tweet')
tweet_id_as_string = str(tweet.id)
tweet_dir = "resources/dataset/rumoureval-data/random-rumours/" + parent_tweet_id + "/" + folder
try:
write(tweet_dir + "/" + tweet_id_as_string + ".json", tweet._json)
except FileExistsError:
return
def persist_structure(tweet_id, structure):
# print('persist_structure')
tweet_id_as_string = str(tweet_id)
tweet_dir = "resources/dataset/rumoureval-data/random-rumours/" + tweet_id_as_string
try:
write(tweet_dir + "/structure.json", structure)
except FileExistsError:
return