-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathbird.py
executable file
·143 lines (122 loc) · 4.21 KB
/
bird.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
import os
import sys
import ConfigParser
import tweepy
class Bird(object):
def __init__(self):
self.consumer_key = None
self.consumer_secret = None
self.access_token = None
self.access_token_secret = None
self.api = None
self._load_config()
def _load_config(self):
"""Parse configuration file, returns a list of keys"""
config = ConfigParser.ConfigParser()
# search for ~/.trickybird
if os.path.isfile(os.path.join(os.path.expanduser("~"), ".twitter")):
conffile = os.path.join(os.path.expanduser("~"), ".twitter")
else:
print("Couldn't find the config file")
sys.exit(1)
config.read(conffile)
keys = []
try:
for attr in ['consumer_key', 'consumer_secret', 'access_token', 'access_token_secret']:
setattr(self, attr, config.get("twitter", attr))
except ConfigParser.NoOptionError:
pass
def _authenticate(self):
"""
Authenticate on twitter
"""
auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret)
auth.set_access_token(self.access_token, self.access_token_secret)
self.api = tweepy.API(auth)
def get_profile_information(self, username):
"""
Get profile information on an account
"""
if self.api is None:
self._authenticate()
return self.api.get_user(screen_name=username)
def get_user_tweets(self, username, since_id=None):
"""
Download all tweets for an user
Max is around 3200 tweets
"""
if self.api is None:
self._authenticate()
tweets = []
if since_id:
cursor = tweepy.Cursor(self.api.user_timeline, screen_name=username, since_id=since_id)
else:
cursor = tweepy.Cursor(self.api.user_timeline, screen_name=username)
for item in cursor.items():
tweets.append(item)
return tweets
def get_searched_tweets(self, hashtag, since_id=None):
"""
Search all tweets for a hashtag
"""
if self.api is None:
self._authenticate()
tweets = []
if since_id:
cursor = tweepy.Cursor(self.api.search, q=hashtag, count=100, since_id=since_id)
else:
cursor = tweepy.Cursor(self.api.search, q=hashtag, count=100)
try:
for item in cursor.items():
tweets.append(item)
except tweepy.error.TweepError:
print("Reached Twitter rate limit")
return tweets
def get_tweet(self, tweet_id):
"""
Return a Tweepy status for this id
"""
if self.api is None:
self._authenticate()
return self.api.get_status(tweet_id)
def get_followers(self, user):
"""
Return followers of this user
"""
if self.api is None:
self._authenticate()
followers = []
for page in tweepy.Cursor(self.api.followers, screen_name=user).pages():
followers.extend(page)
return followers
def get_followers_ids(self, user):
"""
Return followers of this user
"""
if self.api is None:
self._authenticate()
followers = []
for page in tweepy.Cursor(self.api.followers_ids, screen_name=user).pages():
followers.extend(page)
return followers
def get_following(self, user):
if self.api is None:
self._authenticate()
following = []
for page in tweepy.Cursor(self.api.friends, screen_name=user).pages():
following.extend(page)
return following
def get_following_ids(self, user):
if self.api is None:
self._authenticate()
following = []
for page in tweepy.Cursor(self.api.friends_ids, screen_name=user).pages():
following.extend(page)
return following
def get_following(self, user):
if self.api is None:
self._authenticate()
following = []
for page in tweepy.Cursor(self.api.friends, screen_name=user).pages():
following.extend(page)