-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
80 lines (63 loc) · 2.28 KB
/
tests.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
import tweepy #https://github.com/tweepy/tweepy
import os
import filecmp
import tweet_dumper
import json
# looks for json file with the proper credentials for the user
# {
# "consumer_key" : "value",
# "consumer_secret" : "value",
# "access_token" : "value",
# "access_token_secret" : "value"
# }
def get_credentials(filename):
with open(filename) as file:
cfg = json.load(file)
return cfg
def get_api(cfg):
auth = tweepy.OAuthHandler(cfg['consumer_key'], cfg['consumer_secret'])
auth.set_access_token(cfg['access_token'], cfg['access_token_secret'])
return tweepy.API(auth)
def batch_delete(api):
for status in tweepy.Cursor(api.user_timeline).items():
try:
api.destroy_status(status.id)
except:
print "Failed to delete:", status.id
def batch_tweet(api, startid):
tweet = "Test"
for i in range(startid,startid+10):
api.update_status(tweet+str(i))
def batch_tweet_from_file(api, filename):
for line in reversed(open(filename).readlines()):
api.update_status(line)
def compare(file1, file2):
different = filecmp.cmp(file1, file2)
return different
if __name__ == "__main__":
username = "TestsssTom"
cfg = get_credentials(username+".json")
api = get_api(cfg)
# desttroy all tweets on the page
batch_delete(api)
os.remove("TestingFiles/TestsssTom.txt")
print "Deleted all Tweets"
batch_tweet(api,0)
print "First set of tweets printed"
# get the tweets and match them with
tweet_dumper.get_all_tweets5args(username, api, 3240, "TestingFiles/TestsssTom.txt", "lastTweetScraped.txt")
firstPull = compare("TestingFiles/TestsssTom.txt", "TestingFiles/TestsssTomVerify1.txt")
if not firstPull:
print "scraped tweets do not match as expected"
else:
print "scraped tweets match desired output"
# batch tweet 10 more
batch_tweet(api,10)
tweet_dumper.get_all_tweets5args(username, api, 3240, "TestingFiles/TestsssTom.txt", "lastTweetScraped.txt")
fileappend = compare("TestingFiles/TestsssTom.txt", "TestingFiles/TestsssTomVerify2.txt")
if not fileappend:
print "appending new tweets not working"
else:
print "append tweets working"
# delete all the tweets again
batch_delete(api)