Skip to content

Commit

Permalink
Add logged in 'ping', more work on harvester, added media item json i…
Browse files Browse the repository at this point in the history
…nfo.

modified:   app/controllers/api/sessions_controller.rb
-- added security ping action

modified:   app/controllers/media_controller.rb
-- added info response that includes links to audio and spectrogram

modified:   config/routes.rb
-- added route for /security/ping

modified:   config/settings.rb
 - still not sure how to share this between harvester and Rails

modified:   db/seeds.rb
modified:   lib/external/audio_harvester.rb
-- added methods to get information for moving a file from audio recording create response
  • Loading branch information
unknown authored and unknown committed Nov 29, 2012
1 parent b0ad8fe commit 31f6c18
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 46 deletions.
9 changes: 9 additions & 0 deletions app/controllers/api/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ def create

end

# returns 401 or 200 depending on if a user is signed in or not
def ping
if user_signed_in?
head :ok
else
head :unauthorized
end
end

=begin
before_filter :authenticate_user!, :except => [:create, :destroy]
before_filter :ensure_params_exist
Expand Down
46 changes: 38 additions & 8 deletions app/controllers/media_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def item
@file_info = params
@file_info.delete 'controller'
@file_info.delete 'action'
@file_info.delete 'auth_token'
#@file_info.delete 'format'

# decide on the format requested. There are at least two ways to get the request format:
Expand Down Expand Up @@ -71,16 +72,45 @@ def item
download_file full_path, final_format_requested
end

if text_media_types.include? final_format_requested
respond_to do |format|
format.xml {render :xml => @file_info}
format.json {render :json => @file_info}
end
# for any other extension (or no extension)
# respond with file info in requested format

file_info_to_send = {
:start_offset => @file_info[:start_offset],
:end_offset => @file_info[:end_offset],
:date => @file_info[:date],
:time => @file_info[:time],
:id => @file_info[:id],
:channel => @file_info[:channel],
:sample_rate => @file_info[:sample_rate],
:window => @file_info[:window],
:colour => @file_info[:colour],
:original_format => @file_info[:original_format],
:format => final_format_requested,
:info_url => "/media/#{@file_info[:id]}"
}

unless file_info_to_send[:start_offset].nil?
base_audio_segment_url = "/media/#{file_info_to_send[:id]}_#{file_info_to_send[:start_offset]}_#{file_info_to_send[:end_offset]}_"+
"#{file_info_to_send[:channel]}_#{file_info_to_send[:sample_rate]}."

file_info_to_send[:audio_base_url] = base_audio_segment_url
end

unless file_info_to_send[:window].nil?
file_info_to_send[:spectrogram_url] =
"/media/#{file_info_to_send[:id]}_#{file_info_to_send[:start_offset]}_#{file_info_to_send[:end_offset]}_"+
"#{file_info_to_send[:channel]}_#{file_info_to_send[:sample_rate]}_"+
"#{file_info_to_send[:window]}_#{file_info_to_send[:colour]}.png"

file_info_to_send[:color_description] = Spectrogram.colour_options

end

unless all_media_types.include? final_format_requested
# respond with a bad request
head :bad_request
respond_to do |format|
format.any(:xml, :html) {render :xml => file_info_to_send}
format.json {render :json => file_info_to_send}
format.all { head :bad_request }
end

end
Expand Down
131 changes: 93 additions & 38 deletions lib/external/audio_harvester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
require 'logger'
require 'trollop'

module BawSite
class Application

end
end

#=begin
require './lib/modules/OS'
require './lib/modules/audio_sox'
Expand All @@ -16,6 +22,7 @@
require './lib/modules/audio'
require './lib/modules/cache'
require './lib/exceptions'
require './config/settings'
#=end

=begin
Expand All @@ -29,8 +36,10 @@
require '../modules/audio'
require '../modules/cache'
require '../exceptions'
require '../../config/settings'
=end

# to cater for configurations


module AudioHarvester
Expand Down Expand Up @@ -89,7 +98,7 @@ def initialize(host, port, config_file_name, login_email, login_password, endpoi
def directory_list(full_base_directory)
path = File.join(full_base_directory, '*/')
dir_path = Dir[path]
logger.debug('directory_list') { "Directoryies found: #{dir_path}" }
logger.debug('directory_list') { "Directories found: #{dir_path}" }
dir_path
end

Expand Down Expand Up @@ -121,8 +130,8 @@ def file_info(full_file_path)
# calculate the audio recording start date and time
def recording_start_datetime(full_path, utc_offset)
if File.exists? full_path
access_time = File.atime full_path
change_time = File.ctime full_path
#access_time = File.atime full_path
#change_time = File.ctime full_path
modified_time = File.mtime full_path

file_name = File.basename full_path
Expand All @@ -133,9 +142,10 @@ def recording_start_datetime(full_path, utc_offset)
file_name.scan(/.*_(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})\..+/) do |year, month, day, hour, min ,sec|
datetime_from_file = DateTime.new(year.to_i, month.to_i, day.to_i, hour.to_i, min.to_i, sec.to_i, utc_offset)
end
datetime_from_file
else
nil
end

