🖥

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

2019/04/15に公開

Demo

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

image
image
image
image
image

Twitterの反応

わりと好評

image

image

image

image

展開

アイディア次第。

画像の準備

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

image
image

image

https://twitter.com/yumainaura

shell

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

#!/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 を叩いてプロフィール画像を更新する

#!/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

#!/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

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


cron

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

Original by Github issue

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

チャットメンバー募集

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

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

Twitter

https://twitter.com/YumaInaura

公開日時

2019-04-15

Discussion