-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_api.py
73 lines (60 loc) · 2.45 KB
/
twitter_api.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
import argparse
from datetime import datetime, timedelta
import tweepy
class twitterApi:
def __init__(self, consumer_key, consumer_secret, access_token, access_token_secret):
self.consumer_key=consumer_key
self.consumer_secret=consumer_secret
self.access_token=access_token
self.access_token_secret=access_token_secret
def twitter_auth(self):
auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret)
auth.set_access_token(self.access_token, self.access_token_secret)
return tweepy.API(auth)
def delete_tweets(self, before=None):
api = self.twitter_auth()
if before is None:
before = datetime.now() - timedelta(days=365)
tweets = [x for x in tweepy.Cursor(api.user_timeline).items()]
for tweet in tweets:
if tweet.created_at < before:
tweet.destroy()
def unfollow(self, screen_name):
api = self.twitter_auth()
following = api.friends_ids(screen_name)
followers = api.followers_ids(screen_name)
for f in following:
if f not in followers:
api.destroy_friendship(f)
def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('--consumer_key',
dest='consumer_key',
default = None,
help='consumer key')
parser.add_argument('--consumer_secret',
dest='consumer_secret',
default = None,
help='consumer secret')
parser.add_argument('--access_token',
dest='access_token',
default = None,
help='access token')
parser.add_argument('--access_token_secret',
dest='access_token_secret',
default = None,
help='access token secret')
parser.add_argument('--screen_name',
dest='screen_name',
default = None,
help='screen name')
args, _ = parser.parse_known_args(argv)
twitter = twitterApi(args.consumer_key,
args.consumer_secret,
args.access_token,
args.access_token_secret)
twitter.delete_tweets()
if args.screen_name is not None:
twitter.unfollow(args.screen_name)
if __name__ == '__main__':
main()