datetime_from_file
end

# constructs the full path that a file will be moved to
Expand Down Expand Up @@ -169,13 +179,13 @@ def create_params(file_path, file_info, config_file_object)

to_send = {
:audio_recording => {
:file_hash => 'SHA256::'+generate_hash(file_path).hexdigest,
:sample_rate_hertz => file_info[:info][:ffmpeg]['STREAM sample_rate'],
:media_type => media_type,
:uploader_id => config_file_object['uploader_id'],
:site_id => config_file_object['site_id'],
:recorded_date => recording_start
}}
:file_hash => 'SHA256::'+generate_hash(file_path).hexdigest,
:sample_rate_hertz => file_info[:info][:ffmpeg]['STREAM sample_rate'],
:media_type => media_type,
:uploader_id => config_file_object['uploader_id'],
:site_id => config_file_object['site_id'],
:recorded_date => recording_start
}}

if media_type == 'audio/wavpack'
to_send[:audio_recording][:bit_rate_bps] = file_info[:info][:wavpack]['ave bitrate']
Expand All @@ -192,44 +202,71 @@ def create_params(file_path, file_info, config_file_object)
to_send
end

def construct_login_request()
# set up the login HTTP post
login_post = Net::HTTP::Post.new(@endpoint_login)
login_post.body = {:user => {:email => @login_email, :password => @login_password}}.to_json
login_post["Content-Type"] = "application/json"
logger.debug "Login request: #{login_post.inspect}, Body: #{login_post.body}"
login_post
end

def construct_create_request(parameters, auth_token)
new_params = parameters.clone
new_params[:auth_token] = auth_token

req = Net::HTTP::Post.new(@endpoint_create)
req.body = new_params.to_json
req["Content-Type"] = "application/json"
logger.debug "Create request: #{req.inspect}, Body: #{req.body}"
req
end

# get uuid for audio recording from website via REST API
# If you post to a Ruby on Rails REST API endpoint, then you'll get an
# InvalidAuthenticityToken exception unless you set a different
# content type in the request headers, since any post from a form must
# contain an authenticity token.
def create_new_audiorecording(post_params)

# set up the login HTTP post
login_post = Net::HTTP::Post.new(@endpoint_login)
login_post.body = {:user => {:email => @login_email, :password => @login_password}}.to_json
login_post["Content-Type"] = "application/json"
puts "Login request: #{login_post.inspect}, Body: #{login_post.body}"

res = Net::HTTP.start(@host, @port) do |http|
Net::HTTP.start(@host, @port) do |http|
login_post = construct_login_request
login_response = http.request(login_post)
puts "Login response: #{login_response.code}, Message: #{login_response.message}, Body: #{login_response.body}"
logger.debug "Login response: #{login_response.code}, Message: #{login_response.message}, Body: #{login_response.body}"

if login_response.code == "200" then
puts "logged in"
if login_response.code == '200'
logger.debug 'Successfully logged in.'

json_resp = JSON.parse(login_response.body)
@auth_token = json_resp['auth_token']
post_params[:auth_token] = @auth_token

req = Net::HTTP::Post.new(@endpoint_create)
req.body = post_params.to_json
req["Content-Type"] = "application/json"
puts "Create request: #{req.inspect}, Body: #{req.body}"

response = http.request(req)
puts "Create response: #{response.code}, Message: #{response.message}, Body: #{response.body}"
if response.code == "201" then
response.body
auth_token = json_resp['auth_token']

create_post = construct_create_request(post_params, auth_token)
response = http.request(create_post)
logger.debug "Create response: #{response.code}, Message: #{response.message}, Body: #{response.body}"

if response.code == '201'
response_json = JSON.parse(response.body)
Logging.logger.info 'New audio recording created.'
response
elsif response.code == '422'
'Unprocessable Entity'
else
Logging.logger.error "Create response was not recognised: code #{response.code}, Message: #{response.message}, Body: #{response.body}"
nil
end
else
Logging.logger.error "Login response was not 200 OK: #{login_response.code}, Message: #{login_response.message}, Body: #{login_response.body}"
nil
end

end
end

#
def move_file(create_response)
response_json = JSON.parse(create_response)

if response_json.include? :uuid

end
end

def run_once_dir(top_dir)
Expand Down Expand Up @@ -259,9 +296,27 @@ def run_once_file(file_path)

post_result = create_new_audiorecording(to_send)

puts post_result.inspect
#post_result
unless post_result.nil?
response_json = JSON.parse(post_result.body)
puts response_json.inspect
recorded_date = DateTime.parse(response_json['recorded_date'])

file_name_params = {
:id => response_json[:uuid],
:date => recorded_date.strftime("%Y%m%d"),
:time => recorded_date.strftime("%H%M%S"),
:original_format => File.extname(file_path)
}

file_name = Cache::original_audio_file file_name_params
source_possible_paths = Cache::possible_paths(Cache::original_audio_storage_paths,file_name)

Logging.logger.debug "Generated move paths: #{source_possible_paths.join(', ')}"

#result_of_move = move_file(post_result)
end
end

end

end
Expand Down

0 comments on commit 31f6c18

Please sign in to comment.