LoginSignup
0
0

More than 1 year has passed since last update.

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

Last updated at Posted at 2019-04-15

Demo

  • Hit the API to switch profile images at 1 minute intervals
  • 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)

image image image image image

Twitter response

Somewhat popular

image

image

image

image

Deploy

It depends on the idea.

Image preparation

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

image image

image

https://twitter.com/yumainaura

shell

Pick an image at random and give it to 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

Hit the API to update profile image

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

Register on server etc anything you like

Original by Github issue

チャットメンバー募集

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

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0