Skip to content

Instantly share code, notes, and snippets.

@takuya
Created August 9, 2014 14:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takuya/49f3b0fc1605f0e85000 to your computer and use it in GitHub Desktop.
Save takuya/49f3b0fc1605f0e85000 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#/*
# *
# * This program is free software: you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation, either version 3 of the License, or
# * (at your option) any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program. If not, see <http://www.gnu.org/licenses/>.
# *
# */
require 'mechanize'
require 'pp'
def trace_yamato( tracking_ids )
raise 'max 10 tracking_ids' if tracking_ids.size > 10
status_list = []
m = Mechanize.new
m.get("http://toi.kuronekoyamato.co.jp/cgi-bin/tneko")
f = m.page.form_with( :action => /tneko/)
tracking_ids.each.with_index{|tracking_id, idx |
f.fields_with( :name => /number/)[idx].value = tracking_id
}
f.submit
nodes = m.page.search("//center[./a]")
nodes.each{|node|
status = {}
status["伝票番号"] = node.search("*[@class='saisin']/tr[1]/td[3]").text.toutf8.split.last
status["最新状態"] = node.search("*[@class='saisin']/tr[2]/td[3]").text.toutf8
status["状態詳細"] = node.search("*[@class='saisin']/tr[3]/td[1]").text.toutf8
status[node.search("*[@class='meisaibase']//tr[1]/*[1]").text.toutf8] = node.search("*[@class='meisaibase']//tr[2]/*[1]").text.toutf8
status[node.search("*[@class='meisaibase']//tr[1]/*[2]").text.toutf8] = node.search("*[@class='meisaibase']//tr[2]/*[2]").text.toutf8
status["荷物追跡"] = []
updates = node.search("*[@class='meisai']/tr").size
(2..updates).each{|e|
keys = node.search("*[@class='meisai']/tr[1]/*/text()").map{|e| e.text()}
vals = node.search("*[@class='meisai']/tr[#{e}]/*/text()").map{|e| e.text()}
status["荷物追跡"] << Hash[*keys.zip(vals).flatten]
}
status_list << status
}
return status_list
end
pp trace_yamato(["4102-6406-xxxx","4102-6406-xxxx","4102-6406-xxxx"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment