Skip to content

Commit

Permalink
Merge pull request #32 from ukparliament/update-other-module-naming-etc
Browse files Browse the repository at this point in the history
Update module naming
  • Loading branch information
jamesjefferies authored Dec 20, 2024
2 parents 4eab37a + d68c07f commit 119c9ce
Show file tree
Hide file tree
Showing 41 changed files with 227 additions and 220 deletions.
4 changes: 2 additions & 2 deletions app/controllers/task_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
require 'import/members'
require 'import/questions'
require 'cleanup'
require 'tweet/tweet'
require 'tweet'

# We include code from modules.
include Import::Members
include Import::Questions
include Cleanup
include TWEET
include Tweet

class TaskController < ApplicationController

Expand Down
83 changes: 0 additions & 83 deletions lib/post/bluesky/written-statements.rb

This file was deleted.

87 changes: 87 additions & 0 deletions lib/post/bluesky/written_statements.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'net/http'
require 'json'
require 'uri'

module Post
module Bluesky
module WrittenStatements

def post_bluesky

# We up the authentication tokens.
bluesky_handle = ENV['WRITTEN_STATEMENTS_BLUESKY_HANDLE']
bluesky_app_password = ENV['WRITTEN_STATEMENTS_BLUESKY_APP_PASSWORD']

# We get all the written statements that have not yet been posted to Bluesky.
written_statements = WrittenStatement.find_by_sql(
"
SELECT ws.*, m.display_name AS member_name, ab.name AS answering_body_name
FROM written_statements ws, members m, answering_bodies ab
WHERE ws.posted_to_bluesky IS FALSE
AND ws.member_id = m.id
AND ws.answering_body_id = ab.id
"
)

# We report the number of written statements to be posted.
puts "Posting #{written_statements.size} written statements"

# For each written statement ...
written_statements.each do |written_statement|

# ... we attempt to authenticate.
uri = URI( 'https://bsky.social/xrpc/com.atproto.server.createSession' )
body = { "identifier": bluesky_handle, "password": bluesky_app_password }
headers = { 'Content-Type': 'application/json' }
response = Net::HTTP.post( uri, body.to_json, headers )

# We get the post text.
post_text = written_statement.bluesky_post_text

# We grab the access tokens from the JSON response.
access_jwt = JSON.parse( response.body )['accessJwt']
did = JSON.parse( response.body )['did']

# We construct the link facets.
facets = create_facets( post_text )

# We construct the post.
post = {
"$type": "app.bsky.feed.post",
"text": post_text,
"createdAt": Time.now.iso8601,
"facets": facets,
}

# We construct the body.
body = {
"repo": did,
"collection": "app.bsky.feed.post",
"record": post,
}

# We convert the body to JSON.
body = body.to_json

# We attempt to post.
uri = URI( 'https://bsky.social/xrpc/com.atproto.repo.createRecord' )
headers = { 'Content-Type': 'application/json', 'Authorization': "Bearer #{access_jwt}" }
response = Net::HTTP.post( uri, body, headers )

# If the request is successful ...
if response.code == '200'

# ... we mark the written statement as posted to Bluesky.
written_statement.posted_to_bluesky = true
written_statement.save!
else
puts post_text
end

# We pause for two seconds.
sleep( 2 )
end
end
end
end
end
64 changes: 0 additions & 64 deletions lib/post/mastodon/written-statements.rb

This file was deleted.

67 changes: 67 additions & 0 deletions lib/post/mastodon/written_statements.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'net/http'
require 'json'
require 'uri'

module Post
module Mastodon
module WrittenStatements
def post_mastodon

# We up the authentication token.
bearer_token = ENV['WRITTEN_STATEMENTS_BEARER']

# We get all the written statements that have not yet been posted to Mastodon.
written_statements = WrittenStatement.find_by_sql(
"
SELECT ws.*, m.display_name AS member_name, ab.name AS answering_body_name
FROM written_statements ws, members m, answering_bodies ab
WHERE ws.posted_to_mastodon IS FALSE
AND ws.member_id = m.id
AND ws.answering_body_id = ab.id
"
)

# We report the number of written statements to be posted.
puts "Posting #{written_statements.size} written statements"

# For each written statement ...
written_statements.each do |written_statement|

# ... we encode the post text.
post_text = written_statement.mastodon_post_text
parser = URI::Parser.new
post_text = parser.escape( post_text )

# ... we construct the uri ...
uri = URI( "https://mastodon.me.uk/api/v1/statuses?status=#{post_text}" )

# ... create the client ...
http = Net::HTTP.new( uri.host, uri.port )
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

# ... create the request ...
req = Net::HTTP::Post.new( uri )

# ... add headers ...
req.add_field "Authorization", "Bearer #{bearer_token}"

# ... and fetch the request.
response = http.request(req)

# If the request is successful ...
if response.code == '200'

# ... we mark the written statement as posted to Mastodon.
written_statement.posted_to_mastodon = true
written_statement.save!
end

# We pause for two seconds.
sleep( 2 )
end
end
end
end
end

4 changes: 2 additions & 2 deletions lib/tasks/post-bluesky.rake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# We require the tweet code.
require 'post/bluesky/written-statements'
require 'post/bluesky/written_statements'

# We include tweet code from module.
include POST
include Post::Bluesky::WrittenStatements

task :post_bluesky_written_statements => :environment do
puts "posting written statements to bluesky"
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/post-mastodon.rake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# We require the tweet code.
require 'post/mastodon/written-statements'
require 'post/mastodon/written_statements'

# We include tweet code from module.
include POST
include Post::Mastodon::WrittenStatements

task :post_mastodon_written_statements => :environment do
puts "posting written statements to mastodon"
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/tweet-ag.rake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# We require the tweet code.
require 'tweet/tweet'
require 'tweet'

# We include tweet code from module.
include TWEET
include Tweet

task :tweet_ag => :environment do
puts "tweeting new answers from AG"
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/tweet-cc.rake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# We require the tweet code.
require 'tweet/tweet'
require 'tweet'

# We include tweet code from module.
include TWEET
include Tweet

task :tweet_cc => :environment do
puts "tweeting new answers from CC"
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/tweet-co.rake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# We require the tweet code.
require 'tweet/tweet'
require 'tweet'

# We include tweet code from module.
include TWEET
include Tweet

task :tweet_co => :environment do
puts "tweeting new answers from CO"
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/tweet-dbt.rake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# We require the tweet code.
require 'tweet/tweet'
require 'tweet'

# We include tweet code from module.
include TWEET
include Tweet

task :tweet_dbt => :environment do
puts "tweeting new answers from DBT"
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/tweet-dcms.rake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# We require the tweet code.
require 'tweet/tweet'
require 'tweet'

# We include tweet code from module.
include TWEET
include Tweet

task :tweet_dcms => :environment do
puts "tweeting new answers from DCMS"
Expand Down
Loading

0 comments on commit 119c9ce

Please sign in to comment.