Skip to content

Commit

Permalink
accept iso 8601 time periods in recording file names
Browse files Browse the repository at this point in the history
  • Loading branch information
codez committed Sep 16, 2019
1 parent 671e65d commit e826863
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 72 deletions.
22 changes: 16 additions & 6 deletions app/services/import/recording/file/iso8601.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ module Import
module Recording
module File

# Iso 8601 recordings must have the format yyyy-mm-ddTHHMM+ZZZZ_ddd.ext, where ZZZZ
# stands for the time zone offset, ddd for the duration in minutes and ext
# for the file extension.
# ISO 8601 recordings must have the format yyyy-mm-ddTHHMM+ZZZZ_{ddd,PTaaHbbMccS}.ext,
# where ZZZZ stands for the time zone offset, ddd for the duration in minutes or
# PTaaHbbMccS for an ISO 8601 time period (where segments are optional)
# and ext for the file extension.
class Iso8601 < Base

DATE_TIME_FORMAT = '%Y-%m-%dT%H%M%S%z'
IMPORTED_SUFFIX = '_imported'
DATE_GLOB = '[12][019][0-9][0-9]-[0-1][0-9]-[0-3][0-9]' # yyyy-mm-dd
TIME_GLOB = '[0-2][0-9][0-5][0-9][0-5][0-9]{+,-}[0-2][0-9][0-5]0' # HHMMSS+ZZZZ
DURATION_GLOB = DIGIT_GLOB * 3 # ddd, minutes
FILENAME_GLOB = "#{DATE_GLOB}T#{TIME_GLOB}_#{DURATION_GLOB}"
PERIOD_GLOB = 'PT{*H,*M,*S}' # ISO time period, e.g. PT1H30M
FILENAME_GLOB = "#{DATE_GLOB}T#{TIME_GLOB}_{#{DURATION_GLOB},#{PERIOD_GLOB}}"

self.pending_glob = FILENAME_GLOB + '.*'
self.imported_glob = FILENAME_GLOB + IMPORTED_SUFFIX + '.*'
Expand All @@ -25,7 +27,7 @@ def started_at

# in seconds
def duration
@duration ||= filename_parts[2].to_i * 60
@duration ||= parse_duration(filename_parts[2])
end

def mark_imported
Expand All @@ -37,9 +39,17 @@ def mark_imported

private

def parse_duration(part)
if part.start_with?('PT')
ActiveSupport::Duration.parse(part).to_i
else
part.to_i * 60
end
end

def filename_parts
name = basename('.*')
name.match(/^(.+)_(\d{3})(#{IMPORTED_SUFFIX})?$/)
name.match(/^(.+)_(\d{3}|PT[0-9\.\,HMS]+)(#{IMPORTED_SUFFIX})?$/)
end

end
Expand Down
2 changes: 1 addition & 1 deletion doc/import.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The import processes audio files from a external sources, aka recordings. The wa
* The audio files are put in the directories defined by `IMPORT_DIRECTORIES`.
* Multiple directories may contain different recordings for the same times (for failover purposes), but with the same duration.
* Recording durations do not have to correspond to broadcast durations.
* The recording file names must be in the format `yyyy-mm-ddTHHMMSS±ZZZZ_ddd.*` (year '-' month '-' day 'T' hour minute second '+/-' time zone offset '_' duration '.' extension, e.g. '2015-02-12T120000+0200_060.mp3').
* The recording file names must be in the format `yyyy-mm-ddTHHMMSS±ZZZZ_{ddd,PTaaHbbMccS}.*` (year '-' month '-' day 'T' hour minute second '+/-' time zone offset '_' { duration in minutes OR 'PT' hours 'H' minutes 'M' seconds 'S' (= ISO 8061 time period) } '.' extension, e.g. '2015-02-12T120000+0200_090.mp3' OR '2015-02-12T120000+0200_PT1H30M.mp3').

## Procedure

Expand Down
Loading

0 comments on commit e826863

Please sign in to comment.