#Twitter でプロフィール画像を1分毎に切り替えて美女時計みたいなことがやりたい  (お試し)

# Demo

- APIを叩いてプロフィール画像を1分間隔で切り替える
- あとは iphone  アプリやWebのクライアントに任せるだけ (他のユーザーからはリアルタイムに切り替わったように見えたり)

![image](https://user-images.githubusercontent.com/13635059/56091607-0cb51e80-5eec-11e9-8c4a-82f902e8a907.png)
![image](https://user-images.githubusercontent.com/13635059/56091609-1179d280-5eec-11e9-874f-1436694c21f6.png)
![image](https://user-images.githubusercontent.com/13635059/56091608-0fb00f00-5eec-11e9-92fb-2dd8429b98eb.png)
![image](https://user-images.githubusercontent.com/13635059/56091683-e2179580-5eec-11e9-83bb-78477117c372.png)
![image](https://user-images.githubusercontent.com/13635059/56091687-e93ea380-5eec-11e9-93e4-5bae838056a9.png)

# Twitterの反応

わりと好評

![image](https://user-images.githubusercontent.com/13635059/56091661-9369fb80-5eec-11e9-9706-739168eda9b0.png)
---
![image](https://user-images.githubusercontent.com/13635059/56091662-949b2880-5eec-11e9-8dd0-fb83d719febd.png)
---
![image](https://user-images.githubusercontent.com/13635059/56091666-9c5acd00-5eec-11e9-88f4-89dcae18c8df.png)
---
![image](https://user-images.githubusercontent.com/13635059/56091650-71707900-5eec-11e9-9424-d76f642b4232.png)

# 展開

アイディア次第。

# 画像の準備


画像フォルダに数個の画像を用意しておく (APIのサイズ制限などに注意)

![image](https://user-images.githubusercontent.com/13635059/56091611-163e8680-5eec-11e9-871f-94f9f015108d.png)
![image](https://user-images.githubusercontent.com/13635059/56091612-19d20d80-5eec-11e9-82a4-bfb9e1ec93a9.png)

![image](https://user-images.githubusercontent.com/13635059/56091677-ca401180-5eec-11e9-8a65-a18a96794ccb.png)

https://twitter.com/yumainaura

# shell

ランダムに画像を選んでpythonにわたす

```sh
#!/usr/bin/env bash

set -eu

base_dir=$(dirname "$0")

source "$base_dir"/../twitter-setting.sh
source "$base_dir"/../../setting.sh

"$api_dir"/twitter/update-profile.py \
  $(ls -1 "$base_dir"/image-sepia/* | shuf -n 1)


```

# python 

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()))


```

# twtterauth.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)



```

# config.py

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


```

 # cron

サーバーなどに登録
anything you like

# Original by Github issue

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








<!-- Update From Qiita API -->

# チャットメンバー募集


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

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





# Twitter


https://twitter.com/YumaInaura


<!-- Update From Qiita API -->