This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitter.py
88 lines (81 loc) · 2.5 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
76
77
78
79
80
81
82
83
84
85
86
87
88
import tweepy
import engagement_api
from datetime import datetime, timedelta
with open('app_secret', 'r') as f:
consumer_key, consumer_secret = [line.strip()
for line in f.readlines()[:2]]
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
try:
with open('token_secret', 'r') as f:
access_token, access_token_secret = [
line.strip() for line in f.readlines()[:2]]
auth.set_access_token(access_token, access_token_secret)
except:
redirect_url = auth.get_authorization_url()
print(redirect_url)
verifier = input('Verifier: ')
try:
auth.get_access_token(verifier)
except:
print('Failed to retrieve access token.')
with open('token_secret', 'w') as f:
f.write(auth.access_token)
f.write('\n')
f.write(auth.access_token_secret)
API = tweepy.API(auth)
try:
API.engagement
except:
API.engagement = engagement_api.EngagementAPI(auth=auth)
print("Plugging in our own engagement API")
me = API.me()
def get_tweets():
cursor = API.user_timeline(id=me.id, count=50)
min = datetime.today() - timedelta(days=7)
tweets = []
for tweet in cursor:
if tweet.created_at < min:
break
tweets.append({
'text': tweet.text,
'time': tweet.created_at,
'retweets': tweet.retweet_count,
'favorites': tweet.favorite_count,
'id': tweet.id_str
})
return tweets
def get_tweets_plus():
cursor = API.user_timeline(id=me.id, count=50)
min = datetime.today() - timedelta(days=7)
tweets = []
for tweet in cursor:
if tweet.created_at < min:
break
tweets.append({
'text': tweet.text,
'time': tweet.created_at,
'retweets': tweet.retweet_count,
'favorites': tweet.favorite_count,
'id': tweet.id_str
})
e = API.engagement.totals(tweet_ids=[
t['id'] for t in tweets
],
engagement_types=[
'impressions',
'engagements',
'replies'
],
groupings={
'_': {
'group_by': [
'tweet.id',
'engagement.type'
]
}
})
for i, tweet in enumerate(tweets):
tweets[i]['impressions'] = e['_'][tweet['id']]['impressions']
tweets[i]['engagements'] = e['_'][tweet['id']]['engagements']
tweets[i]['replies'] = e['_'][tweet['id']]['replies']
return tweets