example.scm 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ;; Connect to the Pleroma instance and dump the home timeline onto
  2. ;; your screen. This example demonstrates how to authenticate with the
  3. ;; server, get the necessary tokens and serialize them to a file. The
  4. ;; client is serialized to the local file "_client". If there is an
  5. ;; error you can regenerate the authorization data by removing the
  6. ;; "_client" file.
  7. ;; run as `guile -s example.scm`
  8. ;; geiser: C-c C-e C-l to add "." to the load path
  9. (use-modules
  10. (ice-9 rdelim)
  11. (ice-9 format)
  12. (ice-9 match)
  13. (srfi srfi-1)
  14. (web uri)
  15. ;; local
  16. (tapris client))
  17. (define instance (string->uri "https://satania.space"))
  18. (define (ask-for-code client)
  19. (let ((u (build-authorize-url client)))
  20. (format #t "Please visit the following URL to obtain the \
  21. authorization code: ~a\nPlease input the code: " (uri->string u))))
  22. (define (client->list client)
  23. (match client
  24. (($ <client> instance id secret token)
  25. (list (uri->string instance) id secret token))))
  26. (define (list->client ls)
  27. (match ls
  28. ((instance id secret token)
  29. (make-client (string->uri instance) id secret token))))
  30. (define (new-client instance)
  31. (let ((client (register-app instance)))
  32. (ask-for-code client)
  33. (let* ((auth-code (read-line))
  34. (token (get-token client auth-code)))
  35. (set-client-token! client token)
  36. (format #t "Trying to verify the token ~a.\n" auth-code)
  37. (catch 'pleroma (lambda () (verify-credentials client))
  38. (lambda (keys . args)
  39. (format #t "Error!\n")
  40. (for-each (lambda (a) (format #t "-- ~a\n" a)) args)
  41. (exit 1)))
  42. (format #t "Verifcation succeeded! Saving the client data locally.\n")
  43. (let ((port (open-output-file "_client")))
  44. (write (client->list client) port)
  45. (close-port port)
  46. client))))
  47. (define (obtain-client)
  48. (define (try-read)
  49. (let* ((port (open-input-file "_client"))
  50. (client (list->client (read port))))
  51. (close-port port)
  52. client))
  53. (catch 'system-error try-read
  54. (lambda (key . args)
  55. (match args
  56. (("open-file" fmt . rest)
  57. ;; Need to get a new token.
  58. (new-client instance))
  59. (_ (apply throw (cons 'system-error args)))))))
  60. (define client (obtain-client))
  61. (define sts (get-home-timeline client))
  62. (define (format-status st)
  63. (let ((content (status-content st))
  64. (from (assoc-ref (status-account st) "username"))
  65. (medias (map (lambda (m)
  66. (assoc-ref m "url"))
  67. (vector->list (status-media-attachments st)))))
  68. (format #t "~a: ~a\n\n" from content)))
  69. (for-each format-status sts)