Skip to content

Instantly share code, notes, and snippets.

@mimosa
Last active September 9, 2016 09:26
Show Gist options
  • Save mimosa/35bb211860516f9a08f04651f2ef4f7d to your computer and use it in GitHub Desktop.
Save mimosa/35bb211860516f9a08f04651f2ef4f7d to your computer and use it in GitHub Desktop.
快递跟踪
tracer = Kuaidi100.new
a = tracer.trace('1202306458234')
# =>
{
  :status          => true, 
  :shipping_method => "韵达", 
  :tracking_no     => "1202306458234", 
  :routes          => [{
    :time    => "2016-09-09 15:41:22", 
    :context => "从辽宁沈阳分拨中心发出,本次转运目的地:辽宁沈阳浑南开发公司"
  }, {
    :time    => "2016-09-09 14:37:04", 
    :context => "在分拨中心辽宁沈阳分拨中心进行卸车扫描"
  }, {
    :time    => "2016-09-08 19:31:20", 
    :context => "在北京分拨中心进行装车扫描,即将发往:辽宁沈阳分拨中心"
  }, {
    :time    => "2016-09-08 16:38:59", 
    :context => "在分拨中心北京分拨中心进行卸车扫描"
  }, {
    :time    => "2016-09-07 00:16:44", 
    :context => "在广西南宁分拨中心进行装车扫描,即将发往:北京分拨中心"
  }, {
    :time    => "2016-09-07 00:07:46", 
    :context => "在广西南宁分拨中心进行中转集包扫描,将发往:辽宁沈阳网点包"
  }, {
    :time    => "2016-09-06 22:39:05", 
    :context => "在分拨中心广西南宁分拨中心进行称重扫描"
  }, {
    :time    => "2016-09-06 17:26:53", 
    :context => "在广西上林县公司进行发出扫描,将发往:广西南宁分拨中心"
  }], 
  :state           => "在途"
}
a = tracer.trace('abababababab', '韵达')
# =>
  {
    :status  => false, 
    :message => "快递公司参数异常:单号格式错误"
  }
# -*- encoding: utf-8 -*-
class Kuaidi100 < RestCli
def initialize
connection.url_prefix = 'http://m.kuaidi100.com'
end
def trace(no, shipping_method = nil)
code = providers[shipping_method] || find_code(no)
return tracking(no, code) unless code.blank?
{ status: false, message: '快递公司参数异常:单号不存在或者已经过期' }
end
private
def find_code(no)
resp = post '/autonumber/auto', { num: no }, headers(no)
resp[0]["comCode"] unless resp.blank?
end
def tracking(no, code)
resp = get '/query', { type: code, postid: no }, headers(no)
if resp.blank?
{ status: false, message: '服务器出错。' }
elsif resp['status'] == "200"
{
status: true,
shipping_method: codes[code],
tracking_no: no,
routes: resp['data'].as_json( only: ['time', 'context'] ).map(&:symbolize_keys),
state: states[resp['state']]
}
else
{ status: false, message: resp['message'] }
end
end
def headers(no)
{
host: 'm.kuaidi100.com',
origin: 'http://m.kuaidi100.com',
referer: "http://m.kuaidi100.com/result.jsp?nu=#{no}",
user_agent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36',
x_requested_with: 'XMLHttpRequest'
}
end
def codes
{
'quanfengkuaidi' => '全峰',
'rufengda' => '如风达',
'shentong' => '申通',
'shunfeng' => '顺丰',
'yuantong' => '圆通',
'yunda' => '韵达',
'zhongtong' => '中通',
'zhaijisong' => '宅急送',
}
end
def providers
codes.invert
end
def states
{
'0' => '在途', # 即货物处于运输过程中;
'1' => '揽件', # 货物已由快递公司揽收并且产生了第一条跟踪信息;
'2' => '疑难', # 货物寄送过程出了问题;
'3' => '签收', # 收件人已签收;
'4' => '退签', # 即货物由于用户拒签、超区等原因退回', 而且发件人已经签收;
'5' => '派件', # 即快递正在进行同城派件;
'6' => '退回', # 货物正处于退回发件人的途中;
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment