Sphereとともに

scalaとかplayframeworkとか。技術ブログにしたいなと。環境は Windows7x64, mac です。たまに声優さん情報が混ざります。最近ちょっとClojure触りました。

python でまずはtwitterにアクセスしてみる

ほぼ公式みればわかること

使用するライブラリ

pip install pyyaml
pip install tweepy

config.yaml

config.yamlにConsumer KeyとかAccessTokenとか用意する

動かすコード

# -*- coding: utf-8 -*-

import tweepy
import yaml

def readConfig():
    """Read config file"""
    with open('config.yaml') as reader:
        y = reader.read()
        r = yaml.load(y)
        return r

def initializeClient(consumerKey, consumerSecret, accessToken, accessTokenSecret):
    auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
    auth.set_access_token(accessToken, accessTokenSecret)
    return tweepy.API(auth)

def main():
    config = readConfig()
    print(config)
    client = initializeClient(config["consumer_key"], config["consumer_secret"], config["access_token"], config["access_token_secret"])
    print(client.me())


if __name__ == '__main__':
    main()

入門 Python 3

入門 Python 3