I want to do something like a beauty watch by switching profile images every minute on #Twitter (Try it)

<h1> Demo </h1>

<ul>
<li> Hit the API to switch profile images at 1 minute intervals </li>
<li> All you have to do is leave it to the client of the iphone application and the web (it looks like other users have switched to real time) </li>
</ul>

<p><img src="https://user-images.githubusercontent.com/13635059/56091607-0cb51e80-5eec-11e9-8c4a-82f902e8a907.png" alt="image">
<img src="https://user-images.githubusercontent.com/13635059/56091609-1179d280-5eec-11e9-874f-1436694c21f6.png" alt="image">
<img src="https://user-images.githubusercontent.com/13635059/56091608-0fb00f00-5eec-11e9-92fb-2dd8429b98eb.png" alt="image">
<img src="https://user-images.githubusercontent.com/13635059/56091683-e2179580-5eec-11e9-83bb-78477117c372.png" alt="image">
<img src="https://user-images.githubusercontent.com/13635059/56091687-e93ea380-5eec-11e9-93e4-5bae838056a9.png" alt="image"></p>

<h1> Twitter response </h1>

<p> Somewhat popular </p>

<h2><img src="https://user-images.githubusercontent.com/13635059/56091661-9369fb80-5eec-11e9-9706-739168eda9b0.png" alt="image"></h2>

<h2><img src="https://user-images.githubusercontent.com/13635059/56091662-949b2880-5eec-11e9-8dd0-fb83d719febd.png" alt="image"></h2>

<h2><img src="https://user-images.githubusercontent.com/13635059/56091666-9c5acd00-5eec-11e9-88f4-89dcae18c8df.png" alt="image"></h2>

<p><img src="https://user-images.githubusercontent.com/13635059/56091650-71707900-5eec-11e9-9424-d76f642b4232.png" alt="image"></p>

<h1> Deploy </h1>

<p> It depends on the idea. </p>

<h1> Image preparation </h1>

<p> Prepare a few images in the image folder (Be careful with API size restrictions, etc.) </p>

<p><img src="https://user-images.githubusercontent.com/13635059/56091611-163e8680-5eec-11e9-871f-94f9f015108d.png" alt="image">
<img src="https://user-images.githubusercontent.com/13635059/56091612-19d20d80-5eec-11e9-82a4-bfb9e1ec93a9.png" alt="image"></p>

<p><img src="https://user-images.githubusercontent.com/13635059/56091677-ca401180-5eec-11e9-8a65-a18a96794ccb.png" alt="image"></p>

<p> https://twitter.com/yumainaura </p>

<h1> shell </h1>

<p> Pick an image at random and give it to python </p>

<pre> <code class="sh">#!/usr/bin/env bash 
 
 set -eu 
 
 base_dir=$(dirname &quot;$0&quot;) 
 
 source &quot;$base_dir&quot;/../twitter-setting.sh 
 source &quot;$base_dir&quot;/../../setting.sh 
 
 &quot;$api_dir&quot;/twitter/update-profile.py \ 
 $(ls -1 &quot;$base_dir&quot;/image-sepia/* | shuf -n 1) 
 
 
</code> </pre>

<h1> python </h1>

<p> Hit the API to update profile image </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> twtterauth.py </h1>

<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> config.py </h1>

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

<p> # cron </p>

<p> Register on server etc 
 anything you like </p>


# Original by Github issue

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








<!-- Update From Qiita API -->

# チャットメンバー募集


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

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





# Twitter


https://twitter.com/YumaInaura


<!-- Update From Qiita API -->