;; This is an example script using the Flickr API.
;; It authenticates a user, searches their list of photos,
;; and sends the user to the thumnail view of their most
;; recently uploaded photo on flickr.

;; Authentication tokens are stored in the user's preferences
;; and a user is redirected to Flickr's authentication URL only
;; if there is not a valid token available.

;; This program requires "read" permission.

(module example mzscheme
  (require "flickr.ss")
  ;; You'll have to get your own Flickr API key to make this program work!
  ;; dvanhorn's Key and Secret
  (current-api-key "*********")
  (current-sec-key "*********")
  (require (lib "external.ss" "browser")
           (lib "url.ss" "net")
           (lib "match.ss")
           (lib "file.ss"))
  (define (exn:flickr:invalid-auth-token? exn)
    (and (exn:flickr? exn)
         (= 98 (exn:flickr-code exn))))
  (define (authenticate!)
    (parameterize ((sign-all? #t))
      (match (flickr.auth.getFrob)
        [(('frob () frob))
           (send-url (url->string (authorize-url #:frob frob #:perms "read")))
           (read) ;; type something to continue after visiting URL.
           (parameterize ((non-text-tags (list* 'auth (non-text-tags))))
             (match (flickr.auth.getToken #:frob frob)
               [(('auth () 
                        ('token () token)
                        ('perms () perms)
                        ('user (('fullname fn) ('nsid nsid) ('username user)))))
                (put-preferences (list 'flickr:token) (list token))])))])))
  (define (maybe-authenticate!)
    (let ((auth-token (get-preference 'flickr:token)))
      (if auth-token
          (with-handlers ((exn:flickr:invalid-auth-token?
                           (lambda (exn) (authenticate!))))
            (parameterize ((sign-all? #t))
              (flickr.auth.checkToken #:auth_token auth-token)
  (define (run-example!)
    (parameterize ((non-text-tags (list* 'photos (non-text-tags)))
                   (sign-all? #t))
      (match (flickr.photos.search #:user_id "me" #:auth_token (get-preference 'flickr:token))
        [(('photos _ ('photo (('farm farm)
                              ('id id)
                              ('isfamily _)
                              ('isfriend _)
                              ('ispublic _)
                              ('owner owner)
                              ('secret secret)
                              ('server server)
                              ('title _))) . rest))
          (format "http://farm~a.static.flickr.com/~a/~a_~a_t.jpg"
                  farm server id secret))])))
  ) ; end of module example