From 95adb51ba0135bfb205a551b257c875034d2cc3b Mon Sep 17 00:00:00 2001 From: Martin Pels Date: Sun, 17 Nov 2024 15:17:25 +0100 Subject: [PATCH] send website news item notification posts to mastodon instead of twitter --- host_vars/public04.infra.ring.nlnog.net | 4 +- roles/hugo/tasks/main.yml | 20 ++--- .../monitor_ring_nlnog_net_posts.service.j2 | 2 +- ...tor_webdev_ring_nlnog_net_posts.service.j2 | 2 +- .../{tweet_hugo_post.j2 => toot_hugo_post.j2} | 90 ++++++++++--------- vault/public04.infra.ring.nlnog.net.yml | 39 ++++---- 6 files changed, 81 insertions(+), 76 deletions(-) rename roles/hugo/templates/{tweet_hugo_post.j2 => toot_hugo_post.j2} (51%) diff --git a/host_vars/public04.infra.ring.nlnog.net b/host_vars/public04.infra.ring.nlnog.net index c4df0c2cd..5286f9750 100644 --- a/host_vars/public04.infra.ring.nlnog.net +++ b/host_vars/public04.infra.ring.nlnog.net @@ -15,12 +15,12 @@ hugo_sites: repo: https://github.com/NLNOG/ring-web theme_name: ring-anubis theme_repo: https://github.com/NLNOG/hugo-theme-anubis - twitter: true + mastodon: true - name: ring.nlnog.net repo: https://github.com/NLNOG/ring-web theme_name: ring-anubis theme_repo: https://github.com/NLNOG/hugo-theme-anubis - twitter: true + mastodon: true paste_sites: - name: paste.ring.nlnog.net diff --git a/roles/hugo/tasks/main.yml b/roles/hugo/tasks/main.yml index 947c3c1d8..66efdb0d2 100644 --- a/roles/hugo/tasks/main.yml +++ b/roles/hugo/tasks/main.yml @@ -35,17 +35,17 @@ with_items: "{{ hugo_sites|default([]) }}" notify: hugo rebuild -- name: "Install post tweeting requirements" - apt: +- name: "Install post tooting requirements" + pip: name: - - python3-pyinotify - - python3-tweepy - state: latest + - Mastodon.py + state: present + executable: pip3 -- name: "Install post tweeter" +- name: "Install post tooter" template: - src=tweet_hugo_post.j2 - dest=/usr/local/bin/tweet_hugo_post + src=toot_hugo_post.j2 + dest=/usr/local/bin/toot_hugo_post owner=root group=root mode=0755 @@ -66,7 +66,7 @@ owner: root group: root mode: 0644 - when: item.twitter is defined + when: item.mastodon is defined with_items: "{{ hugo_sites|default([]) }}" notify: - reload systemd @@ -76,5 +76,5 @@ service: name: "monitor_{{ item.name|replace('.', '_') }}_posts" state: started - when: item.twitter is defined + when: item.mastodon is defined with_items: "{{ hugo_sites|default([]) }}" diff --git a/roles/hugo/templates/monitor_ring_nlnog_net_posts.service.j2 b/roles/hugo/templates/monitor_ring_nlnog_net_posts.service.j2 index cf5f60cfd..fdeee68fc 100644 --- a/roles/hugo/templates/monitor_ring_nlnog_net_posts.service.j2 +++ b/roles/hugo/templates/monitor_ring_nlnog_net_posts.service.j2 @@ -3,7 +3,7 @@ Description=Hugo post monitor [Service] Type=simple -ExecStart=/usr/local/sbin/monitor_hugo_posts /var/www/ring.nlnog.net/content/post "/usr/local/bin/tweet_hugo_post tweet" +ExecStart=/usr/local/sbin/monitor_hugo_posts /var/www/ring.nlnog.net/content/post "/usr/local/bin/toot_hugo_post toot" [Install] WantedBy=multi-user.target diff --git a/roles/hugo/templates/monitor_webdev_ring_nlnog_net_posts.service.j2 b/roles/hugo/templates/monitor_webdev_ring_nlnog_net_posts.service.j2 index e0c2b43da..4db562c66 100644 --- a/roles/hugo/templates/monitor_webdev_ring_nlnog_net_posts.service.j2 +++ b/roles/hugo/templates/monitor_webdev_ring_nlnog_net_posts.service.j2 @@ -3,7 +3,7 @@ Description=Hugo post monitor [Service] Type=simple -ExecStart=/usr/local/sbin/monitor_hugo_posts /var/www/webdev.ring.nlnog.net/content/post "/usr/local/bin/tweet_hugo_post debug" +ExecStart=/usr/local/sbin/monitor_hugo_posts /var/www/webdev.ring.nlnog.net/content/post "/usr/local/bin/toot_hugo_post debug" [Install] WantedBy=multi-user.target diff --git a/roles/hugo/templates/tweet_hugo_post.j2 b/roles/hugo/templates/toot_hugo_post.j2 similarity index 51% rename from roles/hugo/templates/tweet_hugo_post.j2 rename to roles/hugo/templates/toot_hugo_post.j2 index 149663046..7c6462c48 100755 --- a/roles/hugo/templates/tweet_hugo_post.j2 +++ b/roles/hugo/templates/toot_hugo_post.j2 @@ -1,28 +1,36 @@ #!/usr/bin/env python3 # -# tweet_hugo_post - Read a hugo post and send a tweet about it +# toot_hugo_post - Read a hugo post and send a toot about it # import datetime import re import sys -import tweepy +from mastodon import Mastodon # HUGO settings WEBROOT = "/var/www" HUGOCONF = "config.toml" -# Twitter settings -ACCOUNT_NAME = "NLNOG" +# Mastodon settings +ACCOUNT_NAME = "nlnogring" +ACCOUNT_EMAIL = "ring-admins@nlnog.net" +ACCOUNT_PASSWORD = "{{ mastodon_password }}" TIMELINE_LOOKBACK=100 -CALLBACK_URL = "https://ring.nlnog.net/" -CONSUMER_KEY = "{{ twitter_consumer_key}}" -CONSUMER_SECRET = "{{ twitter_consumer_secret }}" -ACCESS_TOKEN = "{{ twitter_access_token }}" -ACCESS_TOKEN_SECRET = "{{ twitter_access_token_secret }}" +MSTDN_CLIENT_ID="{{ mastodon_client_id }}" +MSTDN_SECRET="{{ mastodon_secret }}" +MSTDN_SERVER = "https://mastodon.nl" +# Register your app! This only needs to be done once (per server, or when +# distributing rather than hosting an application, most likely per device and server). +# Uncomment the code and substitute in your information: +# Mastodon.create_app( +# ACCOUNT_NAME, +# api_base_url = MSTDN_SERVER, +# to_file = 'mastodon.secret' +# ) # Misc settings -DEBUGFILE = "/tmp/tweet_hugo_posts.log" +DEBUGFILE = "/tmp/toot_hugo_posts.log" # Find the base url for a hugo installation def h_base_url(webdir): @@ -63,36 +71,38 @@ def h_parse_post(pathname): pass return post -# Login on twitter -def t_login(): - auth = tweepy.OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET) - auth.set_access_token(ACCESS_TOKEN,ACCESS_TOKEN_SECRET) - api = tweepy.API(auth) +# Login on Mastodon +def m_login(): try: - api.verify_credentials() + mastodon = Mastodon(client_id=MSTDN_CLIENT_ID, + client_secret=MSTDN_SECRET, + api_base_url=MSTDN_SERVER, + user_agent=ACCOUNT_NAME) + mastodon.log_in(ACCOUNT_EMAIL,ACCOUNT_PASSWORD) except: return False - return api + return mastodon -# Check if a tweet containing a string has already been posted recently -def t_duplicate_tweet(api,text): - timeline = api.user_timeline(screen_name=ACCOUNT_NAME,count=TIMELINE_LOOKBACK) +# Check if a toot containing a string has already been posted recently +def m_duplicate_toot(api,text): + me = api.me() + timeline = api.account_statuses(id=me['id'],limit=TIMELINE_LOOKBACK) if not timeline: return True - for tweet in timeline: - if re.search(re.escape(text),tweet.text): + for toot in timeline: + if re.search(re.escape(text),toot['content']): return True return False -def t_send_tweet(api,text): +def m_send_toot(api,text): try: - print("Tweeting: {"+text+"}") - api.update_status(text) + print("Tooting: {"+text+"}") + api.toot(text) except: return False return True -def f_write_tweet(text): +def f_write_toot(text): try: f = open(DEBUGFILE,'a') print(datetime.datetime.now().isoformat()+": {"+text+"}",file=f) @@ -105,7 +115,7 @@ if __name__ == "__main__": # Parse commandline if len(sys.argv) < 3: - sys.stderr.write("%s [tweet|debug] \n" % (sys.argv[0])) + sys.stderr.write("%s [toot|debug] \n" % (sys.argv[0])) sys.exit(1) action = sys.argv[1] pathname = sys.argv[2] @@ -119,26 +129,26 @@ if __name__ == "__main__": sys.stderr.write("Unable to derive post url.\n") sys.exit(2) - # Send to Twitter - api = t_login() + # Send to Mastodon + api = m_login() if not api: - sys.stderr.write("Unable to connect to Twitter.\n") + sys.stderr.write("Unable to connect to Mastodon.\n") sys.exit(2) - if t_duplicate_tweet(api,post['title']): - sys.stderr.write("Duplicate tweet or check failed; not posting.\n") + if m_duplicate_toot(api,post['title']): + sys.stderr.write("Duplicate toot or check failed; not posting.\n") sys.exit(2) - tweet_text = "New post: " + post['title'] + " " + post['url'] - if action == 'tweet': - if t_send_tweet(api,tweet_text): - print("Tweet sent.") + toot_text = "New post: " + post['title'] + " " + post['url'] + if action == 'toot': + if m_send_toot(api,toot_text): + print("Toot sent.") else: - sys.stderr.write("Unable to send tweet.\n") + sys.stderr.write("Unable to send toot.\n") sys.exit(2) elif action == 'debug': - if f_write_tweet(tweet_text): - print("Tweet written to file.") + if f_write_toot(toot_text): + print("Toot written to file.") else: - sys.stderr.write("Unable to write tweet to file.\n") + sys.stderr.write("Unable to write toot to file.\n") sys.exit(2) else: sys.stderr.write("Unknown action %s.\n" % (action)) diff --git a/vault/public04.infra.ring.nlnog.net.yml b/vault/public04.infra.ring.nlnog.net.yml index 1e573ce12..29fcdcc91 100644 --- a/vault/public04.infra.ring.nlnog.net.yml +++ b/vault/public04.infra.ring.nlnog.net.yml @@ -1,23 +1,18 @@ $ANSIBLE_VAULT;1.1;AES256 -66323366393738316236616265316164303431633065646432633961663235303963646339633266 -3132613039303830363363663737653530316163626631320a356332363964386239616237643536 -38653763616437643930316639623437346433376232386233313731613630316137326334333033 -3832343630623861340a636437346162636332623163353732323438666637306132333133363461 -32353864313539373331383662613939376261313433386637323736643565643334336437346136 -39653238633138393233316266646230613533643330636464616133353037653730653064346533 -34363964336135613364333466613731313930613264353530363432363236333765396636626165 -38343163303437363232636266623833316666313635393836643731393032663530396434633461 -36373630336661626537653234636338333236386633643161313566313635616233373438353361 -37646137303830396630633936326362613062393934306230313166393133306638346664353331 -39303637626431656163303962623435653664666435643165636265303764313266343439326637 -66356536636337373534613466313335353730353335366266356164393366656438336639613739 -62346432333234623933643464306364373266386631383764383531393361356162653339303038 -62636365613334376164633762626235346139346365313665396536373866333466386264353565 -63643962386261383034373332393834343433343037316336663734303237363031663033653933 -64623366636265383835333965383239633333383164366237353537666635306333663436643633 -63666131373733636533376132626263323362376538393434346261383261626364303036313430 -37303034383235333466633933636130376538333536333261613365363236366165393730323161 -35623964343265643232323233373931333364383539626539306439626637376630353234343736 -37373135656236363561303334303938316536656639383161666462653463303638626265323933 -33633539356663306562386466626137343336663361353064363062316461376131363465396134 -3163386237393634653038613561373138363734623830636261 +64366232373766313664643632323664666365356163613166356232616161373264336566333463 +6332336336313838303738633137353633373337643362360a326434366236633735616636343430 +36303165666633326566613031646461393739323430366439386162386538616638343837326533 +6534636262356636610a346331616332396237333961326334653366343733303164666531626536 +32396337663330656234346562356335303335323963643635373033626230666135343662643034 +33663062346230333562333431613436336139373938386535316663363030623164366138313465 +34396363343136323863343334306135333966313661653961643462646164326531306361653239 +31356666336132643466636136656266616562303830663535616266353330656463613930333737 +65646139653232363164303538326336653463623564306465373935323738343835323231383133 +66613431343234633139376130386430613463383534626535376238663836393332323432626634 +63376164346431343632343966653438376266663536343530616234633338346232343339643334 +33623035646438626161313432363734343536653363343861393264396530666132633836386235 +63633734646635633936336331636563393730396330326161383865376639313338316530623134 +34366136316331326337353862393531653439333161313166373461653563653130623033643562 +66376564613164323365616438396363376639376562653462653030663439346230363763316638 +61376134313133333933383763336239633166623136373961646233333231306430396534333638 +35663538326437303861333235396234626432393534333536353538366137636133