forked from dialoguesystems/dialogue-datasets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_spider.py
55 lines (42 loc) · 1.5 KB
/
twitter_spider.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
# -*- coding: utf-8 -*-
import tweepy
import time
import pickle
import argparse
parser = argparse.ArgumentParser(description='distinct-n')
parser.add_argument('-data', type=str, default="twitter_ids.txt",
help='Path to the *-train.pt file from preprocess.py')
opt = parser.parse_args()
# these arguments can be accessed in facebook developer.
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
def main():
twitter_ids_lines = open(opt.data, "r").readlines()
utterance_ids = []
for twitter_ids_line in twitter_ids_lines:
ids = [int(_id) for _id in twitter_ids_line.strip().split(" ")]
utterance_ids += ids
id2text = {}
utterance_cnt = len(utterance_ids)
for index in range(0, utterance_cnt, 100):
id_list = utterance_ids[index: index+100]
try:
results = api.statuses_lookup(id_list)
for result in results:
id2text[result.id] = result.text.encode("utf-8").replace("\n", " ")
except:
continue
if index % 1000 == 0:
print("Crawlling index = ", index)
print("Percentage " + str(index * 1.0 / utterance_cnt) + " %")
time.sleep(5)
if index % 10000 == 0:
time.sleep(120)
pickle.dump(id2text, open("id2text.train.pkl", "w"))
if __name__ == '__main__':
main()