Skip to content

Instantly share code, notes, and snippets.

@brb3
Last active April 22, 2017 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brb3/e26bedb15b4e0ddf22645874ce5ba164 to your computer and use it in GitHub Desktop.
Save brb3/e26bedb15b4e0ddf22645874ce5ba164 to your computer and use it in GitHub Desktop.
Translation Bot
#!/usr/bin/env ruby
require 'mastodon'
require 'curb' # We'll need this for talking to Microsoft's Translator API
translator_token = '{TOKEN_FROM_TRANSLATOR_API}'
mastodon_token = '{ACCESS_TOKEN_FROM_MASTODON}'
# We're going to extend the String Class to do a bit of our dirty work
class String
# We'll use this to strip XML/HTML tags out of strips
def without_tags
self.gsub(%r{</?[^>]+?>}, '')
end
# Use regex to check for Japanese Characters
def japanese?
# Using `!!` to make sure that we end up with a bool.
# Also, \p will match Unicode properties. Here, we're looking for Japanese
# characters.
!!(self =~ /\p{Katakana}|\p{Hiragana}/)
end
end
# Gets an access token for the Translator API
def get_token(translator_token)
token_url = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"
response = Curl.post(token_url) do |response|
response.headers['Ocp-Apim-Subscription-key'] = translator_token
end
return response.body_str
end
# Connects to the API and translate the toot's content.
# If there's an issue with the connection, we return `false`
def translate_toot(toot, token)
toot_content = toot.content.without_tags
translation_url =
"http://api.microsofttranslator.com/v2/Http.svc/Translate?" +
"text=" + toot_content + "&to=en&from=ja"
begin
response = Curl.get(translation_url) do |response|
response.headers['Authorization'] = "Bearer " + token
end
rescue
return false
end
if response.status != "200 OK"
return false
end
return response.body_str.without_tags
end
# Connect to mastodon
mastodon = Mastodon::REST::Client.new(
base_url: 'https://mastodon.cloud', bearer_token: mastodon_token
)
# Grab the 25 most recent toots from the Public Timeline
toots = mastodon.public_timeline(limit:25)
# Find all of the Japanese toots and translate them
japanese_toots = []
token = get_token(translator_token)
toots.each do |toot|
toot_content = toot.content.without_tags
if toot_content.japanese?
translated = translate_toot(
toot, token
)
if translated
japanese_toots << [toot, translated]
end
end
end
# Loop through the Japanese toots and start replying
japanese_toots.each do |toot|
begin
# toot[1] is the content of the toot
translated_toot = "English Translation:\n" + toot[1]
# Make sure we don't go over the maximum toot length
if translated_toot.length <= 500
# toot[0] is the original toot we are replying to
mastodon.create_status(
translated_toot,
toot[0].id
)
puts "Tooted @ " + toot[0].account.acct
end
rescue
puts "Failed tooting!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment