script/import_csv.pl untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 12 Podpod n/a totaltotal 0.0% 0 / 127 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.09'; # VERSION
7  
8 use autodie;
9  
10 use Video::PlaybackMachine::Schema;
11 use Text::CSV;
12 use DateTime;
13 use DateTime::Format::Strptime;
14 use DateTime::TimeZone::UTC;
15 use Video::Xine;
16 use Video::Xine::Stream ':meta_constants';
17 use POSIX 'ceil';
18 use Getopt::Long;
19  
20 our $Erase_Flag = 0;
21  
22 GetOptions(
23 'erase' => \$Erase_Flag
24 );
25  
26 my ($schedule_name, $csv_file, $db_file) = @ARGV;
27  
28 MAIN: {
29  
30 my $csv = Text::CSV->new( { 'binary' => 1 } )
31 or die 'Cannot use CSV: ' . Text::CSV->error_diag();
32  
33 open(my $fh, '<:encoding(utf8)', $csv_file);
34
35 my $strp_spreadsheet = DateTime::Format::Strptime->new(
36 pattern => '%m/%d/%Y %T',
37 time_zone => DateTime::TimeZone::UTC->new(),
38 on_error => 'croak'
39 );
40
41 my $strp_epoch = DateTime::Format::Strptime->new(
42 pattern => '%s',
43 time_zone => DateTime::TimeZone::UTC->new(),
44 on_error => 'croak'
45 );
46
47 if ($Erase_Flag && -e $db_file) {
48 rename($db_file, "$db_file.old");
49 }
50
51 my $schema = Video::PlaybackMachine::Schema->connect(
52 "dbi:SQLite:dbname=$db_file",
53 '',
54 ''
55 );
56
57 if ($Erase_Flag) {
58 $schema->deploy({ 'add_drop_table' => 0 });
59 }
60
61 my $schedule = $schema->resultset('Schedule')->create({ 'name' => $schedule_name });
62
63 # Skip header line
64 my $header = $csv->getline( $fh );
65
66 my $entry_rs = $schema->resultset('ScheduleEntry');
67 my $movie_info_rs = $schema->resultset('MovieInfo');
68 my $entry_end_rs = $schema->resultset('ScheduleEntryEnd');
69
70 my $xine = Video::Xine->new();
71
72 my $vo = Video::Xine::Driver::Video->new($xine, 'none');
73 my $ao = Video::Xine::Driver::Audio->new($xine, 'none');
74 my $stream = $xine->stream_new($ao, $vo);
75
76 ROW: while ( my $row = $csv->getline( $fh ) ) {
77 my ($start_str, $mrl) = @$row;
78 length($start_str) or next;
79
80 my $start_dt = $strp_spreadsheet->parse_datetime( $start_str );
81
82 my $start_epoch = $strp_epoch->format_datetime( $start_dt );
83
84 $stream->open($mrl) or die "Couldn't open '$mrl'";
85 my (undef, undef, $length_millis) = $stream->get_pos_length();
86 my $title = $stream->get_meta_info(XINE_META_INFO_TITLE);
87 if ( defined $title ) {
88 print STDERR "$title\n";
89 }
90 $stream->close();
91
92 my $length_secs = ceil( $length_millis / 1000 );
93
94 my @conflicts = $schedule->movie_conflicts( $start_epoch, $length_secs );
95
96 if ( scalar @conflicts ) {
97 print STDERR "Movie '$title' conflicts with: ",
98 join(', ', map { $_->mrl() } @conflicts ),
99 "\n";
100 next ROW;
101 }
102
103 my $entry = $entry_rs->create({ 'schedule_id' => $schedule->schedule_id,
104 'mrl' => $mrl,
105 'start_time' => $start_epoch
106 });
107
108 $movie_info_rs->create({'mrl' => $mrl,
109 'duration' => $length_secs,
110 'title' => $title
111 });
112
113 $entry_end_rs->create({ 'schedule_entry_id' => $entry->schedule_entry_id(),
114 'stop_time' => $start_epoch + $length_secs
115 });
116 }
117 }