Created
March 21, 2012 01:03
-
-
Save nadia2012/2143266 to your computer and use it in GitHub Desktop.
project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pip install | |
git+git://github.com/joshthecoder/tweepy.git. | |
import tweepy | |
import sys | |
import webbrowser | |
import json | |
import osimport mimetypesfrom tweepy.binder | |
import bind_apifrom tweepy.error | |
import TweepErrorfrom tweepy.parsers | |
import ModelParser, RawParserfrom tweepy.utils | |
import list_to_csv | |
# This mode of authentication is the new preferred way | |
# of authenticating with Twitter. | |
class API(object): | |
"""Twitter API""" | |
def __init__(self, auth_handler=None, | |
host='api.twitter.com', search_host='search.twitter.com', | |
cache=None, secure=False, api_root='/1', search_root='', | |
retry_count=0, retry_delay=0, retry_errors=None, | |
parser=None): | |
self.auth = auth_handler | |
self.host = host | |
self.search_host = search_host | |
self.api_root = api_root | |
self.search_root = search_root | |
self.cache = cache | |
self.secure = secure | |
self.retry_count = retry_count | |
self.retry_delay = retry_delay | |
self.retry_errors = retry_errors | |
self.parser = parser or ModelParser() | |
consumer_key="" | |
consumer_secret="" | |
# The access tokens can be found on your applications's Details | |
# page located at https://dev.twitter.com/apps (located | |
# under "Your access token") | |
access_token="" | |
access_token_secret="" | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
api = tweepy.API(auth) | |
# If the authentication was successful, you should | |
# see the name of the account print out | |
print api.me().name | |
# If the application settings are set for "Read and Write" then | |
# this line should tweet out the message to your account's | |
# timeline. The "Read and Write" setting is on https://dev.twitter.com/apps | |
api.update_status('Updating using OAuth authentication via Tweepy!') | |
##Retrieves and shows at least the last 100 tweets for a username typed in by the application user. | |
#First, decide where about on your page you want your last tweet to display. Then paste in this line of HTML | |
# here shold be a while loop we think | |
""" statuses/user_timeline """ | |
user_timeline = bind_api( | |
path = '/statuses/user_timeline.json', | |
payload_type = 'status', payload_list = True, | |
allowed_param = ['id', 'user_id', 'screen_name', 'since_id', | |
'max_id', 'count', 'page', 'include_rts'] ) | |
[html]<div id=”twitter_update_list”> | |
</div>[/html] | |
#On the 2nd line of code where it says 12345.json, you need to replace 12345 with your twitter username | |
consumer_key.json | |
[js]<script type=”text/javascript” src=”http://twitter.com/javascripts/blogger.js”> | |
</script> | |
<script type=”text/javascript” src=”http://twitter.com/statuses/user_timeline/12345.json?callback=twitterCallback2&count=1″> | |
</script>[/js] | |
##Show all the mentions in the user timeline with a count of how many times each one occurred | |
""" statuses/mentions """ | |
mentions = bind_api( | |
path = '/statuses/mentions.json', | |
payload_type = 'status', payload_list = True, | |
allowed_param = ['since_id', 'max_id', 'count', 'page'], | |
require_auth = True ) | |
##Show all the hashtags in the user timeline with a count of how many times each one occurred. | |
#s = 'this_is_a_twitter_hashtag_#'; | |
#s = s.find('#') | |
#print s | |
#this php source code for get count hashtag(#) twitter | |
<?php | |
global $total, $hashtag; | |
//$hashtag = '#supportvisitbogor2011'; | |
$hashtag = '#australialovesjustin'; | |
$total = 0; | |
function getTweets($hash_tag, $page) { | |
global $total, $hashtag; | |
$url = 'http://search.twitter.com/search.json?q='.urlencode($hash_tag).'&'; | |
$url .= 'page='.$page; | |
$ch = curl_init($url); | |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
$json = curl_exec ($ch); | |
curl_close ($ch); | |
//echo "<pre>"; | |
//$json_decode = json_decode($json); | |
//print_r($json_decode->results); | |
$json_decode = json_decode($json); | |
$total += count($json_decode->results); | |
if($json_decode->next_page){ | |
$temp = explode("&",$json_decode->next_page); | |
$p = explode("=",$temp[0]); | |
getTweets($hashtag,$p[1]); | |
} | |
} | |
getTweets($hashtag,1); | |
echo $total; | |
?> | |
##User can select the list of hashtags/mentions and all tweets containing the hashtags/mentions are shown. | |
# Query terms | |
Q = sys.argv[1:] | |
# Get values from your application settings. | |
# Get values from the "My Access Token" link located in the | |
# margin of your application details, or perform the full OAuth | |
# dance. | |
class CustomStreamListener(tweepy.StreamListener): | |
def on_status(self, status): | |
# We'll simply print some values in a tab-delimited format | |
# suitable for capturing to a flat file but you could opt | |
# store them elsewhere, retweet select statuses, etc. | |
try: | |
print "%s\t%s\t%s\t%s" % (status.text, | |
status.author.screen_name, | |
status.created_at, | |
status.source,) | |
except Exception, e: | |
print >> sys.stderr, 'Encountered Exception:', e | |
pass | |
def on_error(self, status_code): | |
print >> sys.stderr, 'Encountered error with status code:', status_code | |
return True # Don't kill the stream | |
def on_timeout(self): | |
print >> sys.stderr, 'Timeout...' | |
return True # Don't kill the stream | |
# Create a streaming API and set a timeout value of 60 seconds. | |
streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60 | |
# Optionally filter the statuses you want to track by providing a list | |
# of users to "follow". | |
print >> sys.stderr, 'Filtering the public timeline for "%s"' % (' '.join(sys.argv[1:]),) | |
streaming_api.filter(follow=None, track=Q) | |
##Proper error handling and input validation | |
class TweepError(Exception): | |
"""Tweepy exception""" | |
def __init__(self, reason, response=None): | |
self.reason = unicode(reason) | |
self.response = response | |
def __str__(self): | |
return self.reason | |
##(optional) Be able to login with your account to retrieve and send direct messages | |
""" direct_messages """ | |
direct_messages = bind_api( | |
path = '/direct_messages.json', | |
payload_type = 'direct_message', payload_list = True, | |
allowed_param = ['since_id', 'max_id', 'count', 'page'], r | |
equire_auth = True | |
) | |
""" direct_messages/show """ | |
get_direct_message = bind_api( | |
path = '/direct_messages/show/{id}.json', | |
payload_type = 'direct_message', | |
allowed_param = ['id'], | |
require_auth = True | |
) | |
""" direct_messages/sent """ | |
sent_direct_messages = bind_api( | |
path = '/direct_messages/sent.json', | |
payload_type = 'direct_message', payload_list = True, | |
allowed_param = ['since_id', 'max_id', 'count', 'page'], | |
require_auth = True | |
) | |
""" direct_messages/new """ | |
send_direct_message = bind_api( | |
path = '/direct_messages/new.json', | |
method = 'POST', | |
payload_type = 'direct_message', | |
allowed_param = ['user', 'screen_name', 'user_id', 'text'], | |
require_auth = True | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment