From 6655128662e1bf80d22a15efa5980196623f0d02 Mon Sep 17 00:00:00 2001 From: Pascal Zumkehr Date: Sat, 8 Apr 2017 14:09:15 +0200 Subject: [PATCH] glob only valid mp3recs, create show upfront to handle multiple broadcasts of new show in one batch --- app/services/import/broadcast_mapping.rb | 4 ++-- app/services/import/recording/file/base.rb | 2 ++ app/services/import/recording/file/iso8601.rb | 2 +- app/services/import/recording/file/mp3_rec.rb | 2 +- app/services/import/recording/file/self_contained.rb | 10 ---------- test/integration/import_test.rb | 2 +- .../broadcast_mapping/builder/airtime_db_test.rb | 2 +- test/services/import/broadcast_mapping_test.rb | 6 +++--- 8 files changed, 11 insertions(+), 19 deletions(-) diff --git a/app/services/import/broadcast_mapping.rb b/app/services/import/broadcast_mapping.rb index 15d2a5b..7b22972 100644 --- a/app/services/import/broadcast_mapping.rb +++ b/app/services/import/broadcast_mapping.rb @@ -75,8 +75,8 @@ def overlaps?(recording) end def fetch_show(attrs = {}) - Show.where(name: attrs.fetch(:name)).first_or_initialize.tap do |show| - show.details ||= attrs[:details] + Show.where(name: attrs.fetch(:name)).first_or_create.tap do |show| + show.details = attrs[:details] if attrs[:details].present? end end diff --git a/app/services/import/recording/file/base.rb b/app/services/import/recording/file/base.rb index fe999a4..e02ac9a 100644 --- a/app/services/import/recording/file/base.rb +++ b/app/services/import/recording/file/base.rb @@ -7,6 +7,8 @@ module File # time and duration). class Base + DIGIT_GLOB = '[0-9]'.freeze + include Loggable class_attribute :pending_glob, :imported_glob, :lossy diff --git a/app/services/import/recording/file/iso8601.rb b/app/services/import/recording/file/iso8601.rb index b75f009..745fc99 100644 --- a/app/services/import/recording/file/iso8601.rb +++ b/app/services/import/recording/file/iso8601.rb @@ -11,7 +11,7 @@ class Iso8601 < Base IMPORTED_SUFFIX = '_imported'.freeze DATE_GLOB = '[12][019][0-9][0-9]-[0-1][0-9]-[0-3][0-9]'.freeze # yyyy-mm-dd TIME_GLOB = '[0-2][0-9][0-5][0-9][0-5][0-9]{+,-}[0-2][0-9][0-5]0'.freeze # HHMMSS+ZZZZ - DURATION_GLOB = '[0-9][0-9][0-9]'.freeze # ddd, minutes + DURATION_GLOB = DIGIT_GLOB * 3 # ddd, minutes FILENAME_GLOB = "#{DATE_GLOB}T#{TIME_GLOB}_#{DURATION_GLOB}".freeze self.pending_glob = FILENAME_GLOB + '.*' diff --git a/app/services/import/recording/file/mp3_rec.rb b/app/services/import/recording/file/mp3_rec.rb index 7c6393a..ab1e206 100644 --- a/app/services/import/recording/file/mp3_rec.rb +++ b/app/services/import/recording/file/mp3_rec.rb @@ -6,8 +6,8 @@ module File # mp3rec-05-5-20170203-090000-3600-sec-der_morgen.wav.mp3 class Mp3Rec < SelfContained - self.extension = 'mp3' self.lossy = true + self.pending_glob = "*#{DIGIT_GLOB * 8}-#{DIGIT_GLOB * 6}-#{DIGIT_GLOB * 4}-sec-*.mp3" def started_at @started_at ||= Time.zone.parse(filename_parts[1].tr('-', ' ')) diff --git a/app/services/import/recording/file/self_contained.rb b/app/services/import/recording/file/self_contained.rb index 81fc843..08cb638 100644 --- a/app/services/import/recording/file/self_contained.rb +++ b/app/services/import/recording/file/self_contained.rb @@ -3,18 +3,8 @@ module Recording module File class SelfContained < Base - class_attribute :extension - attr_reader :show_name - class << self - - def pending_glob - "*.#{extension}" - end - - end - def sequel?(other) other && show_name == other.show_name && diff --git a/test/integration/import_test.rb b/test/integration/import_test.rb index c1e3386..0d296e8 100644 --- a/test/integration/import_test.rb +++ b/test/integration/import_test.rb @@ -28,7 +28,7 @@ class ImportTest < ActiveSupport::TestCase .expects(:notify_exception) .with(Import::Recording::TooShortError.new(Import::Recording::File.new(@f5)), instance_of(Hash)) - assert_difference('Show.count', 1) do + assert_difference('Show.count', 2) do assert_difference('Broadcast.count', 2) do assert_difference('AudioFile.count', 6) do Import.run diff --git a/test/services/import/broadcast_mapping/builder/airtime_db_test.rb b/test/services/import/broadcast_mapping/builder/airtime_db_test.rb index 02306ac..dd4be95 100644 --- a/test/services/import/broadcast_mapping/builder/airtime_db_test.rb +++ b/test/services/import/broadcast_mapping/builder/airtime_db_test.rb @@ -52,7 +52,7 @@ class Import::BroadcastMapping::Builder::AirtimeDbTest < ActiveSupport::TestCase morgen_map = mappings.first assert_equal morgen.name, morgen_map.show.name assert_equal morgen.description, morgen_map.show.details - assert morgen_map.show.new_record? + assert morgen_map.show.persisted? assert_equal morgen.name, morgen_map.broadcast.label assert_equal morgen.description, morgen_map.broadcast.details assert_equal Time.zone.local(2016, 1, 1, 8), morgen_map.broadcast.started_at diff --git a/test/services/import/broadcast_mapping_test.rb b/test/services/import/broadcast_mapping_test.rb index 50118d1..92e04b8 100644 --- a/test/services/import/broadcast_mapping_test.rb +++ b/test/services/import/broadcast_mapping_test.rb @@ -10,7 +10,7 @@ class Import::BroadcastMappingTest < ActiveSupport::TestCase assert_equal 'Morgen', show.name assert_equal 'La maƱana', show.details assert_equal profiles(:default), show.profile - assert show.new_record? + assert show.persisted? end test '#assign_show_attrs uses an existing show' do @@ -22,12 +22,12 @@ class Import::BroadcastMappingTest < ActiveSupport::TestCase assert_equal shows(:info), show end - test '#assign_show_attrs uses an existing show and keeps details' do + test '#assign_show_attrs uses an existing show and updates details' do shows(:info).update!(details: 'RaBe Info') mapping.assign_show(name: 'Info', details: 'Info') assert_equal 'Info', show.name - assert_equal 'RaBe Info', show.details + assert_equal 'Info', show.details assert_equal profiles(:important), show.profile assert_equal shows(:info), show end