Rubyによるアメブロ『いいね』自動化スクリプト(2) 『いいね』をつけるスクリプト
1回目で説明した「いいね」をつける仕組みをrubyで実装する。
まずはアメブロにログインしてクッキーを入手するスクリプト
require 'digest/md5' require 'net/http' Net::HTTP.version_1_2 cookie = Hash.new #Set-Cookie def set_cookie_from_res(res,cook) if not res.get_fields('Set-cookie') == nil res.get_fields('Set-cookie').each{|str| k,v = str[0...str.index(';')].split('=') cook[k] = v } end end def cookie_str(cook) cook.map{|k,v| "#{k}=#{v}" }.join(';') end #Program Start print "Please input your BlogID :" blog_id = gets.chop print "Please input your password :" blog_pwd = gets.chop print "Please input iineNo :" iine_no = gets.chop.to_i #ログインしてクッキーを入手 http = Net::HTTP.new("www.ameba.jp",80) path = '/' req=Net::HTTP::Get.new(path) req['Connection'] = 'Keep-Alive' req['Cookie'] = cookie_str(cookie) req['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.0; Win64; x64; rv:29.0) Gecko/20100101 Firefox/29.0' http.start response = http.request(req) set_cookie_from_res(response,cookie) path = '/login.do' post_data = 'amebaId=' + blog_id + '&password=' + Digest::MD5.hexdigest(blog_pwd) + '&Submit.x=0&Submit.y=0' req = Net::HTTP::Post.new(path) req['Connection'] = 'Keep-Alive' req['Cookie'] = cookie_str(cookie) req['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.0; Win64; x64; rv:29.0) Gecko/20100101 Firefox/29.0' response = http.request(req,post_data) if response.code == '302' set_cookie_from_res(response,cookie) #ここに「いいね」をつけるスクリプトを書く else print "Wrong ID or wrong pathword!!" end
このスクリプトは前のペタ貼りのスクリプトと同じ。
ユーザエージェントの設定は、素直にrubyのままだとサーバー側で弾かれるかも知れないのでFireFoxを詐称している。(アメブロさん、ごめんなさい)
上記のスクリプトの『#ここに「いいね」をつけるスクリプトを書く』の部分に、コードを入れる。
まずは、準備として、
(1)「いいね」をつけたいアメブロのidが入っている配列
(2)サーバーから受け取った文字列からデータを抽出するための正規表現
を定義する。
iine_list = Array.new#「いいね」つけたいidを入れる配列 iine_entry_url = Regexp.new("/web/display_iine.html\\?receiveAmebaId=[A-Za-z0-9\-] +&entryId=[0-9]+&from=entry&device=pc")#いいねボタン検出 iine_receiveAmebaId = Regexp.new('name="receiveAmebaId" value="[A-Za-z0-9\\-]+"') iine_entryId = Regexp.new('name="entryId" value="[0-9]+"') iine_token = Regexp.new('name="token" value="[a-z0-9]+"')
前回の「いいね」のしくみで調べたように、「いいね」の管理は一般的のアメブロの記事と別サーバーなので、スクリプトではhttp2を用意して、「いいね」のサーバーを割り当てる。
「いいね」をつけるスクリプトのコードは以下の通り
http2 = Net::HTTP.new("iine.blog.ameba.jp",80) iine_list.each do |line| begin print "Checking ", line ," blog... " target_blog_path = '/' + line + '/' response = http.get(target_blog_path,header) set_cookie_from_res(response,cookie) if response.code == '404' print "Can't find target blog." else iine_entry = iine_entry_url.match(response.body).to_s path = iine_entry+' HTTP/1.1' header['Referer'] = 'http://ameblo.jp'+target_blog_path response = http2.get(path,header) if response.body =~ /alreadyIineFlg:true/ print "Already iine" else receiveAmebaId = iine_receiveAmebaId.match(response.body).to_s.sub (/name="receiveAmebaId" value=/,'').tr('"','') if not receiveAmebaId == '' entryId = iine_entryId.match(response.body).to_s.sub (/name="entryId" value=/,'').tr('"','') token = iine_token.match(response.body).to_s.sub (/name="token" value=/,'').tr('"','') postdata = 'receiveAmebaId=' + receiveAmebaId + '&entryId=' + entryId + '&token=' + token + '&from=entry&device=pc' print "Sending iine..." path = '/web/exec_iine.html HTTP/1.1' header['Referer'] = 'http://iine.blog.ameba.jp/web/display_iine.html?receiveAmebaId=' + receiveAmebaId + '&entryId=' + entryId + '&from=entry&device=pc' response = http2.post(path,postdata,header) print "OK" else print "No iine_entry in this blog." end end end rescue Timeout::Error print "Timeout!!" rescue Errno:: ECONNRESET print "Econn reset!!" ensure print "\n" end end
さて、問題はiine_listの中に入れるアメブロのid。
ターゲットになるidをどうやって取得するのか…
次回は、その取得方法について書く。