-
Notifications
You must be signed in to change notification settings - Fork 0
/
shipment_tracking_utility.rb
249 lines (220 loc) · 6.5 KB
/
shipment_tracking_utility.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'json'
class ShipmentTrackingUtilityPlugin < Plugin
class ShipmentStatus
@number = nil
@location = nil
@time = nil
@activity = nil
@carrier = nil
attr_reader :number, :location, :time, :activity, :carrier
def initialize(data)
@number = data[:number]
@location = data[:location]
@time = data[:time]
@activity = data[:activity]
@carrier = data[:carrier]
end # initialize
def ircify
"#{activity}#{" @ #{time}" if time}#{" - #{location}" if location}"
end
end # ShipmentStatus
module Scrapers
module UPS
NAME_KEYS = [:ups]
PRIMARY_NAME = 'UPS'
def self.fetch(number)
doc = Nokogiri::HTML(open("http://wwwapps.ups.com/WebTracking/processInputRequest?sort_by=status&tracknums_displayed=1&TypeOfInquiryNumber=T&loc=en_US&InquiryNumber1=#{number}&track.x=0&track.y=0"))
table = doc.search('table[@class=dataTable]').first
latest_row = table.search('tr')[1]
if latest_row
cells = latest_row.search('td')
status = ShipmentStatus.new(
:number => number,
:location => cells[0].content.ircify_html,
:time => (cells[1].content + " " + cells[2].content).ircify_html,
:activity => cells[3].content.ircify_html,
:carrier => PRIMARY_NAME
)
else
nil
end
end # fetch UPS
end
module FedEx
NAME_KEYS = [:fedex]
PRIMARY_NAME = 'FedEx'
def self.fetch(number)
doc = open("http://www.fedex.com/Tracking?language=english&cntry_code=us&tracknumbers=#{number}")
data = ''
doc.each_line do |line|
if line =~ /^var detailInfoObject/
data = line
break # ignore the rest of this page
end
end
data = data.sub(/var detailInfoObject = /, '').sub(/;\n$/, '')
if data == ''
return nil
end
# keys = ["scanDate", "GMTOffset", "showReturnToShipper", "scanStatus", "scanLocation", "scanTime", "scanComments"]
data = JSON.parse(data)
latest_row = data['scans'].first
if latest_row
status = ShipmentStatus.new(
:number => number,
:location => latest_row['scanLocation'],
:time => latest_row['scanDate'] << ' ' << latest_row['scanTime'],
:activity => latest_row['scanStatus'],
:carrier => PRIMARY_NAME
)
else
nil
end
end # fetch fedex
end
module Purolator
NAME_KEYS = [:purolator]
PRIMARY_NAME = 'Purolator'
def self.fetch(number)
doc = Nokogiri::HTML(open("https://eshiponline.purolator.com/SHIPONLINE/Public/Track/TrackingDetails.aspx?pin=#{number}"))
latest_row = doc.search('//div[@id="detailTable"]/table/tbody/tr').first
if latest_row
latest_row = latest_row.search('./td')
status = ShipmentStatus.new(
:number => number,
:location => nil,
:time => (latest_row[0].inner_text + ' ' + latest_row[1].inner_text).gsub("\n", ' ').gsub(/\s+/, ' '),
:activity => latest_row[2].inner_text.gsub("\n", ' ').gsub(/\s+/, ' '),
:carrier => PRIMARY_NAME
)
else
nil
end
end
end
module Newegg
NAME_KEYS = [:newegg]
PRIMARY_NAME = 'newegg'
def self._get_json_obj(doc)
javascript_tags = doc.search('script[@type="text/javascript"]').
find_all {|x| x.to_s.include?('detailInfoObject') }
if javascript_tags.empty?
return nil
end
split_data = javascript_tags[0].text.strip.
split(";\r\n")[0].split("=", 2)
if split_data.size != 2
return nil
end
return JSON.parse(split_data[1].strip)
end
def self.fetch(number)
doc = Nokogiri::HTML(open("http://www.newegg.com/Info/TrackOrder.aspx?TrackingNumber=#{number}"))
latest_row = doc.search('table[@class="trackDetailUPSSum"]/tr')[1]
json_data = _get_json_obj(doc)
if json_data and json_data.include?('scans') and !json_data['scans'].empty?
latestScan = json_data['scans'][0]
ShipmentStatus.new(
:number => number,
:location => latestScan['scanLocation'],
:time => ("%s %s %s" % [
latestScan['scanDate'],
latestScan['scanTime'],
latestScan['GMTOffset']
]),
:activity => latestScan['scanStatus'],
:carrier => PRIMARY_NAME
)
elsif latest_row
datetime, location, activity = latest_row.search('td').map(&:text)
ShipmentStatus.new(
:number => number,
:location => location,
:time => datetime,
:activity => activity,
:carrier => PRIMARY_NAME
)
end
end
end
module PackageTrackr
NAME_KEYS = [:packagetrackr]
PRIMARY_NAME = 'packagetrackr'
def self.fetch(number)
doc = Nokogiri::HTML(open("http://www.packagetrackr.com/track/#{number}"))
latest_row = doc.search('#track-info-progress tr')[0]
if latest_row
details = latest_row.children[0].inner_text.strip.split(/\n/)
status = ShipmentStatus.new(
:number => number,
:location => details[0],
:time => details[1],
:activity => details[2],
:carrier => PRIMARY_NAME
)
else
nil
end
end
end
module USPS
NAME_KEYS = [:usps]
PRIMARY_NAME = 'USPS'
def self.fetch(number)
doc = Nokogiri::HTML(open("https://tools.usps.com/go/TrackConfirmAction_input?origTrackNum=#{number}"))
ShipmentStatus.new(
:number => number,
:location => doc.css('.shaded .location')[0].text.strip,
:time => doc.css('.shaded .date-time')[0].text.strip,
:activity => doc.css('.shaded .status')[0].text.strip,
:carrier => PRIMARY_NAME
)
end
end
end
class ShipmentScreenScraperManager
def initialize()
@by_name = Hash.new
@loaded_modules = Array.new
end
def loaded_modules
@loaded_modules.dup
end
def register(scraper_module)
for name in scraper_module::NAME_KEYS
if @by_name.has_key?(name.to_sym)
raise RuntimeException, "already registered."
end
@by_name[name.to_sym] = scraper_module
end
@loaded_modules << scraper_module
end
def [](name)
@by_name[name.to_sym]
end
def has_courier?(name)
@by_name.has_key?(name.to_sym)
end
def fetch(courier, number)
self[courier.to_sym].fetch(number)
end
end
def initialize()
super
@scrapers = ShipmentScreenScraperManager.new()
@scrapers.register(Scrapers::UPS)
@scrapers.register(Scrapers::FedEx)
@scrapers.register(Scrapers::Purolator)
@scrapers.register(Scrapers::Newegg)
@scrapers.register(Scrapers::PackageTrackr)
@scrapers.register(Scrapers::USPS)
end
attr_accessor :scrapers
def help(plugin, topic="")
"This plugin is a utility plugin which is only meant to be used by other plugins."
end
end
ShipmentTrackingUtilityPlugin.new