Skip to content

Instantly share code, notes, and snippets.

@clicube
Created July 29, 2012 15:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save clicube/3199457 to your computer and use it in GitHub Desktop.
Save clicube/3199457 to your computer and use it in GitHub Desktop.
change icon
# original Ruby Twitter Gem : example/helper/config_store.rb
# modified at 2009/08/13
require 'yaml'
class ConfigStore
attr_reader :file
def initialize(file)
@file = file
load
end
def load
if(@config == nil)
if(File::exist?(file))
@config ||= YAML::load(open(file))
else
@config = {}
end
end
self
end
def [](key)
load
@config[key]
end
def []=(key, value)
load
@config[key] = value
end
def delete(*keys)
keys.each { |key| @config.delete(key) }
save
self
end
def update(c={})
@config.merge!(c)
save
self
end
def save
File.open(file, 'w') { |f| f.write(YAML.dump(@config)) }
self
end
end
#coding: utf-8
#$KCODE = 'u'
Dir.chdir(File.dirname(__FILE__))
require 'kconv'
require 'rubygems'
require 'twitter'
require 'cairo'
require_relative 'config_store'
# constants
Original_img = 'Charlotte_204.png'
#Original_beem_img = 'Charlotte_beem_204.png'
New_img = 'Charlotte_204_comment.png'
$Config = ConfigStore.new("config.yaml")
def main
#oauth = Twitter::OAuth.new($Config['ctoken'], $Config['csecret'])
#oauth.authorize_from_access($Config['atoken'],$Config['asecret'])
#twitter = Twitter::Base.new(oauth)
twitter = Twitter.new(
:consumer_key => $Config['ctoken'],
:consumer_secret => $Config['csecret'],
:oauth_token => $Config['atoken'],
:oauth_token_secret => $Config['asecret']
)
# check command
last_id = $Config['Last_id'] || 0
nlast_id = last_id
owner = twitter.verify_credentials.screen_name
setter = nil
comment = nil
twitter.mentions.each do |m|
if(m.id <= last_id)
break
else
nlast_id = m.id if(nlast_id < m.id)
end
puts m.text
if(m.text =~ /^@#{owner}[  ]+(icon|(アイコン|あいこん)(変えて|かえて)?)[^  ]*[  ]+(.+)/)
setter = m.user.screen_name
allowlist = getwhitelist(twitter,owner,"white")
if(allowlist.index(setter))# || true)
comment = $4.toutf8
#break;
else
twitter.update("@#{m.user.screen_name} ( ╹◡╹)< おことわりしますっ",:in_reply_to_status_id=>m.id)
end
end
end
$Config['Last_id'] = nlast_id
exit if !comment
comment = $1 if(comment =~ /"(.*)"/)
comment = $1 if(comment =~ /'(.*)'/)
# generate image
#if comment.include?("ビーム")
# img_filename = Original_beem_img
#else
img_filename = Original_img
#end
surface = Cairo::ImageSurface.from_png(img_filename)
context = Cairo::Context.new(surface)
#context.set_source_color(Cairo::Color.parse("#22337e"))
context.set_source_color(Cairo::Color.parse("#bc353c")) #charlotte
#context.set_source_color(Cairo::Color.parse("#000000"))
#context.move_to(115,80) #charlotte
context.move_to(125,80)
context.rotate(-0.30)
## set font size
max = 30
width = 65
context.set_font_size(max)
if((w = context.text_extents(comment).width) > width)
size = max * width / w
context.set_font_size(size)
end
context.select_font_face("YukarryAA",0,0)
context.show_text(comment)
surface.write_to_png(New_img)
# update image
File.open(New_img) do |img|
twitter.update_profile_image(img)
end
# post message
r = rand(5)
s_msg = ""
if(r == 0)
s_msg = "(✿╹◡╹)< #{comment}"
else
s_msg = "( ╹◡╹)< #{comment}"
end
#s_msg = "\#{comment}!/"
twitter.update(s_msg)
File.open('cron.log','a') do |file|
file.puts "comment=#{comment},setter=#{setter}"
end
end
def getwhitelist(twitter,owner,slug)
arr = []
cursor = -1
begin
res = twitter.list_members(owner,slug,{:cursor=>cursor})
res.users.each do |item|
arr << item.screen_name
end
cursor = res.next_cursor
end while cursor != 0
return arr
end
# finalize
END{
$Config.save
}
# call main
begin
main
rescue => e
File.open('cron.log','a') do |file|
puts e.message
file.puts e.message
file.puts e.backtrace
end
rescue Timeout::Error => e
File.open('cron.log','a') do |file|
file.puts e.inspect
file.puts e.message
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment