Skip to content

Instantly share code, notes, and snippets.

@showyou
Created March 14, 2010 20:48
Show Gist options
  • Save showyou/332227 to your computer and use it in GitHub Desktop.
Save showyou/332227 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import simplejson
import tweepy
import sys
def load_json_file(filename):
try:
f = open(filename)
except IOError:
print(u'Error: ファイルが見つかりません:'+filename)
return None
data = simplejson.loads(f.read())
f.close()
return data
def save_json_file(filename, data):
try:
f = open(filename,'w')
except IOError:
print(u'Error: ファイルが開けません:'+filename)
return
simplejson.dump( data, f )
f.close()
def init_config_using_xauth(consumer_token, consumer_secret):
auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
username = raw_input('username:').strip()
password = raw_input('password:').strip()
try:
auth.get_xauth_access_token(username, password )
except tweepy.error.TweepError:
print(u'Error: 認証に失敗しました')
sys.exit(-1)
access_token = auth.access_token
user = {}
user['key'] = key = access_token.key
user['secret'] = secret = access_token.secret
user['credential'] = dict(user = tweepy.API(auth).me().screen_name)
save_json_file('user.json', user)
return user
def connect(consumer_token, consumer_secret):
user = load_json_file('user.json')
if user == None:
user = init_config_using_xauth(consumer_token, consumer_secret)
auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
auth.set_access_token(user['key'], user['secret'])
api = tweepy.API(auth)
print(u'tweepy で接続しました')
return api
conf = load_json_file('consumer.json')
if conf == None:
print(u'Error: consumer設定が空なので終了します')
sys.exit(-1)
api = connect(conf['consumer_token'], conf['consumer_secret'])
#api.update_status(status=u'おはようなのよ')
print api.rate_limit_status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment