/
twitter_functions.py
60 lines (50 loc) · 2.04 KB
/
twitter_functions.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
import twitter
import util
from config import *
BOSTON_WOEID = 2367105
api = twitter.Api(consumer_key=key,consumer_secret=secret,access_token_key=access_key,access_token_secret=access_secret)
def search(searchTerm):
"""
Print recent tweets containing `searchTerm`.
To test this function, at the command line run:
python twitter_api.py --search=<search term>
For example,
python twitter_api.py --search=python
"""
api = twitter.Api(consumer_key=key,consumer_secret=secret,access_token_key=access_key,access_token_secret=access_secret)
tweets = api.GetSearch(searchTerm)
for tweet in tweets:
util.safe_print(tweet.GetText())
def trendingTopics():
"""
Print the currently trending topics.
To test this function, at the command line run:
python twitter_api.py -t
"""
api = twitter.Api(consumer_key=key,consumer_secret=secret,access_token_key=access_key,access_token_secret=access_secret)
trending_topics = api.GetTrendsWoeid(BOSTON_WOEID)
for topic in trending_topics:
util.safe_print(topic.name)
def userTweets(username):
"""
Print recent tweets by `username`.
You may find the twitter.Api() function GetUserTimeline() helpful.
To test this function, at the command line run:
python twitter_api.py -u <username>
For example,
python twitter_api.py -u bostonpython
"""
api = twitter.Api(consumer_key=key,consumer_secret=secret,access_token_key=access_key,access_token_secret=access_secret)
user_tweet = api.GetUserTimeline(screen_name=username)
for tweet in user_tweet:
util.safe_print(tweet.GetText())
def trendingTweets():
"""
Print tweets for all the trending topics.
To test this function, at the command line run:
python twitter_api.py -w
"""
api = twitter.Api(consumer_key=key,consumer_secret=secret,access_token_key=access_key,access_token_secret=access_secret)
trending_topics = api.GetTrendsWoeid(BOSTON_WOEID)
for tweet in trending_topics:
util.safe_print(tweet.GetText())