Last active
December 17, 2015 08:49
-
-
Save pnlybubbles/5582541 to your computer and use it in GitHub Desktop.
"YURUYURI♪1st.Series Best Album ゆるゆりずむ♪ [7777セット完全生産限定盤] [Limited Edition]" のAmazonからの販売再開をTwitterを利用して監視し、再入荷情報を取得時に瞬間で予約注文を行うスクリプト。watir-webdriver, oauth gem必要。FireFox必要。@amacheckをフォローする必要あり。Amazon 1-Click注文の設定必要。
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
# encoding: utf-8 | |
require 'watir-webdriver' | |
require 'net/https' | |
require 'oauth' | |
require 'cgi' | |
require 'json' | |
require 'openssl' | |
CONSUMER_KEY = "*" | |
CONSUMER_SECRET = "*" | |
ACCESS_TOKEN = "*" | |
ACCESS_TOKEN_SECRET = "*" | |
class TwitterAPI | |
def initialize | |
@consumer = OAuth::Consumer.new( | |
CONSUMER_KEY, | |
CONSUMER_SECRET, | |
:site => 'http://api.twitter.com/1.1' | |
) | |
@access_token = OAuth::AccessToken.new( | |
@consumer, | |
ACCESS_TOKEN, | |
ACCESS_TOKEN_SECRET | |
) | |
end | |
def connect(&block) | |
uri = URI.parse("https://userstream.twitter.com/1.1/user.json") | |
https = Net::HTTP.new(uri.host, uri.port) | |
https.use_ssl = true | |
# https.ca_file = CERTIFICATE_PATH | |
# https.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
https.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
# https.verify_depth = 5 | |
https.start do |https| | |
request = Net::HTTP::Get.new(uri.request_uri) | |
request.oauth!(https, @consumer, @access_token) | |
buf = "" | |
https.request(request) do |response| | |
response.read_body do |chunk| | |
buf << chunk | |
while(line = buf[/.*(\r\n)+/m]) | |
begin | |
buf.sub!(line,"") | |
line.strip! | |
status = JSON.parse(line) | |
rescue | |
break | |
end | |
block.call(status) | |
end | |
end | |
end | |
end | |
end | |
def post(text, *id) | |
if(id.empty?) | |
@access_token.post('/statuses/update.json', | |
'status' => text) | |
else | |
@access_token.post('/statuses/update.json', | |
'status' => text, | |
'in_reply_to_status_id' => id[0]) | |
end | |
end | |
end | |
AMAZON_EMAIL = "****" | |
AMAZON_PASS = "****" | |
class Amazon | |
def initialize | |
@browser = Watir::Browser.new | |
@browser.goto("https://www.amazon.co.jp/ap/signin?_encoding=UTF8&openid.assoc_handle=jpflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.co.jp%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dgno_custrec_signin") | |
@browser.text_field(:id => "ap_email").set(AMAZON_EMAIL) | |
@browser.input(:id => "ap_signin_existing_radio").click | |
@browser.text_field(:id => "ap_password").set(AMAZON_PASS) | |
@browser.input(:id => "signInSubmit-input").click | |
@browser.goto("https://www.amazon.co.jp/gp/css/account/address/view.html?ie=UTF8&*Version*=1&*entries*=0&") | |
@browser.input(:alt => "1-Click を ON にする").click | |
@browser.goto("http://www.amazon.co.jp/ref=gno_logo") | |
end | |
def run(url) | |
@browser.goto(url) | |
@browser.input(:name => "submit.one-click-dropdown-purchase").click | |
# @browser.input(:name => "submit.addToCart").click | |
end | |
end | |
MY_SCREEN_NAME = "pn1y" | |
amazon = Amazon.new | |
twitter_api = TwitterAPI.new | |
loop { | |
puts "connecting..." | |
begin | |
twitter_api.connect { |res| | |
if res['text'] | |
if res['user']['screen_name'] == "amacheck" && res['text'] =~ /ゆるゆり/ | |
puts "#{res['created_at']} #{res['user']['screen_name']}: #{res['text']}" | |
puts "url: #{res['entities']['urls'][0]['expanded_url']}" | |
begin | |
amazon.run(res['entities']['urls'][0]['expanded_url']) | |
puts "SUCCEEDED!!!!!!" | |
twitter_api.post("@#{MY_SCREEN_NAME} アマゾン自動購入を実行しました。注文履歴を確認して下さい。https://www.amazon.co.jp/gp/css/order-history/ref=gno_yam_yrdrs") | |
rescue Exception => e | |
twitter_api.post("@#{MY_SCREEN_NAME} #{e}") | |
puts "ERROR SCRAPING: #{e}" | |
end | |
end | |
if res['user']['screen_name'] == MY_SCREEN_NAME && res['text'] =~ /@#{MY_SCREEN_NAME}\s+amazon.*stop/ | |
puts "STOP" | |
twitter_api.post("@#{MY_SCREEN_NAME} アマゾン自動購入を停止します。") | |
exit 1 | |
end | |
end | |
} | |
rescue Exception => e | |
exit 1 if e.to_s == "" || e.to_s == "exit" | |
puts "ERROR: #{e}" | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment