Twitterで夜はナイトモード的にプロフィール画像を切り替えたい。 ( #Twitter #API でプロフィール画像を更新する #python  スクリプトの例 )

# config.py

```
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''

```

 # twitterauth.py

```py
#!/usr/bin/env python3

import os, config
from requests_oauthlib import OAuth1Session

if os.environ.get('TWITTER_CONSUMER_KEY'):
  CONSUMER_KEY = os.environ.get('TWITTER_CONSUMER_KEY')
  CONSUMER_SECRET = os.environ.get('TWITTER_CONSUMER_SECRET')
  ACCESS_TOKEN = os.environ.get('TWITTER_ACCESS_TOKEN')
  ACCESS_TOKEN_SECRET = os.environ.get('TWITTER_ACCESS_TOKEN_SECRET')
else:
  CONSUMER_KEY = config.CONSUMER_KEY
  CONSUMER_SECRET = config.CONSUMER_SECRET
  ACCESS_TOKEN = config.ACCESS_TOKEN
  ACCESS_TOKEN_SECRET = config.ACCESS_TOKEN_SECRET

def twitter():
  return  OAuth1Session(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)



```

# Script

画像のファイルパスを指定して base64 エンコードして API に 文字列 として渡す。

```py
#!/usr/bin/env python3

# https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/post-account-update_profile_image.html

# The avatar image for the profile, base64-encoded. Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size. Images with width larger than 400 pixels will be scaled down. Animated GIFs will be converted to a static GIF of the first frame, removing the animation.

import json, twitterauth, base64, sys

twitter = twitterauth.twitter()

image_path = sys.argv[1]

with open(image_path, "rb") as image_file:
  image_encoded_string = base64.b64encode(image_file.read())

api_url = 'https://api.twitter.com/1.1/account/update_profile_image.json'

params = {
  "image": image_encoded_string
}

response = twitter.post(api_url, params=params)

print(json.dumps(response.json()))


```

# exe

```
$ ./update-profile.py image/profile.jpg
{"id": 473780756, "id_str": "473780756", "name": "Rails\u6b744\u5e74\u30a8\u30f3\u30b8\u30cb\u30a2\u3092\u304a\u63a2\u3057\u306e\u65b9\u306f\u3053\u3061\u3089\u307e\u3067@\u7a32\u6d66\u60a0\u99ac", "screen_name": "YumaInaura", "location": null, "description": "\u6b21\u306e\u304a\u4ed5\u4e8b\u63a2\u3057\u4e2d / Rails\u696d\u52d9\u7d04\u56db\u5e74 / \u73fe\u5728\u56db\u793e\u76f8\u8ac7\u4e2d / \u30ea\u30e2\u30fc\u30c8\u512a\u5148 / \u5927\u962a / Ruby / Perl / Shell / BigQuery / MySQL / Linux / Apache / ansible / Jenkins etc .. / \u8a73\u7d30\u306fWantedly\u3067 / Twitter DM\u53ef", "url": null, "entities": {"description": {"urls": []}}, "protected": false, "followers_count": 593, "friends_count": 0, "listed_count": 30, "created_at": "Wed Jan 25 09:59:57 +0000 2012", "favourites_count": 27660, "utc_offset": null, "time_zone": null, "geo_enabled": true, "verified": false, "statuses_count": 41139, "lang": "en", "contributors_enabled": false, "is_translator": false, "is_translation_enabled": false, "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_tile": false, "profile_image_url": "http://pbs.twimg.com/profile_images/1117233785987158016/1rvLg75w_normal.jpg", "profile_image_url_https": "https://pbs.twimg.com/profile_images/1117233785987158016/1rvLg75w_normal.jpg", "profile_banner_url": "https://pbs.twimg.com/profile_banners/473780756/1552991452", "profile_link_color": "1B95E0", "profile_sidebar_border_color": "000000", "profile_sidebar_fill_color": "000000", "profile_text_color": "000000", "profile_use_background_image": false, "default_profile": false, "default_profile_image": false, "following": null, "follow_request_sent": null, "notifications": null, "translator_type": "none"}
```

![image](https://user-images.githubusercontent.com/13635059/56087018-a7d2d780-5e9d-11e9-9888-3459659025ed.png)

```
$ ./update-profile.py image/profile_sleepy.jpg
{"id": 473780756, "id_str": "473780756", "name": "Rails\u6b744\u5e74\u30a8\u30f3\u30b8\u30cb\u30a2\u3092\u304a\u63a2\u3057\u306e\u65b9\u306f\u3053\u3061\u3089\u307e\u3067@\u7a32\u6d66\u60a0\u99ac", "screen_name": "YumaInaura", "location": null, "description": "\u6b21\u306e\u304a\u4ed5\u4e8b\u63a2\u3057\u4e2d / Rails\u696d\u52d9\u7d04\u56db\u5e74 / \u73fe\u5728\u56db\u793e\u76f8\u8ac7\u4e2d / \u30ea\u30e2\u30fc\u30c8\u512a\u5148 / \u5927\u962a / Ruby / Perl / Shell / BigQuery / MySQL / Linux / Apache / ansible / Jenkins etc .. / \u8a73\u7d30\u306fWantedly\u3067 / Twitter DM\u53ef", "url": null, "entities": {"description": {"urls": []}}, "protected": false, "followers_count": 593, "friends_count": 0, "listed_count": 30, "created_at": "Wed Jan 25 09:59:57 +0000 2012", "favourites_count": 27660, "utc_offset": null, "time_zone": null, "geo_enabled": true, "verified": false, "statuses_count": 41139, "lang": "en", "contributors_enabled": false, "is_translator": false, "is_translation_enabled": false, "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_tile": false, "profile_image_url": "http://pbs.twimg.com/profile_images/1117233922763411457/9A3lowvf_normal.jpg", "profile_image_url_https": "https://pbs.twimg.com/profile_images/1117233922763411457/9A3lowvf_normal.jpg", "profile_banner_url": "https://pbs.twimg.com/profile_banners/473780756/1552991452", "profile_link_color": "1B95E0", "profile_sidebar_border_color": "000000", "profile_sidebar_fill_color": "000000", "profile_text_color": "000000", "profile_use_background_image": false, "default_profile": false, "default_profile_image": false, "following": null, "follow_request_sent": null, "notifications": null, "translator_type": "none"}
```

![image](https://user-images.githubusercontent.com/13635059/56087024-bae5a780-5e9d-11e9-9a37-6149a8aa6174.png)

# sample Images

容量やサイズ制限などわりと厳しいので注意。

![image](https://user-images.githubusercontent.com/13635059/56087008-8a057280-5e9d-11e9-94c0-6dd5c35a2c75.png)


![image](https://user-images.githubusercontent.com/13635059/56087028-d781df80-5e9d-11e9-9cf3-0b1488c94d01.png)

# 定時実行

てきとうにサーバーでcronを回すなどして。


# Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/1251








<!-- Update From Qiita API -->

# チャットメンバー募集


何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ





# Twitter


https://twitter.com/YumaInaura


<!-- Update From Qiita API -->