Skip to content

Instantly share code, notes, and snippets.

@shriphani
Last active December 11, 2015 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shriphani/4539017 to your computer and use it in GitHub Desktop.
Save shriphani/4539017 to your computer and use it in GitHub Desktop.
#lang racket
(require net/url)
(require (planet dherman/json:4:0))
; Blekko API implementation
(struct blekko (queries api-key))
(define *BLEKKO-BASE-URL* "http://blekko.com/ws/")
(define (issue-queries a-blekko gather-results-file)
(for ((q (blekko-queries a-blekko)))
(display q)
(newline)
(append-to-file 'url (issue-query q (blekko-api-key a-blekko) 0 '()) gather-results-file)))
; collect results from all pages
(define (issue-query q auth page-no acc-results)
(let* ((cur-pg-results (with-handlers
([exn:fail? (lambda (x) (make-hash))])
(read-json (get-pure-port
(struct-copy
url
(string->url *BLEKKO-BASE-URL*)
(query (list
(cons 'q (string-join
(list q
" /json /ps=100") ""))
(cons 'auth auth)
(cons 'p (number->string page-no))))))))))
(if (member 'RESULT (hash-keys cur-pg-results))
(begin
(sleep 1)
(display "next page")
(newline)
; (display (map (lambda (r) (hash-ref r 'url)) (hash-ref cur-pg-results 'RESULT)))
; (newline)
(issue-query q auth (+ page-no 1) (append acc-results (hash-ref cur-pg-results 'RESULT))))
acc-results)))
; given a field in the blekko results obj, we dump the associated data obj
(define (dump-to-file result-field results filename)
(display-lines-to-file (map (lambda (x) (hash-ref x result-field)) results) filename #:mode 'text))
(define (append-to-file result-field results filename)
(display-lines-to-file (map (lambda (x) (hash-ref x result-field)) results) filename #:mode 'text #:exists 'append))
(provide (all-defined-out))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment