-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from ukparliament/update-other-module-naming-etc
Update module naming
- Loading branch information
Showing
41 changed files
with
227 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.