Skip to content

Instantly share code, notes, and snippets.

@mad
Created April 5, 2011 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mad/904099 to your computer and use it in GitHub Desktop.
Save mad/904099 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby -wKU
# -*- coding: utf-8 -*-
require 'cgi'
require 'uri'
require 'net/http'
require 'nokogiri'
require 'pp'
# TODO: think about easily add new tracking system!
class Tracking
@@ru_url = 'http://info.russianpost.ru/servlet/post_item'
@@ru_params = {
'action' => 'search',
'searchType' => 'barCode',
'barCode' => 'XXXXXXXXXXXXX',
'show_form' => 'yes',
'page' => '1',
'barCodeSearchBtn' => 'Поиск'
}
def ru_track(bar_code)
@@ru_params['barCode'] = bar_code
x = Net::HTTP.post_form(URI.parse(@@ru_url), @@ru_params)
doc = Nokogiri::HTML(x.body)
rows = doc.xpath('//table[@width=""]/tr[@valign="top"]')
details = rows.collect do |row|
detail = {}
[
[:op, 'td[1]/text()'],
[:date, 'td[2]/text()'],
[:cur_index, 'td[3]/text()'],
[:cur_addr, 'td[4]/text()'],
[:attr, 'td[5]/text()'],
[:weight, 'td[6]/text()'],
[:price, 'td[7]/text()'],
[:cod, 'td[8]/text()'],
[:to_index, 'td[9]/text()'],
[:to_addr, 'td[10]/text()'],
].collect do |name, xpath|
detail[name] = row.at_xpath(xpath).text.strip
end
detail
end
end
end
if __FILE__ == $PROGRAM_NAME
tracks = ['xxxxxxxxxxxxx', 'yyyyyyyyyyyyy', 'zzzzzzzzzzzzz']
t = Tracking.new
tracks.each do |tr|
details = t.ru_track(tr)
puts tr
details.each do |i|
puts i.values.flatten.join(" ")
end
puts
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment