アカウント設定関連¶
本節では PTT による、Twitter アカウント設定関連のインターフェイスの利用方法を記す。
GET account/verify_credentials¶
GET account/verify_credentials は自分の credentials が効力があるのかを試すのに用いる機能らしい。正当な場合は自分を表現するユーザーオブジェクトが得られる。
サンプルコードを示す。
#!/usr/bin/env python
# Demonstration GET account/verify_credentials
# See https://dev.twitter.com/rest/reference/get/account/verify_credentials
from secret import twitter_instance
from json import dump
import sys
tw = twitter_instance()
# [1]
response = tw.account.verify_credentials(
    skip_status=True, email=True)
# [2]
dump(response, sys.stdout, ensure_ascii=False, indent=4, sort_keys=True)
- [1] 適当にオプションを指定して Twitter にリクエストを送信する。 
- [2] 自分を表現するオブジェクトを受信したはずなので、それを画面に出力する。 
次に実行例を示す。
bash$ ./account-verify_credentials.py
{
    "contributors_enabled": false,
    "created_at": "Wed Jan 11 12:01:03 +0000 2012",
    "default_profile": false,
    "default_profile_image": false,
    "description": "実は電子の世界の人で現実には存在しない。",
    "entities": {
        "description": {
            "urls": []
        },
        "url": {
            "urls": [
                {
                    "display_url": "github.com/showa-yojyo",
                    "expanded_url": "https://github.com/showa-yojyo",
                    "indices": [
                        0,
                        23
                    ],
                    "url": "https://t.co/YxrYqoZQ1r"
                }
            ]
        }
    },
    "favourites_count": 701,
    "follow_request_sent": false,
    "followers_count": 24,
    "following": false,
    "friends_count": 0,
    "geo_enabled": false,
    "has_extended_profile": false,
    "id": 461058152,
    "id_str": "461058152",
    "is_translation_enabled": true,
    "is_translator": false,
    "lang": "ja",
    "listed_count": 2,
    "location": "東京都区内",
    "name": "プレハブ小屋",
    "notifications": false,
    "profile_background_color": "FFFFFF",
    "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/678214720462020608/70eusxt6.png",
    "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/678214720462020608/70eusxt6.png",
    "profile_background_tile": true,
    "profile_banner_url": "https://pbs.twimg.com/profile_banners/461058152/1450531542",
    "profile_image_url": "http://pbs.twimg.com/profile_images/678206878820642816/7o08Gvb9_normal.png",
    "profile_image_url_https": "https://pbs.twimg.com/profile_images/678206878820642816/7o08Gvb9_normal.png",
    "profile_link_color": "FF1493",
    "profile_sidebar_border_color": "FFFFFF",
    "profile_sidebar_fill_color": "DDEEF6",
    "profile_text_color": "333333",
    "profile_use_background_image": true,
    "protected": false,
    "screen_name": "showa_yojyo",
    "statuses_count": 7407,
    "time_zone": "Tokyo",
    "url": "https://t.co/YxrYqoZQ1r",
    "utc_offset": 32400,
    "verified": false
}
見慣れたデータが得られた。
GET account/settings¶
GET account/settings は自分のアカウント設定情報を得る機能だ。次にサンプルコードを示す。
#!/usr/bin/env python
# Demonstration GET accout/settings
# https://dev.twitter.com/rest/reference/get/account/settings
from secret import twitter_instance
from json import dump
import sys
tw = twitter_instance()
# [1]
response = tw.account.settings(_method='GET')
# [2]
dump(response, sys.stdout, ensure_ascii=False, indent=4, sort_keys=True)
- [1] 実は account/settings には POST 版もあるため、PTT に間違わせないように - _method=GETと指示する。 Twitter のインターフェイスとしては、このリクエストは引数を取らない。
- [2] 受信データを画面に出力する。 
次に実行例を示す。
bash$ ./account-settings.py
{
    "allow_contributor_request": "all",
    "allow_dm_groups_from": "following",
    "allow_dms_from": "all",
    "always_use_https": true,
    "discoverable_by_email": true,
    "discoverable_by_mobile_phone": false,
    "display_sensitive_media": false,
    "geo_enabled": false,
    "language": "ja",
    "protected": false,
    "screen_name": "showa_yojyo",
    "sleep_time": {
        "enabled": true,
        "end_time": 0,
        "start_time": 3
    },
    "smart_mute": false,
    "time_zone": {
        "name": "Tokyo",
        "tzinfo_name": "Asia/Tokyo",
        "utc_offset": 32400
    },
    "trend_location": [
        {
            "country": "Japan",
            "countryCode": "JP",
            "name": "東京",
            "parentid": 23424856,
            "placeType": {
                "code": 7,
                "name": "Town"
            },
            "url": "http://where.yahooapis.com/v1/place/1118370",
            "woeid": 1118370
        }
    ],
    "use_cookie_personalization": true
}
見慣れたプロパティーもあれば、そうでないのもある。
POST account/settings¶
POST account/settings は自分のアカウント設定情報を更新する機能だ。
#!/usr/bin/env python
# Demonstration POST accout/settings
# https://dev.twitter.com/rest/reference/post/account/settings
from secret import twitter_instance
from json import dump
import sys
tw = twitter_instance()
# [1]
response = tw.account.settings(
    _method='POST',
    sleep_time_enabled=True,
    start_sleep_time='03', end_sleep_time='24',
    time_zone='Tokyo',
    lang='ja')
# [2]
dump(response, sys.stdout, ensure_ascii=False, indent=4, sort_keys=True)
- [1] 先ほど述べた理由により - _method=POSTも併せて指示する。- スリープタイム系のパラメーターのタイムゾーンは、現時点での設定値が基準になる?それとも同時に設定しているタイムゾーンの値になる? 
 
- [2] 処理結果を画面に出力する。 
次に実行例を示す。
bash$ ./account-settings-p.py
{
    "allow_contributor_request": "all",
    "allow_dm_groups_from": "following",
    "allow_dms_from": "all",
    "always_use_https": true,
    "discoverable_by_email": true,
    "discoverable_by_mobile_phone": false,
    "display_sensitive_media": false,
    "geo_enabled": false,
    "language": "ja",
    "protected": false,
    "screen_name": "showa_yojyo",
    "sleep_time": {
        "enabled": true,
        "end_time": 0,
        "start_time": 3
    },
    "smart_mute": false,
    "time_zone": {
        "name": "Tokyo",
        "tzinfo_name": "Asia/Tokyo",
        "utc_offset": 32400
    },
    "trend_location": [
        {
            "country": "Japan",
            "countryCode": "JP",
            "name": "東京",
            "parentid": 23424856,
            "placeType": {
                "code": 7,
                "name": "Town"
            },
            "url": "http://where.yahooapis.com/v1/place/1118370",
            "woeid": 1118370
        }
    ],
    "use_cookie_personalization": true
}
問題なさそうだ。
POST account/update_delivery_device¶
これは動かない。
POST account/update_profile¶
POST account/update_profile は自分のプロフィール部分の更新をする機能だ。次にサンプルコードを示す。
#!/usr/bin/env python
# Demonstration POST account/update_profile
# See https://dev.twitter.com/rest/reference/post/account/update_profile
from secret import twitter_instance
from json import dump
import sys
tw = twitter_instance()
# [1]
response = tw.account.update_profile(
    name='プレハブ小屋',
    url='https://github.com/showa-yojyo',
    location='東京都区内',
    description='実は電子の世界の人で現実には存在しない。',
    profile_link_color='FF1493',
    include_entities=False,
    skip_status=True)
