Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 4 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Commits on Nov 26, 2011
@jesstess twitter_api.DONE hasn't been kept in sync with twitter_api, so get ri…
…d of it.
d53a684
@jesstess Move option parsing to its own file.
Attendees don't need to know about it, and it only adds
confusion/clutter.
d209ea0
@jesstess Add a --user-tweets command-line option.
Attendees don't have enough time to figure out how to do this
themselves.
339c0eb
Commits on Nov 27, 2011
@jesstess Change use of GetTrendsCurrent to custom GetTrendsWoeid.
GetTrendsCurrent wraps the GET trends request, which is now deprecated
and requires authorization
(https://dev.twitter.com/docs/api/1/get/trends).

Get trends has been replaced by GET trends/:woeid, where WOEID is the
Yahoo! Where On Earth ID for a location. python-twitter doesn't
currently support this; I've opened
http://code.google.com/p/python-twitter/issues/detail?id=217 with a
patch to add support.

In the meantime, because we already bundle the software attendees
install, we'll have them use a custom version of python-twitter that
includes the patch rather than monkeypatch here, to minimize the
amount of confusing code they have to see to make this work.

That means others wanting to use TwitterAPI will need to apply the
patch from ticket 217 to their version of python-twitter for
--trending-topics to work.
c6ac181
Showing with 46 additions and 132 deletions.
  1. +0 −98 twitter_api.DONE.py
  2. +11 −34 twitter_api.py
  3. +35 −0 twitter_functions.py
View
98 twitter_api.DONE.py
@@ -1,98 +0,0 @@
-import optparse
-import sys
-
-import twitter
-from util import encode
-
-def printTweet(tweet):
- """
- Format and print `tweet`.
- """
- print "@" + tweet.GetUser().GetScreenName() + ": " + encode(tweet.GetText())
-
-def search(searchTerm):
- """
- Print recent tweets containing `searchTerm`.
- """
- api = twitter.Api()
- tweets = api.GetSearch(searchTerm)
- for tweet in tweets:
- print encode(tweet.GetText())
-
-def trendingTopics():
- """
- Print the currently trending topics.
- """
- api = twitter.Api()
- trending_topics = api.GetTrendsCurrent()
- for topic in trending_topics:
- print encode(topic.name)
-
-def userTweets(username):
- """
- Print recent tweets by `username`.
- """
- api = twitter.Api()
- userTweets = api.GetUserTimeline(screen_name=username)
- for tweet in userTweets:
- printTweet(tweet)
-
-def trendingTweets():
- """
- Print tweets for all the trending topics.
- """
- api = twitter.Api()
-
- trending_topics = api.GetTrendsCurrent()
- tweets = []
- # To add some variety, let's round-robin through the trending
- # topics, displaying a tweet from each until we run out of tweets.
- for topic in trending_topics:
- tweets.append((topic, api.GetSearch(topic.name)))
-
- while True:
- for topic, topic_tweets in tweets:
- if topic_tweets:
- printTweet(topic_tweets.pop())
- else:
- return
-
-def main(args):
- parser = optparse.OptionParser("""Usage: %prog [-s <search term> | -t | -u <username>]""")
-
- parser.add_option("-s", "--search",
- type="string",
- action="store",
- dest="search_term",
- default=None,
- help="Display tweets containing a particular string.")
- parser.add_option("-t", "--trending-topics",
- action="store_true",
- dest="trending_topics",
- default=False,
- help="Display the trending topics.")
- parser.add_option("-u", "--user",
- type="string",
- action="store",
- dest="username",
- default=None,
- help="Display tweets for a particular public user.")
- parser.add_option("-w", "--trending-tweets",
- action="store_true",
- dest="trending_tweets",
- default=None,
- help="Display the tweets from trending topics.")
-
- (opts, args) = parser.parse_args(args)
-
- if opts.search_term:
- search(opts.search_term)
- elif opts.trending_topics:
- trendingTopics()
- elif opts.username:
- userTweets(opts.username)
- elif opts.trending_tweets:
- trendingTweets()
-
-if __name__ == "__main__":
- main(sys.argv[1:])
View
45 twitter_api.py
@@ -1,38 +1,7 @@
import optparse
import sys
-import twitter
-import util
-
-def search(searchTerm):
- """
- Print recent tweets containing `searchTerm`.
- """
- api = twitter.Api()
- tweets = api.GetSearch(searchTerm)
- for tweet in tweets:
- util.safe_print(tweet.GetText())
-
-def trendingTopics():
- """
- Print the currently trending topics.
- """
- api = twitter.Api()
- trending_topics = api.GetTrendsCurrent()
- for topic in trending_topics:
- util.safe_print(topic.name)
-
-def userTweets(username):
- """
- Print recent tweets by `username`.
- """
- pass
-
-def trendingTweets():
- """
- Print tweets for all the trending topics.
- """
- pass
+import twitter_functions
def main(args):
parser = optparse.OptionParser("""Usage: %prog [-s <search term> | -t | -u <username>]""")
@@ -48,13 +17,21 @@ def main(args):
dest="trending_topics",
default=False,
help="Display the trending topics.")
+ parser.add_option("-u", "--user-tweets",
+ type="string",
+ action="store",
+ dest="user_tweets",
+ default=False,
+ help="Display a user's tweets.")
(opts, args) = parser.parse_args(args)
if opts.search_term:
- search(opts.search_term)
+ twitter_functions.search(opts.search_term)
elif opts.trending_topics:
- trendingTopics()
+ twitter_functions.trendingTopics()
+ elif opts.user_tweets:
+ twitter_functions.userTweets(opts.user_tweets)
if __name__ == "__main__":
main(sys.argv[1:])
View
35 twitter_functions.py
@@ -0,0 +1,35 @@
+import twitter
+import util
+
+BOSTON_WOEID = 2367105
+
+def search(searchTerm):
+ """
+ Print recent tweets containing `searchTerm`.
+ """
+ api = twitter.Api()
+ tweets = api.GetSearch(searchTerm)
+ for tweet in tweets:
+ util.safe_print(tweet.GetText())
+
+def trendingTopics():
+ """
+ Print the currently trending topics.
+ """
+ api = twitter.Api()
+
+ trending_topics = api.GetTrendsWoeid(BOSTON_WOEID)
+ for topic in trending_topics:
+ util.safe_print(topic.name)
+
+def userTweets(username):
+ """
+ Print recent tweets by `username`.
+ """
+ pass
+
+def trendingTweets():
+ """
+ Print tweets for all the trending topics.
+ """
+ pass

No commit comments for this range

Something went wrong with that request. Please try again.