-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.py
75 lines (63 loc) · 2.68 KB
/
twitter.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
import tweepy
def createapi():
"""
Creates the API variable for twitter functions
:return: api object
"""
consumer_key = 'YOURKEY'
consumer_secret = 'YOURSECRET'
access_token = 'YOURTOKEN'
access_token_secret = 'YOURSECRETTOKEN'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
return tweepy.API(auth, wait_on_rate_limit=True)
def grabaccountdata(handle, api):
"""
Collects user account data from the specified handle
:param handle: a twitter handle string
:param api: api connection
:return: a tuple of user account data
"""
try:
# create user object and store user account data
user = api.get_user(handle)
name_actual = user.name
numfollowers = user.followers_count
numfriends = user.friends_count
numtweets = user.statuses_count
creationdate = dateonly(str(user.created_at))
accountdata = (handle, name_actual, numfollowers, numfriends, numtweets, creationdate)
return accountdata
except RateError:
raise str('Twitter or rate limit error. Number of API calls: ' + apicallcount)
def grabtweetdata(handle, api, sinceid):
"""
Collects initial tweet data from the specified handle
:param sinceid: specified if onlt new tweets are required
:param handle: a twitter handle string
:param api: api connection
:return: a list of tuples containing tweet data
"""
if sinceid ==0:
apicall = tweepy.Cursor(api.user_timeline, id='@' + handle, tweet_mode='extended',
include_rts=False).items()
else:
apicall = tweepy.Cursor(api.user_timeline, id='@' + handle, since_id=sinceid, tweet_mode='extended',
include_rts=False).items()
tweets = []
try:
# 3,200 tweet limit applies, even when not including retweets.
for tweet in apicall:
tweetdata = tuple() # tuple must be reinitialised before populating since it is immutable
id = tweet._json['id_str']
createdat = tweet._json['created_at']
tweettext_raw = tweet.full_text
# coords = tweet._json['coordinates']
# location = tweet._json['place']
numretweets = tweet._json['retweet_count']
numfavourites = tweet._json['favorite_count']
tweetdata = (handle, id, createdat, tweettext_raw, numretweets, numfavourites)
tweets.append(tweetdata[:])
return tweets
except RateError:
raise str('Twitter or rate limit error.')