-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexist.py
91 lines (73 loc) · 2.39 KB
/
exist.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import tweepy
import json
import sys
from pymongo import MongoClient
def check_exist(input_file, output_file):
data = open('pwd/search.pwd').read()
infos = json.loads(data)
consumer_key = infos['consumer_key']
consumer_secret = infos['consumer_secret']
auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
f = open(output_file, 'w')
tweets = {}
for line in open(input_file):
items = line.split(',', 3)
tid = items[1]
tweets[int(tid)] = line
keys = sorted(tweets.keys())
i = 0
total = 0
count = 0
while i * 100 < len(tweets):
tids = keys[i * 100 : (i+1) * 100]
total += len(tids)
i += 1
results = api.statuses_lookup(tids, trim_user=True)
count += len(results)
for t in results:
f.write(tweets[t.id])
f.close()
print 'checked %d tweets, %d still exist' % (total, count)
def check_exist_in_db(source_db, target_db):
data = open('pwd/search.pwd').read()
infos = json.loads(data)
consumer_key = infos['consumer_key']
consumer_secret = infos['consumer_secret']
auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
tweets_dict = {}
tweets_list = []
client = MongoClient()
source = client[source_db]['tweets']
target = client[target_db]['tweets']
for tweet in source.find():
try:
tid = int(tweet['tweet_id'])
tweets_list.append(tid)
tweets_dict[tid] = tweet
except:
pass
i = 0
total = 0
count = 0
while i * 100 < len(tweets_list):
tids = tweets_list[i * 100 : (i+1) * 100]
total += len(tids)
# print tids
i += 1
results = api.statuses_lookup(tids, trim_user=True)
count += len(results)
for t in results:
print tweets_dict[t.id]
target.insert(tweets_dict[t.id])
print 'checked %d tweets, %d still exist' % (total, count)
if __name__ == '__main__':
# input_file = sys.argv[1]
# output_file = sys.argv[2]
# check_exist(input_file, output_file)
source_db = sys.argv[1]
target_db = sys.argv[2]
check_exist_in_db(source_db, target_db)