-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterAPI.py
55 lines (45 loc) · 2.49 KB
/
twitterAPI.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
from twitter import api
# initialize api instance
twitter_api = api.Api(consumer_key='EBkDD4HtvWGK7jZNaDP9HmWdu',
consumer_secret='X1dDhfWZkH39ywSOhVsgIT9WUcGYXI2wRZZ6Z5uu2a6buJIEkP',
access_token_key='805207123504365568-df1kPDJDGcSsTXKYjkVeWJ2urywjXbA',
access_token_secret='qFPbr7Rn9YwcuKn43sgXOFS2mYjwUWITOmYgdNOrZUPeV')
# test authentication
# print(twitter_api.VerifyCredentials())
def buildTestSet(search_keyword):
tweets_fetched = twitter_api.GetSearch(search_keyword, count=1, result_type='recent', include_entities=True, return_json=True)
print("Fetched " + str(len(tweets_fetched["statuses"])) + " tweets for the term " + search_keyword)
return [{
"id": tweet["id_str"],
"text": tweet["text"],
"created_at": tweet["created_at"],
"retweeted": tweet["retweeted"],
"retweets": tweet["retweet_count"],
"favourites": tweet["favorite_count"],
"entities":{
"hastags": tweet["entities"]["hashtags"],
"url": tweet["entities"]["urls"],
"user_mentions": tweet["entities"]["user_mentions"]
},
"user": {
"screen_name": tweet["user"]["screen_name"],
"follower_count": tweet["user"]["followers_count"],
"friend_count": tweet["user"]["friends_count"]
},
"stock_price": None
} for tweet in tweets_fetched["statuses"]]
def tweet_to_string(tweet):
return "id: " + tweet["id"],
"created_at: " + tweet["created_at"], # when the tweet was tweeted
"text: " + tweet["text"], # content of the tweet
"hashtags: " + tweet["entities"]["hashtags"], # list of hashtags used ["text"]
"user_mentions: " + tweet["entities"]["user_mentions"], # list of usermentions ["screen_name"]
"urls: " + tweet["entities"]["urls"], # list of urls["expanded_url"]
"retweeted: " + tweet["retweeted"], # was this a retweet
"retweet_count: " + tweet["retweet_count"], # how many times was this retweeted
"favorite_count: " + tweet["favorite_count"], # how many times was this favourited
"screen_name: " + tweet["user"]["screen_name"], # handle of the user
"followers_count: " + tweet["user"]["followers_count"], # number of people who follow them
"friends_count: " + tweet["user"]["friends_count"] # number of people who they follow
tweets = buildTestSet("Bitcoin")
print(tweet_to_string(tweets[0]))