Created
May 29, 2013 20:18
-
-
Save bhyde/5673492 to your computer and use it in GitHub Desktop.
Snarf historical prices from ebay... via common lisp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(in-package "CL-USER") | |
(let ((*print-case* :upcase)) | |
(ql:quickload '(#:fare-memoization #:parse-number | |
#:optima #:optima.ppcre #:fare-quasiquote-optima | |
#:drakma #:closure-html))) | |
(defpackage #:eb | |
(:use #:common-lisp #:fare-memoization #:parse-number | |
#:optima #:optima.ppcre | |
#:drakma #:closure-html)) | |
(in-package "EB") | |
(defun fetch-price-page (query page-number) | |
;; for example: (fetch-price-page "macbook air 13" 3) | |
(let* ((url-template | |
"http://www.ebay.com/csc/i.html?_from=R40&_sacat=0&LH_Complete=1&_nkw=~a&_pgn=~d&_skc=50&rt=nc") | |
(url (format nil url-template (url-encode query *drakma-default-external-format*) page-number))) | |
(parse (http-request url) (closure-html:make-lhtml-builder)))) | |
(memoize 'fetch-price-page) | |
(defun snarf-prices (page-sxpr) | |
;; for example: (snarf-prices (fetch-price-page "macbook air 13" 3)) | |
;; | |
;; They look like this: | |
;; (:div ((:class "g-b bidsold") (:itemprop "price")) " | |
;; $18.99") | |
(let ((result ())) | |
(labels ((recure (x) | |
(match x | |
(`(:div ((:class "g-b bidsold") (:itemprop "price")) | |
,(ppcre "\\w*\\$([\\d.]+)" price)) | |
(push (parse-number price) result)) | |
((list* (satisfies keywordp) * children) | |
(map nil #'recure children))))) | |
(recure page-sxpr) | |
(nreverse result)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment