-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_auth.py
50 lines (44 loc) · 2.03 KB
/
twitter_auth.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
import http.client
from rauth import OAuth1Service
from secret import key, secret
class TwitterObj():
def __init__(self):
self.twitter_auth = OAuth1Service(
name='PythonFeedReader',
consumer_key=key,
consumer_secret=secret,
request_token_url='https://api.twitter.com/oauth/request_token',
access_token_url='https://api.twitter.com/oauth/access_token',
authorize_url='https://api.twitter.com/oauth/authorize',
base_url='https://api.twitter.com/1.1/'
)
def get_auth_url(self):
self.req_token, self.req_token_secret = self.twitter_auth.get_request_token()
return self.twitter_auth.get_authorize_url(self.req_token)
def get_tweets(self,pin):
session = self.twitter_auth.get_auth_session(self.req_token, self.req_token_secret,
method='POST', data={ 'oauth_verifier':pin })
rawTweets = session.get('statuses/home_timeline.json',
params={'count':100}).json()
tweets = list()
profilePictures = dict()
for rawTweet in rawTweets:
tweet = dict()
tweet["name"] = rawTweet["user"]["name"]
tweet["text"] = rawTweet["text"]
if tweet["name"] in profilePictures:
tweet["imgFilePath"] = profilePictures["name"]
else:
profilePictureUrl = rawTweet["user"]["profile_image_url"]
domain = profilePictureUrl.split("/", 3)[2]
route = "/" + profilePictureUrl.split("/", 3)[3]
conn = http.client.HTTPConnection(domain)
conn.request("GET", route)
rawImg = conn.getresponse()
imgFilePath = "images/" + tweet["name"] + "_profile.jpg"
profilePictures["name"] = imgFilePath
with open(imgFilePath, "wb") as imgFile:
imgFile.write(rawImg.read())
tweet["imgFilePath"] = imgFilePath
tweets.append(tweet)
return tweets