From 6a0621468b5dc5e2eff569ad3fa0f3cf7efd434b Mon Sep 17 00:00:00 2001 From: Feufochmar Date: Mon, 26 Dec 2022 14:10:22 +0100 Subject: [PATCH] Remove Twitter support in bots. --- postbot/arnytron.rkt | 13 ----- postbot/configuration.rkt | 18 ------ postbot/floraverse.rkt | 42 ++++---------- postbot/twitter.rkt | 119 -------------------------------------- 4 files changed, 10 insertions(+), 182 deletions(-) delete mode 100644 postbot/twitter.rkt diff --git a/postbot/arnytron.rkt b/postbot/arnytron.rkt index 65c2604..50f5281 100644 --- a/postbot/arnytron.rkt +++ b/postbot/arnytron.rkt @@ -2,7 +2,6 @@ (require net/url "mastodon.rkt" - "twitter.rkt" "configuration.rkt") ; Do an API call to the website @@ -19,15 +18,3 @@ quote #:visibility "unlisted")) (newline) - -; Publication on Twitter -(display - (send - (new twitter-client% - [consumer-key arnytron:twitter-consumer-key] - [consumer-secret arnytron:twitter-consumer-secret] - [access-key arnytron:twitter-access-token] - [access-secret arnytron:twitter-access-token-secret]) - status-update - quote)) -(newline) diff --git a/postbot/configuration.rkt b/postbot/configuration.rkt index 027b779..47efa62 100644 --- a/postbot/configuration.rkt +++ b/postbot/configuration.rkt @@ -6,17 +6,9 @@ ; arnytron:mastodon-instance arnytron:mastodon-authorization-bearer - arnytron:twitter-consumer-key - arnytron:twitter-consumer-secret - arnytron:twitter-access-token - arnytron:twitter-access-token-secret ; floraverse:mastodon-instance floraverse:mastodon-authorization-bearer - floraverse:twitter-consumer-key - floraverse:twitter-consumer-secret - floraverse:twitter-access-token - floraverse:twitter-access-token-secret ; daily-island:mastodon-instance daily-island:mastodon-authorization-bearer @@ -30,22 +22,12 @@ (define arnytron:mastodon-instance "...") ; OAuth2 authorization bearer (define arnytron:mastodon-authorization-bearer "...") -; Twitter keys -(define arnytron:twitter-consumer-key "...") -(define arnytron:twitter-consumer-secret "...") -(define arnytron:twitter-access-token "...") -(define arnytron:twitter-access-token-secret "...") ; Floraverse postbot ; Publication on Mastodon (define floraverse:mastodon-instance "...") ; OAuth2 authorization bearer (define floraverse:mastodon-authorization-bearer "...") -; Twitter keys -(define floraverse:twitter-consumer-key "...") -(define floraverse:twitter-consumer-secret "...") -(define floraverse:twitter-access-token "...") -(define floraverse:twitter-access-token-secret "...") ; Daily Island postbot ; Publication on Mastodon diff --git a/postbot/floraverse.rkt b/postbot/floraverse.rkt index a6a3825..91eac2e 100644 --- a/postbot/floraverse.rkt +++ b/postbot/floraverse.rkt @@ -3,7 +3,6 @@ (require net/url json "mastodon.rkt" - "twitter.rkt" "configuration.rkt" srfi/8) @@ -14,7 +13,6 @@ (string->url character-generator)))) ; Function to generate a character -; Each string defining the character should be less than 140 characters (twitter limit) ; The concatenation of strings should be less than 500 characters (mastodon limit) ; The function generates characters as long as the limits are not respected ; The function returns two values: a list of strings for twitter and a single string for mastodon @@ -26,36 +24,16 @@ (hash-ref chr 'motto) (hash-ref chr 'traits))) (define toot (string-join tweets "\n")) - (if (or (>= (string-length toot) 500) - (member #t (map (lambda (x) (>= (string-length x) 140)) tweets))) + (if (>= (string-length toot) 500) (generate-messages) - (values tweets toot))) + toot)) ; Post a character -(receive - (tweets toot) (generate-messages) - ; Send to mastodon - (send - (new mastodon-client% - [instance floraverse:mastodon-instance] - [authorization-bearer floraverse:mastodon-authorization-bearer]) - new-status - toot - #:visibility "public") - ; Send to twitter - (define client (new twitter-client% - [consumer-key floraverse:twitter-consumer-key] - [consumer-secret floraverse:twitter-consumer-secret] - [access-key floraverse:twitter-access-token] - [access-secret floraverse:twitter-access-token-secret])) - (foldl - (lambda (message id-previous) - (hash-ref - (send - client - status-update - message - #:in-reply-to-id id-previous) - 'id_str)) - #f - tweets)) +; Send to mastodon +(send + (new mastodon-client% + [instance floraverse:mastodon-instance] + [authorization-bearer floraverse:mastodon-authorization-bearer]) + new-status + (generate-messages) + #:visibility "public") diff --git a/postbot/twitter.rkt b/postbot/twitter.rkt deleted file mode 100644 index 854fd68..0000000 --- a/postbot/twitter.rkt +++ /dev/null @@ -1,119 +0,0 @@ -#lang racket/base - -(require racket/class - racket/string - net/url - net/uri-codec - json - racket/date - web-server/stuffers/hmac-sha1 - net/base64) - -(provide twitter-client%) - -(define twitter-client% - (class object% - ; Needs the various OAuth keys and secrets - (init consumer-key consumer-secret access-key access-secret) - (super-new) - ; - (define twitter-consumer-key consumer-key) - (define twitter-consumer-secret consumer-secret) - (define twitter-access-token access-key) - (define twitter-access-token-secret access-secret) - ; Twitter root - (define twitter-root "https://api.twitter.com/1.1") - ; - ; Private methods for OAuth 1.0 - ; To generate timestamp & nonce - (define (get-timestamp) (number->string (current-seconds))) - ; Compute the parameter string - ; The oauth-headers and parameters are lists of (key . value) - (define (compute-signature-parameter-string oauth-headers parameters) - (string-join - (sort - (map - (lambda (x) - (string-append (uri-unreserved-encode (car x)) "=" (uri-unreserved-encode (cdr x)))) - (append oauth-headers parameters)) - stringstring/utf-8 - (base64-encode - (HMAC-SHA1 - (string->bytes/utf-8 signature-key) - (string->bytes/utf-8 signature-base-string)) - #""))) - ; compute the signature from the request - (define (compute-signature-from-request request-type url oauth-headers parameters) - (compute-signature - (compute-signature-key) - (compute-signature-base-string - request-type - url - (compute-signature-parameter-string - oauth-headers - parameters)))) - ; - ; Post a new status - ; Return a dictionnary corresponding to the json structure returned by the server - (define/public (status-update status - #:in-reply-to-id [in-reply-to-id #f]) - (let* - ((url (string-append twitter-root "/statuses/update.json")) - (parameters - (append - (list (cons "status" status)) - (if in-reply-to-id - (list - (cons "in_reply_to_status_id" in-reply-to-id) - (cons "auto_populate_reply_metadata" "true")) - (list)) - )) - (timestamp (get-timestamp)) - (oauth-headers - (list - (cons "oauth_consumer_key" twitter-consumer-key) - (cons "oauth_token" twitter-access-token) - (cons "oauth_version" "1.0") - (cons "oauth_timestamp" timestamp) - (cons "oauth_nonce" timestamp) - (cons "oauth_signature_method" "HMAC-SHA1")))) - (read-json - (post-pure-port - (string->url url) - (string->bytes/utf-8 - (string-join - (map - (lambda (x) (string-append (uri-unreserved-encode (car x)) "=" (uri-unreserved-encode (cdr x)))) - parameters) - "&")) - (list - "Content-Type: application/x-www-form-urlencoded" - (string-append - "Authorization: OAuth " - "realm=\"\"," - (string-join - (map - (lambda (x) (string-append (uri-unreserved-encode (car x)) "=\"" (uri-unreserved-encode (cdr x)) "\"")) - oauth-headers) - ",") - ",oauth_signature=\"" (uri-unreserved-encode (compute-signature-from-request "POST" url oauth-headers parameters)) "\"") - ))))) - ))