Created
November 23, 2012 14:23
-
-
Save mizzy/4135847 to your computer and use it in GitHub Desktop.
www.shikakun.com で あんちぽさんの過去tweetをランダムな相手にランダムにつぶやく
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
require 'mechanize' | |
require 'twitter' | |
Twitter.configure do |config| | |
config.consumer_key = ENV['TWITTER_CONSUMER_KEY'] | |
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET'] | |
config.oauth_token = ENV['TWITTER_OAUTH_TOKEN'] | |
config.oauth_token_secret = ENV['TWITTER_OAUTH_SECRET'] | |
end | |
tweets = [] | |
max_id = 999999999999999999 | |
prev_max_id = 0 | |
(1 .. 20).each do |page| | |
Twitter.user_timeline("kentaro", { max_id: max_id, count: 200 }).each do |tweet| | |
tweets << tweet.text | |
max_id = tweet.id | |
end | |
break if max_id == prev_max_id | |
prev_max_id = max_id | |
max_id -= 1 | |
end | |
agent = Mechanize.new | |
shikakun = agent.get('http://www.shikakun.com') | |
shikakun = agent.get('http://shikakun-mizzy.sqale.jp') | |
shikakun.links.each do |link| | |
if link.href == '/auth/twitter' | |
form = link.click.form | |
form.field_with(name: 'session[username_or_email]').value = ENV['TWITTER_USERNAME'] | |
form.field_with(name: 'session[password]').value = ENV['TWITTER_PASSWORD'] | |
agent.submit(form).links.each do |link| | |
if link.text == 'click here to continue' | |
count = 0 | |
while 1 | |
begin | |
shikakun = link.click | |
rescue | |
puts "Retrying ..." | |
count += 1 | |
break if count > 3 | |
sleep 1 | |
else | |
break | |
end | |
end | |
end | |
end | |
break | |
end | |
end | |
form = shikakun.form | |
form.field_with(name: 'message').value = tweets[rand(tweets.length)].gsub(/@[a-zA-Z0-9]+/, '') | |
select_list = form.field_with(name: 'to') | |
options = select_list.options | |
options[rand(options.length)].select | |
form.submit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment