CC-5709: Airtime Analyzer

* File import via the Add Media page now works!
* Reworked StoredFile::copyFileToStor() because it's still needed.
* Reworked the way file paths are reported in airtime_analyzer
  and in the RESTful media API
This commit is contained in:
Albert Santoni 2014-03-17 10:19:39 -04:00
parent 6a68967f81
commit 50a42f12bb
5 changed files with 144 additions and 54 deletions

View file

@ -1,7 +1,7 @@
import logging
import multiprocessing
import shutil
import os
import os, errno
from metadata_analyzer import MetadataAnalyzer
class AnalyzerPipeline:
@ -12,28 +12,59 @@ class AnalyzerPipeline:
# Take message dictionary and perform the necessary analysis.
@staticmethod
def run_analysis(queue, audio_file_path, final_directory):
def run_analysis(queue, audio_file_path, import_directory, original_filename):
if not isinstance(queue, multiprocessing.queues.Queue):
raise TypeError("queue must be a multiprocessing.Queue()")
if not isinstance(audio_file_path, unicode):
raise TypeError("audio_file_path must be unicode. Was of type " + type(audio_file_path).__name__ + " instead.")
if not isinstance(final_directory, unicode):
raise TypeError("final_directory must be unicode. Was of type " + type(final_directory).__name__ + " instead.")
if not isinstance(import_directory, unicode):
raise TypeError("import_directory must be unicode. Was of type " + type(import_directory).__name__ + " instead.")
if not isinstance(original_filename, unicode):
raise TypeError("original_filename must be unicode. Was of type " + type(original_filename).__name__ + " instead.")
#print ReplayGainAnalyzer.analyze("foo.mp3")
# Analyze the audio file we were told to analyze:
# First, we extract the ID3 tags and other metadata:
queue.put(MetadataAnalyzer.analyze(audio_file_path))
results = MetadataAnalyzer.analyze(audio_file_path)
# Note that the queue we're putting the results into is our interprocess communication
# back to the main process.
#print ReplayGainAnalyzer.analyze("foo.mp3")
#Import the file over to it's final location.
final_file_path = import_directory
if results.has_key("artist_name"):
final_file_path += "/" + results["artist_name"]
if results.has_key("album"):
final_file_path += "/" + results["album"]
final_file_path += "/" + original_filename
final_audio_file_path = final_directory + os.sep + os.path.basename(audio_file_path)
if os.path.exists(final_audio_file_path) and not os.path.samefile(audio_file_path, final_audio_file_path):
os.remove(final_audio_file_path)
#Ensure any redundant slashes are stripped
final_file_path = os.path.normpath(final_file_path)
shutil.move(audio_file_path, final_audio_file_path)
#final_audio_file_path = final_directory + os.sep + os.path.basename(audio_file_path)
if os.path.exists(final_file_path) and not os.path.samefile(audio_file_path, final_file_path):
raise Exception("File exists and will not be overwritten.") # by design
#Overwriting a file would mean Airtime's database has the wrong information...
#Ensure the full path to the file exists
mkdir_p(os.path.dirname(final_file_path))
#Move the file into its final destination directory
shutil.move(audio_file_path, final_file_path)
#Pass the full path back to Airtime
results["full_path"] = final_file_path
queue.put(results)
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else: raise

View file

@ -83,11 +83,13 @@ class MessageListener:
try:
msg_dict = json.loads(body)
audio_file_path = msg_dict["tmp_file_path"]
final_directory = msg_dict["final_directory"]
#final_file_path = msg_dict["final_file_path"]
import_directory = msg_dict["import_directory"]
original_filename = msg_dict["original_filename"]
callback_url = msg_dict["callback_url"]
api_key = msg_dict["api_key"]
audio_metadata = MessageListener.spawn_analyzer_process(audio_file_path, final_directory)
audio_metadata = MessageListener.spawn_analyzer_process(audio_file_path, import_directory, original_filename)
StatusReporter.report_success_to_callback_url(callback_url, api_key, audio_metadata)
except KeyError as e:
@ -123,11 +125,11 @@ class MessageListener:
channel.basic_ack(delivery_tag=method_frame.delivery_tag)
@staticmethod
def spawn_analyzer_process(audio_file_path, final_directory):
def spawn_analyzer_process(audio_file_path, import_directory, original_filename):
q = multiprocessing.Queue()
p = multiprocessing.Process(target=AnalyzerPipeline.run_analysis,
args=(q, audio_file_path, final_directory))
args=(q, audio_file_path, import_directory, original_filename))
p.start()
p.join()
if p.exitcode == 0: