-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitter.py
82 lines (72 loc) · 2.86 KB
/
twitter.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from requests_oauthlib import OAuth1Session
from urllib.parse import parse_qs, urlencode
import os
import json
class Twitter:
client_key = None
client_secret = None
oauth_token = None
oauth_token_secret = None
def __init__(self):
if 'PASSER_TWITTER_KEY' in os.environ:
self.client_key = os.environ['PASSER_TWITTER_KEY']
if 'PASSER_TWITTER_SECRET' in os.environ:
self.client_secret = os.environ['PASSER_TWITTER_SECRET']
def make_session(self, data={}):
if self.client_key and self.client_secret:
if self.oauth_token:
data['resource_owner_key'] = self.oauth_token
if self.oauth_token_secret:
data['resource_owner_secret'] = self.oauth_token_secret
return OAuth1Session(self.client_key,
client_secret=self.client_secret,
**data)
else:
return None
def twitter_request_helper(self, target, data=None, oauthdata={},
request_type='POST'):
twitbase = 'https://api.twitter.com'
session = self.make_session(data=oauthdata)
if not session:
return {}
url = '%s/%s' % (twitbase, target)
if request_type == 'POST':
result = session.post(url, data=data)
elif request_type == 'GET':
result = session.get(url)
return result.text
def get_request_token(self):
result = parse_qs(self.twitter_request_helper('/oauth/request_token',
{'callback_uri': 'oob'}))
if ('oauth_callback_confirmed' in result and
'true' in result['oauth_callback_confirmed']):
self.oauth_token = result['oauth_token'][0]
self.oauth_token_secret = result['oauth_token_secret'][0]
else:
return False
return True
def get_user_auth_url(self):
if not self.oauth_token:
if not self.get_request_token():
return False
login_url = 'https://api.twitter.com/oauth/authenticate?oauth_token=%s'
login_url = login_url % self.oauth_token
return login_url
def confirm_pin(self, pin):
result = parse_qs(self.twitter_request_helper('/oauth/access_token',
oauthdata={'verifier': pin}))
self.oauth_token = result['oauth_token'][0]
self.oauth_token_secret = result['oauth_token_secret'][0]
def verify_credentials(self):
return self.twitter_request_helper(
'/1.1/account/verify_credentials.json',
request_type='GET')
def post_tweet(self, content):
tweet = {
'status': content
}
tweet = urlencode(tweet)
return self.twitter_request_helper(
'/1.1/statuses/update.json',
data=tweet
)