I want to switch profile images in night mode on Twitter at night. (#Python script example of updating profile image with #Twitter # API)

<h1> config.py </h1>

<pre> <code>CONSUMER_KEY = &#39;&#39; 
 CONSUMER_SECRET = &#39;&#39; 
 ACCESS_TOKEN = &#39;&#39; 
 ACCESS_TOKEN_SECRET = &#39;&#39; 
 
</code> </pre>

<p> # twitterauth.py </p>

<pre> <code class="py">#!/usr/bin/env python3 
 
 import os, config 
 from requests_oauthlib import OAuth1Session 
 
 if os.environ.get(&#39;TWITTER_CONSUMER_KEY&#39;): 
 CONSUMER_KEY = os.environ.get(&#39;TWITTER_CONSUMER_KEY&#39;) 
 CONSUMER_SECRET = os.environ.get(&#39;TWITTER_CONSUMER_SECRET&#39;) 
 ACCESS_TOKEN = os.environ.get(&#39;TWITTER_ACCESS_TOKEN&#39;) 
 ACCESS_TOKEN_SECRET = os.environ.get(&#39;TWITTER_ACCESS_TOKEN_SECRET&#39;) 
 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) 
 
 
 
</code> </pre>

<h1> Script </h1>

<p> Specify the file path of the image and base64 encode and pass it as a string to the API. </p>

<pre> <code class="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, &quot;rb&quot;) as image_file: 
 image_encoded_string = base64.b64encode(image_file.read()) 
 
 api_url = &#39;https://api.twitter.com/1.1/account/update_profile_image.json&#39; 
 
 params = { 
 &quot;image&quot;: image_encoded_string 
 } 
 
 response = twitter.post(api_url, params=params) 
 
 print(json.dumps(response.json())) 
 
 
</code> </pre>

<h1> EXE </h1>

<pre> <code>$ ./update-profile.py image/profile.jpg 
 {&quot;id&quot;: 473780756, &quot;id_str&quot;: &quot;473780756&quot;, &quot;name&quot;: &quot;Rails\u6b744\u5e74\u30a8\u30f3\u30b8\u30cb\u30a2\u3092\u304a\u63a2\u3057\u306e\u65b9\u306f\u3053\u3061\u3089\u307e\u3067@\u7a32\u6d66\u60a0\u99ac&quot;, &quot;screen_name&quot;: &quot;YumaInaura&quot;, &quot;location&quot;: null, &quot;description&quot;: &quot;\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&quot;, &quot;url&quot;: null, &quot;entities&quot;: {&quot;description&quot;: {&quot;urls&quot;: []}}, &quot;protected&quot;: false, &quot;followers_count&quot;: 593, &quot;friends_count&quot;: 0, &quot;listed_count&quot;: 30, &quot;created_at&quot;: &quot;Wed Jan 25 09:59:57 +0000 2012&quot;, &quot;favourites_count&quot;: 27660, &quot;utc_offset&quot;: null, &quot;time_zone&quot;: null, &quot;geo_enabled&quot;: true, &quot;verified&quot;: false, &quot;statuses_count&quot;: 41139, &quot;lang&quot;: &quot;en&quot;, &quot;contributors_enabled&quot;: false, &quot;is_translator&quot;: false, &quot;is_translation_enabled&quot;: false, &quot;profile_background_color&quot;: &quot;000000&quot;, &quot;profile_background_image_url&quot;: &quot;http://abs.twimg.com/images/themes/theme1/bg.png&quot;, &quot;profile_background_image_url_https&quot;: &quot;https://abs.twimg.com/images/themes/theme1/bg.png&quot;, &quot;profile_background_tile&quot;: false, &quot;profile_image_url&quot;: &quot;http://pbs.twimg.com/profile_images/1117233785987158016/1rvLg75w_normal.jpg&quot;, &quot;profile_image_url_https&quot;: &quot;https://pbs.twimg.com/profile_images/1117233785987158016/1rvLg75w_normal.jpg&quot;, &quot;profile_banner_url&quot;: &quot;https://pbs.twimg.com/profile_banners/473780756/1552991452&quot;, &quot;profile_link_color&quot;: &quot;1B95E0&quot;, &quot;profile_sidebar_border_color&quot;: &quot;000000&quot;, &quot;profile_sidebar_fill_color&quot;: &quot;000000&quot;, &quot;profile_text_color&quot;: &quot;000000&quot;, &quot;profile_use_background_image&quot;: false, &quot;default_profile&quot;: false, &quot;default_profile_image&quot;: false, &quot;following&quot;: null, &quot;follow_request_sent&quot;: null, &quot;notifications&quot;: null, &quot;translator_type&quot;: &quot;none&quot;} 
</code> </pre>

<p><img src="https://user-images.githubusercontent.com/13635059/56087018-a7d2d780-5e9d-11e9-9888-3459659025ed.png" alt="image"></p>

<pre> <code>$ ./update-profile.py image/profile_sleepy.jpg 
 {&quot;id&quot;: 473780756, &quot;id_str&quot;: &quot;473780756&quot;, &quot;name&quot;: &quot;Rails\u6b744\u5e74\u30a8\u30f3\u30b8\u30cb\u30a2\u3092\u304a\u63a2\u3057\u306e\u65b9\u306f\u3053\u3061\u3089\u307e\u3067@\u7a32\u6d66\u60a0\u99ac&quot;, &quot;screen_name&quot;: &quot;YumaInaura&quot;, &quot;location&quot;: null, &quot;description&quot;: &quot;\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&quot;, &quot;url&quot;: null, &quot;entities&quot;: {&quot;description&quot;: {&quot;urls&quot;: []}}, &quot;protected&quot;: false, &quot;followers_count&quot;: 593, &quot;friends_count&quot;: 0, &quot;listed_count&quot;: 30, &quot;created_at&quot;: &quot;Wed Jan 25 09:59:57 +0000 2012&quot;, &quot;favourites_count&quot;: 27660, &quot;utc_offset&quot;: null, &quot;time_zone&quot;: null, &quot;geo_enabled&quot;: true, &quot;verified&quot;: false, &quot;statuses_count&quot;: 41139, &quot;lang&quot;: &quot;en&quot;, &quot;contributors_enabled&quot;: false, &quot;is_translator&quot;: false, &quot;is_translation_enabled&quot;: false, &quot;profile_background_color&quot;: &quot;000000&quot;, &quot;profile_background_image_url&quot;: &quot;http://abs.twimg.com/images/themes/theme1/bg.png&quot;, &quot;profile_background_image_url_https&quot;: &quot;https://abs.twimg.com/images/themes/theme1/bg.png&quot;, &quot;profile_background_tile&quot;: false, &quot;profile_image_url&quot;: &quot;http://pbs.twimg.com/profile_images/1117233922763411457/9A3lowvf_normal.jpg&quot;, &quot;profile_image_url_https&quot;: &quot;https://pbs.twimg.com/profile_images/1117233922763411457/9A3lowvf_normal.jpg&quot;, &quot;profile_banner_url&quot;: &quot;https://pbs.twimg.com/profile_banners/473780756/1552991452&quot;, &quot;profile_link_color&quot;: &quot;1B95E0&quot;, &quot;profile_sidebar_border_color&quot;: &quot;000000&quot;, &quot;profile_sidebar_fill_color&quot;: &quot;000000&quot;, &quot;profile_text_color&quot;: &quot;000000&quot;, &quot;profile_use_background_image&quot;: false, &quot;default_profile&quot;: false, &quot;default_profile_image&quot;: false, &quot;following&quot;: null, &quot;follow_request_sent&quot;: null, &quot;notifications&quot;: null, &quot;translator_type&quot;: &quot;none&quot;} 
</code> </pre>

<p><img src="https://user-images.githubusercontent.com/13635059/56087024-bae5a780-5e9d-11e9-9a37-6149a8aa6174.png" alt="image"></p>

<h1> sample Images </h1>

<p> Be careful because it is rather strict such as capacity and size restrictions. </p>

<p><img src="https://user-images.githubusercontent.com/13635059/56087008-8a057280-5e9d-11e9-94c0-6dd5c35a2c75.png" alt="image"></p>

<p><img src="https://user-images.githubusercontent.com/13635059/56087028-d781df80-5e9d-11e9-9cf3-0b1488c94d01.png" alt="image"></p>

<h1> Scheduled execution </h1>

<p> Turn the cron on the server and so on. </p>


# Original by Github issue

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








<!-- Update From Qiita API -->

# チャットメンバー募集


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

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





# Twitter


https://twitter.com/YumaInaura


<!-- Update From Qiita API -->