-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_external_item_prices.rb
executable file
·58 lines (43 loc) · 1.49 KB
/
update_external_item_prices.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
#!/usr/bin/ruby
$:.unshift File.dirname(__FILE__) + '/lib'
require 'rubygems'
require 'bundler/setup'
require 'rest_client'
require 'uri'
require 'json'
require_relative 'mtg'
app_id = 'BenPrew2f-def9-421f-87b8-55dc6a53837'
ebay_api_version = 595
gateway = 'http://open.api.ebay.com/shopping'
def url_ify(gateway, params)
url = gateway + params.inject('?') { |r, e| r + "#{e[0]}=#{URI.escape e[1].to_s}&" }
url.chop
end
@@current_time = Time.parse(JSON.parse(RestClient.get(url_ify( gateway,
:appid => app_id,
:version => ebay_api_version,
:responseencoding => 'JSON',
:callname => 'GeteBayTime' )))['Timestamp'])
items = ExternalItem.filter('end_time < ?', @@current_time - 24 * 60 * 60).
filter(:has_been_finalized => 0).all
while (items.length > 0) do
warn "getting 20 items " + items.length.to_s
data = JSON.parse(RestClient.get( url_ify(
gateway,
:appid => app_id,
:version => ebay_api_version,
:responseencoding => 'JSON',
:callname => 'GetItemStatus',
:ItemId =>items.slice!(0,20).map { |i| i.external_item_id }.join(",")
)))
data['Item'].each do |i|
e = ExternalItem[i['ItemID']]
e.has_been_finalized = 1
e.last_updated = @@current_time
if i.has_key?('BidCount') && i['BidCount'] > 0
warn "updating price for #{i['ItemID']} #{i['ConvertedCurrentPrice']['Value']}"
e.price = i['ConvertedCurrentPrice']['Value']
end
e.save
end
end