Skip to content

Commit

Permalink
Upload mp3 to S3
Browse files Browse the repository at this point in the history
  • Loading branch information
gesteves committed Dec 1, 2016
1 parent b6b23d6 commit 5a08d80
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 44 deletions.
1 change: 1 addition & 0 deletions .env.example
Expand Up @@ -10,3 +10,4 @@ SPEAK_REGEX=(sn|m)arkov speak
POLLY_VOICE='Brian'
AWS_ACCESS_KEY=
AWS_SECRET_KEY=
S3_BUCKET=
1 change: 0 additions & 1 deletion Gemfile
Expand Up @@ -5,7 +5,6 @@ gem 'sinatra'
gem 'thin'
gem 'redis'
gem 'httparty'
gem 'rest-client'
gem 'dotenv'
gem 'foreman'
gem 'rake'
Expand Down
16 changes: 0 additions & 16 deletions Gemfile.lock
Expand Up @@ -11,31 +11,19 @@ GEM
aws-sigv4 (1.0.0)
daemons (1.2.4)
dalli (2.7.6)
domain_name (0.5.20161129)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.1.1)
eventmachine (1.2.1)
foreman (0.82.0)
thor (~> 0.19.1)
http-cookie (1.0.3)
domain_name (~> 0.5)
httparty (0.14.0)
multi_xml (>= 0.5.2)
jmespath (1.3.1)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
multi_xml (0.5.5)
netrc (0.11.0)
rack (1.6.5)
rack-protection (1.5.3)
rack
rake (11.3.0)
redis (3.3.2)
rest-client (2.0.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
sinatra (1.4.7)
rack (~> 1.5)
rack-protection (~> 1.4)
Expand All @@ -46,9 +34,6 @@ GEM
rack (>= 1, < 3)
thor (0.19.1)
tilt (2.0.5)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.2)

PLATFORMS
ruby
Expand All @@ -61,7 +46,6 @@ DEPENDENCIES
httparty
rake
redis
rest-client
sinatra
thin

Expand Down
48 changes: 21 additions & 27 deletions app.rb
Expand Up @@ -8,7 +8,6 @@
require 'dalli'
require 'aws-sdk'
require 'tempfile'
require 'rest-client'

configure do
# Load .env vars
Expand Down Expand Up @@ -81,7 +80,7 @@
if is_mute_command?(params)
response = mute_bot(params[:text])
elsif is_speak_command?(params)
speak_markov(params[:channel_id])
response = audio_markov
else
store_message(params[:text]) if should_store_message?(params)
response = build_markov if should_reply?(params)
Expand Down Expand Up @@ -240,21 +239,39 @@ def markov_topic(channel_id)
end
end

def speak_markov(channel_id)
def audio_markov
text = ''
min_length = ENV['MIN_LENGTH'] || 5
while text.split(' ').size < min_length
text = build_markov
end
url = generate_mp3_url(text)
upload_file(url, text, channel_id)
file = download_file(url)
s3_url = upload_to_s3(file)
"<#{s3_url}|#{text}>"
end

def generate_mp3_url(text)
signer = Aws::Polly::Presigner.new(credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY'], ENV['AWS_SECRET_KEY']), region: 'us-east-1')
signer.synthesize_speech_presigned_url(output_format: 'mp3', text: text, voice_id: ENV['POLLY_VOICE'])
end

def download_file(url)
tmp = Tempfile.new([Time.now.to_i.to_s, '.mp3'])
tmp.binmode
tmp.write(HTTParty.get(url).body)
tmp.flush
File.new(tmp)
end

def upload_to_s3(file)
client = Aws::S3::Client.new(access_key_id: ENV['AWS_ACCESS_KEY'], secret_access_key: ENV['AWS_SECRET_KEY'], region: 'us-east-1')
s3 = Aws::S3::Resource.new(client: client)
obj = s3.bucket(ENV['S3_BUCKET']).object(File.basename(file))
obj.upload_file(file.path, { acl: 'public-read' })
obj.public_url
end

def json_response_for_slack(reply)
response = { text: reply, link_names: 1 }
response[:username] = ENV['BOT_USERNAME'] unless ENV['BOT_USERNAME'].nil?
Expand Down Expand Up @@ -428,26 +445,3 @@ def set_topic(channel_id, topic)
puts "[ERROR] Error setting channel topic: #{response['error']}"
end
end

def upload_file(url, title, channel_id)
tmp = Tempfile.new([Time.now.to_i.to_s, '.mp3'])
tmp.binmode
tmp.write(HTTParty.get(url).body)
tmp.flush

opts = {
token: ENV['API_TOKEN'],
title: title,
file: File.new(tmp),
filetype: 'mp3',
filename: "#{title}.mp3",
channels: channel_id,
}
request = RestClient.post('https://slack.com/api/files.upload', opts)
response = JSON.parse(request.body)
if response['ok']
puts "[LOG] File uploaded with title \"#{title}\""
else
puts "[ERROR] Error uploading file: #{response['error']}"
end
end

0 comments on commit 5a08d80

Please sign in to comment.