# [2]
dump(response, sys.stdout, ensure_ascii=False, indent=4, sort_keys=True)
- [1] 指定可能なすべてのパラメーターを全部使う。 
- [2] 処理結果を画面に出力する。 
次に実行例を示す。
bash$ ./account-update_profile.py
{
    "contributors_enabled": false,
    "created_at": "Wed Jan 11 12:01:03 +0000 2012",
    "default_profile": false,
    "default_profile_image": false,
    "description": "実は電子の世界の人で現実には存在しない。",
    "entities": {
        "description": {
            "urls": []
        }
        "url": {
            "urls": [
                {
                    "display_url": "github.com/showa-yojyo",
                    "expanded_url": "https://github.com/showa-yojyo",
                    "indices": [
                        0,
                        23
                    ],
                    "url": "https://t.co/YxrYqoZQ1r"
                }
            ]
        },
    },
    "favourites_count": 700,
    "follow_request_sent": false,
    "followers_count": 24,
    "following": false,
    "friends_count": 0,
    "geo_enabled": false,
    "has_extended_profile": false,
    "id": 461058152,
    "id_str": "461058152",
    "is_translation_enabled": true,
    "is_translator": false,
    "lang": "ja",
    "listed_count": 2,
    "location": "東京都区内",
    "name": "プレハブ小屋",
    "notifications": false,
    "profile_background_color": "FFFFFF",
    "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/557620683388108800/RoH3aAq8.png",
    "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/557620683388108800/RoH3aAq8.png",
    "profile_background_tile": true,
    "profile_image_url": "http://pbs.twimg.com/profile_images/518444238069968896/9swnzcfK_normal.png",
    "profile_image_url_https": "https://pbs.twimg.com/profile_images/518444238069968896/9swnzcfK_normal.png"
    "profile_link_color": "FF1493",
    "profile_location": null,
    "profile_sidebar_border_color": "FFFFFF",
    "profile_sidebar_fill_color": "DDEEF6",
    "profile_text_color": "333333",
    "profile_use_background_image": true,
    "protected": false,
    "screen_name": "showa_yojyo",
    "statuses_count": 7382,
    "time_zone": "Tokyo",
    "url": "https://t.co/YxrYqoZQ1r",
    "utc_offset": 32400,
    "verified": false,
}
どうも include_entities が効いていない?
POST account/update_profile_image¶
POST account/update_profile_image はたぶんユーザーのアイコンの画像の更新をする機能だ。やり方は上述のものと同様になる。
サンプルコードを次に示す。
#!/usr/bin/env python
# Demonstration POST account/update_profile_image
# See https://dev.twitter.com/rest/reference/post/account/update_profile_image
from secret import twitter_instance
from json import dump
import sys
tw = twitter_instance()
# [1]
imagepath = "profile-image.png"
with open(imagepath, mode='rb') as fp:
    filedata = fp.read()
# [2]
response = tw.account.update_profile_image(image=filedata)
# [3]
dump(response, sys.stdout, ensure_ascii=False, indent=4, sort_keys=True)
- [1] あらかじめ画像を用意しておく。 
- [2] アイコン画像ファイルを開いて、ファイルオブジェクトを得ておき、これをメソッドにキーワード引数 - imageとして渡す。
- [3] 処理結果を画面に出力する。 
次に実行例を示す。出力はいつもの自分のユーザーオブジェクトなので興味のある所だけを示す。
bash$ ./account-update_profile_image.py
{
    "default_profile_image": false,
    ...
    "profile_image_url": "http://pbs.twimg.com/profile_images/518444238069968896/9swnzcfK_normal.png",
    "profile_image_url_https": "https://pbs.twimg.com/profile_images/518444238069968896/9swnzcfK_normal.png",
    ...
}
Twitter クライアント等を用いて画像が本当に更新されたかどうかを確認するためには、このアップロード処理終了後に数秒間をおくとよい。
POST account/update_profile_background_image¶
POST account/update_profile_background_image は自分の Twitter のページの背景画像を更新する機能だ。背景画像はあまりお目にかからない気がするが、例えばリスト画面で描画される画像だ。
画像を指定する方法はこれだけは二つある。
- 画像データを base64 エンコードしたバイナリーデータをアップロードする 
- POST media/upload 済みの画像を ID で指定する 
ここでは後者の方法を記す。サンプルコードは次のようになる。
#!/usr/bin/env python
# Demonstration POST account/update_profile_background_image
# See https://dev.twitter.com/rest/reference/post/account/update_profile_background_image
from secret import twitter_instance
from json import dump
import sys
tw = twitter_instance()
# [1]
imagepath = 'profile-background.png'
with open(imagepath, mode='rb') as fp:
    filedata = fp.read()
# [2]
response = tw.account.update_profile_background_image(
    image=filedata, tile=True)
# [3]
dump(response, sys.stdout, ensure_ascii=False, indent=4, sort_keys=True)
- [1] あらかじめ画像を用意しておく。画像ファイルを開いて、ファイルオブジェクトを経て生バイナリーを抱えておく。 
- [2] これをメソッドにキーワード引数 - imageとして渡す。また、画像を反復させて描画したいので- tile=Trueも指定する。
- [3] 処理結果を画面に出力する。 
次に実行例を示す。出力はいつもの自分のユーザーオブジェクトなので興味のある所だけを示す。
bash$ ./account-update_profile_background_image.py
{
    ...
    "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/557620683388108800/RoH3aAq8.png",
    "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/557620683388108800/RoH3aAq8.png",
    "profile_background_tile": true,
    "profile_use_background_image": true
}