Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby script to set informations of a twitter account.
#! /usr/local/bin/ruby
# coding: utf-8
#
# ========================
# Twitter settings
# ========================
#
require 'twitter'
require 'oauth'
class TwitterSettings
# Constants
CONS_KEY = "<your_consumer_key>"
CONS_SEC = "<your_consumer_secret>"
ACCS_KEY = "<your_access_token>"
ACCS_SEC = "<your_access_token_secret>"
T_ZONE = "Tokyo"
NAME = "テストアカウント"
URL = "http://xxx.yyy.zzz/"
LOCATION = "Ruby City MATSUE"
DESC = "これはテストアカウントです。"
LN_COLOR = "FA743E"
PR_BGIMG = "/path/to/background.gif"
PR_BANNER = "/path/to/banner.jpg"
PR_IMG = "/path/to/profile.png"
def initialize
@client = Twitter::REST::Client.new do |config|
config.consumer_key = CONS_KEY
config.consumer_secret = CONS_SEC
config.access_token = ACCS_KEY
config.access_token_secret = ACCS_SEC
end
end
# Main process
def exec
# account/settings
settings
# account/update_profile
update_profile
# account/update_profile_background_image
update_profile_background_image
# account/update_profile_banner
update_profile_banner
# account/update_profile_image
update_profile_image
rescue => e
$stderr.puts "[EXCEPTION][#{self.class.name}.#{__method__}] #{e}"
exit 1
end
private
# account/settings
# - time_zone: The timezone dates and times should be displayed in for the user.
# The timezone must be one of the Rails TimeZone names.
def settings
puts "* account/settings"
begin
@client.settings({time_zone: T_ZONE})
rescue => e
raise
end
end
# account/update_profile
# - name : Maximum of 20 characters
# - url : Maximum of 100 characters
# - location : Maximum of 30 characters
# - description : Maximum of 160 characters
# - profile_link_color: (ex: F00 or FA743E)
def update_profile
puts "* account/update_profile"
begin
@client.update_profile(
{
name: NAME,
url: URL,
location: LOCATION,
description: DESC,
profile_link_color: LN_COLOR
}
)
rescue => e
raise
end
end
# account/update_profile_background_image
# - The background image for the profile, base64-encoded. Must be a valid GIF,
# JPG, or PNG image of less than 800 kilobytes in size. Images with width
# larger than 2048 pixels will be forcibly scaled down. The image must be
# provided as raw multipart data, not a URL.
def update_profile_background_image
puts "* account/update_profile_background_image"
begin
@client.update_profile_background_image(
File.open(PR_BGIMG), {tile: true, use: true}
)
rescue => e
raise
end
end
# account/update_profile_banner
# - banner: The Base64-encoded or raw image data being uploaded as the user’s
# new profile banner.
def update_profile_banner
puts "* account/update_profile_banner"
begin
@client.update_profile_banner(File.open(PR_BANNER))
rescue => e
raise
end
end
# account/update_profile_image
# - image: 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.
def update_profile_image
puts "* account/update_profile_image"
begin
@client.update_profile_image(File.open(PR_IMG))
rescue => e
raise
end
end
end
TwitterSettings.new.exec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment