Last active
December 11, 2015 03:38
-
-
Save shriphani/4539017 to your computer and use it in GitHub Desktop.
This file contains 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
#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