Last active
March 13, 2017 05:43
-
-
Save ryumei/5250701f188559c1646639ee8e7115b2 to your computer and use it in GitHub Desktop.
This file contains 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
import requests | |
from requests.auth import HTTPBasicAuth | |
import urllib | |
class Twitter(object): | |
def _access_token(self, key, secret): | |
r = requests.post('https://api.twitter.com/oauth2/token', | |
auth=HTTPBasicAuth(key, secret), | |
headers={'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}, | |
data={'grant_type':'client_credentials'}) | |
return r.json()['access_token'] | |
def __init__(self, key, secret): | |
self.access_token = self._access_token(key, secret) | |
def search(self, keyword): | |
parameter = urllib.urlencode({'src': 'typd', 'q': keyword}) | |
r = requests.get('https://api.twitter.com/1.1/search/tweets.json?%s' % parameter, | |
headers={"Authorization": "Bearer %s" % self.access_token}) | |
return r | |
import os | |
t = Twitter(os.environ['TWITTER_CONSUMER_KEY'], os.environ['TWITTER_CONSUMER_SECRET']) | |
r = t.search('engineer') | |
import json | |
print(json.dumps(r.json(), indent=2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment