{ Josh Rendek }

<3 Ruby & Go

Ruby and Ebay: Get list prices

Oct 3, 2010 - 2 minutes

I’ll eventually be turning my ruby ebay work into a gem, but for now here are little snippets:

Please note that this code automagically takes care of things like 4x SOMETHING – it’ll take the current list price and divide by four, if you don’t want that functionality, remove the divider. You also need rest-open-uri and hpricot for gems.

To get a list of prices:

 1def self.get_search_results(query)
 2            # API request variables
 3            endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1';  # URL to call
 4            version = '1.0.0';  # API version supported by your application
 5            appid = 'YOUR APP ID';  # Replace with your own AppID
 6            globalid = 'EBAY-US';  # Global ID of the eBay site you want to search (e.g., EBAY-DE)
 7            safequery = URI.encode(query);  # Make the query URL-friendly
 8
 9            # Construct the findItemsByKeywords HTTP GET call
10            apicall = "#{endpoint}?";
11            apicall += "OPERATION-NAME=findItemsByKeywords";
12            apicall += "&SERVICE-VERSION=#{version}";
13            apicall += "&SECURITY-APPNAME=#{appid}";
14            apicall += "&GLOBAL-ID=#{globalid}";
15            apicall += "&keywords=#{safequery}";
16            apicall += "&paginationInput.entriesPerPage=25";
17
18            res = ""
19
20            open( apicall ).each { |s| res << s }
21            prices = []
22
23
24            doc = Hpricot.parse(res)
25            (doc/:item).each do |x|
26
27                divider = 1
28                get_count = (x/:title).inner_html.scan(/[0-9]/).first
29                p "#{(x/:title).inner_html}"
30                if !get_count.nil?
31                    divider = get_count.to_i
32                end
33                prices << (x/:sellingstatus/:currentprice).inner_html.to_f/divider
34            end
35
36            prices
37end
comments powered by Disqus