|
require 'twitter' |
|
require 'google_translate' |
|
require 'colorize' |
|
|
|
SEPARATOR = "@------------------------@" |
|
URI_REGEX = %r"((?:(?:[^ :/?#]+):)(?://(?:[^ /?#]*))(?:[^ ?#]*)(?:\?(?:[^ #]*))?(?:#(?:[^ ]*))?)" |
|
# http://stackoverflow.com/questions/6368659/removing-url-from-text-using-ruby |
|
|
|
SKIP = 'n' |
|
|
|
# get these values by registering at https://apps.twitter.com/ |
|
CONSUMER_KEY = "" |
|
CONSUMER_SECRET = "" |
|
ACCESS_TOKEN = "" |
|
ACCESS_SECRET = "" |
|
|
|
NO_SAY = false |
|
TO_LANG = :en |
|
FROM_LANG = :de |
|
|
|
def print_separator |
|
puts SEPARATOR.white |
|
end |
|
|
|
def strip_links(text) |
|
text.gsub(URI_REGEX, '') |
|
end |
|
|
|
def create_twitter_client |
|
Twitter::REST::Client.new do |config| |
|
config.consumer_key = CONSUMER_KEY |
|
config.consumer_secret = CONSUMER_SECRET |
|
config.access_token = ACCESS_TOKEN |
|
config.access_token_secret = ACCESS_SECRET |
|
end |
|
end |
|
|
|
def use_tweet(tweet_text) |
|
|
|
print_separator |
|
|
|
tweet_text = strip_links(tweet_text) |
|
|
|
translator = GoogleTranslate.new |
|
result = translator.translate(FROM_LANG, TO_LANG, tweet_text) |
|
|
|
translation = result[0][0][0] |
|
|
|
puts "Tweet Text (#{FROM_LANG}): #{tweet_text}".magenta |
|
|
|
translator.say(FROM_LANG, tweet_text) unless NO_SAY |
|
|
|
puts "What is your translation of the above tweet? ('n' to skip)" |
|
your_translation = gets.chomp |
|
|
|
if your_translation.eql? SKIP |
|
puts "Skipping tweet" |
|
return |
|
end |
|
|
|
puts |
|
puts "Google Translation: #{translation}".light_green |
|
puts "Your translation: #{your_translation}".cyan |
|
end |
|
|
|
client = create_twitter_client |
|
|
|
# Set search_text to your desired search terms. The '-rt' means filter out retweets. |
|
search_text = "tech -rt" |
|
|
|
puts "Welcome to Twitter Translation Practice." |
|
puts "Translating tweets with the search '#{search_text}' from #{FROM_LANG} to #{TO_LANG}" |
|
puts |
|
|
|
while true |
|
tweet_text = client.search(search_text, :lang => FROM_LANG.to_s).each do |tweet| |
|
use_tweet(tweet.text) |
|
end |
|
end |