Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google translate #1978

Merged
merged 12 commits into from
Apr 26, 2017
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ gem 'hypdf', '~> 1.0.10' # PDFInfoAgent
# FIXME needs to loosen omniauth dependency
gem 'weibo_2', github: 'dsander/weibo_2', branch: 'master'

# GoogleCalendarPublishAgent
gem "google-api-client", require: 'google/api_client'
Copy link
Member

@cantino cantino Apr 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add back in the require: 'google/api_client' so that the GoogleCalendarPublishAgent will also work ok? I think you should also copy the gem_dependency_check and #{'## Include "google-api-client" in your Gemfile to use this Agent!' if dependencies_missing?} lines from GoogleCalendarPublishAgent into your Agent so that it correctly detects if google-api-client has been removed from the Gemfile, and remove the require line that you added at the top.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and done!

# GoogleCalendarPublishAgent and GoogleTranslateAgent
gem 'google-api-client', '~> 0.7.1', require: 'google/api_client'

# Twitter Agents
gem 'twitter', '~> 5.14.0' # Must to be loaded before cantino-twitter-stream.
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ DEPENDENCIES
foreman (~> 0.63.0)
geokit (~> 1.8.4)
geokit-rails (~> 2.2.0)
google-api-client
google-api-client (~> 0.7.1)
guard (~> 2.13.0)
guard-livereload (~> 2.5.1)
guard-rspec (~> 4.6.4)
Expand Down
92 changes: 92 additions & 0 deletions app/models/agents/google_translation_agent.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
module Agents
class GoogleTranslationAgent < Agent
cannot_be_scheduled!

gem_dependency_check { defined?(Google) && defined?(Google::APIClient) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defined?(Google) && is redundant here. defined?(Google::APIClient) won't err if Google is undefined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good to know, thanks!


description <<-MD
The Translation Agent will attempt to translate text between natural languages.

#{'## Include `google-api-client` in your Gemfile to use this Agent!' if dependencies_missing?}

Services are provided using Google Translate. You can [sign up](https://cloud.google.com/translate/) to get `google_api_key` which is required to use this agent.
The service is **not free**.

`to` must be filled with a [translator language code](https://cloud.google.com/translate/docs/languages).

`from` is the language translated from. If it's not specified, the API will attempt to detect the source language automatically and return it within the response.

Specify what you would like to translate in `content` field, you can use [Liquid](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) specify which part of the payload you want to translate.

`expected_receive_period_in_days` is the maximum number of days you would allow to pass between events.
MD

event_description "User defined"

def default_options
{
'to' => "sv",
'from' => 'en',
'google_api_key' => '',
'expected_receive_period_in_days' => 1,
'content' => {
'text' => "{{message}}",
'moretext' => "{{another message}}"
}
}
end

def working?
last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
end

def validate_options
unless options['google_api_key'].present? && options['to'].present? && options['content'].present? && options['expected_receive_period_in_days'].present?
errors.add :base, "google_api_key, to, content and expected_receive_period_in_days are all required"
end
end

def translate_from
interpolated["from"].presence || 'en'
end

def receive(incoming_events)
incoming_events.each do |event|
translated_event = {}
opts = interpolated(event)
opts['content'].each_pair do |key, value|
result = translate(value)
translated_event[key] = result.data.translations.last.translated_text
end
create_event payload: translated_event
end
end

def google_client
@google_client ||= Google::APIClient.new(
{
application_name: "Huginn",
application_version: "0.0.1",
key: options['google_api_key'],
authorization: nil
}
)
end

def translate_service
@translate_service ||= google_client.discovered_api('translate','v2')
end

def translate(value)
google_client.execute(
api_method: translate_service.translations.list,
parameters: {
format: 'text',
source: translate_from,
target: options["to"],
q: value
}
)
end
end
end
79 changes: 0 additions & 79 deletions app/models/agents/translation_agent.rb

This file was deleted.