#lang scheme
;; This is an example script using the Flickr API.
;; It authenticates a user, searches their list of photos,
;; and sends the user to the thumbnail 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.

(require (planet dvanhorn/flickr))

;; You'll have to get your own Flickr API key to make this program work!
;; dvanhorn's Key and Secret
(current-api-key "9cf50ab165c6da4189e8b55f519ad18b")
(current-sec-key "41b0847d415c8359")

(require (lib "external.ss" "browser")
         (lib "url.ss" "net")
         (lib "file.ss"))

(define (exn:flickr:invalid-auth-token? exn)
  (and (exn:flickr? exn)
       (= 98 (exn:flickr-code exn))))

(define (authenticate!)
  (parameterize ((signed? #t))
    (match (flickr.auth.getFrob)
      [(list (list 'frob '() frob))
         (send-url (url->string (authorize-url (current-sec-key)
                                               (cons 'api_key (current-api-key))
                                               (cons 'frob frob)
                                               (cons '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)
             [(list (list 'auth '() 
                          (list 'token '() token)
                          (list 'perms '() perms)
                          (list 'user (list (list 'fullname fn)
                                            (list 'nsid nsid)
                                            (list '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 ((signed? #t))
            (flickr.auth.checkToken #:auth_token auth-token)

(define (run-example!)
  (parameterize ((non-text-tags (list* 'photos (non-text-tags)))
                 (signed? #t))        
    (match (flickr.photos.search #:user_id "me" 
                                 #:auth_token (get-preference 'flickr:token))
      [(list (list-rest 'photos 
                        (list-no-order (list 'page page)
                                       (list 'pages pages)
                                       (list 'perpage perpage)
                                       (list 'total total))
                        (list 'photo 
                              (list-no-order (list 'farm farm)
                                             (list 'id id)
                                             (list 'isfamily _)
                                             (list 'isfriend _)
                                             (list 'ispublic _)
                                             (list 'owner owner)
                                             (list 'secret secret)
                                             (list 'server server)
                                             (list 'title _)))
        (format "http://farm~a.static.flickr.com/~a/~a_~a_t.jpg"
                farm server id secret))])))