-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterutil.py
50 lines (37 loc) · 1.57 KB
/
twitterutil.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
"""Utility methods for posting to Twitter."""
import urllib2
import oauthutil
import models
from third_party import oauth
def get_user_by_token_key(key):
"""Gets the TwitterUser object associated with the given key name."""
return models.TwitterUser.get_by_key_name(key)
def set_status_by_user_id(status, user_id):
"""Set a status by user id. ID is expected to be in the datastore."""
key, secret = get_key_and_secret(user_id)
set_status(status, key, secret)
def get_key_and_secret(user_id):
"""Get the OAuth token key and secret for the given user id from the datastore."""
query = models.TwitterUser.all()
query.filter('user_id =', user_id)
user = query.get()
return user.key().name(), user.secret
def set_status(status, oauth_token, oauth_secret):
"""Set a Twitter status.
Args:
status: New Tweet, ideally < 140 characters.
oauth_token: Access token.
oauth_secret: Access token secret.
Returns: The server response.
"""
consumer = oauthutil.get_consumer()
token = oauth.OAuthToken(oauth_token, oauth_secret)
update_url = 'http://twitter.com/statuses/update.xml'
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
consumer, token=token, http_method='POST', http_url=update_url)
# POST signing isn't working. It apparently works to just put the request as part
# of the query string.
oauth_request.set_parameter('status', status)
oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), consumer, token)
# This request actually posts the Tweet.
return urllib2.urlopen(oauth_request.to_url(), '').read()