Adding support for video file sizes
[catagits/App-IdiotBox.git] / lib / App / IdiotBox / Importer.pm
CommitLineData
ebba317f 1package App::IdiotBox::Importer;
2
3use strict;
4use warnings FATAL => 'all';
5use Cwd;
6use IO::All;
7use ExtUtils::MakeMaker qw(prompt);
8use File::Spec::Functions qw(catfile catdir);
9use POSIX qw(strftime);
10
1a1d7b64 11use Utils::PresentingPerl;
12
ebba317f 13sub log_info (&) { print $_[0]->(), "\n"; }
14
15sub run {
16 my ($class, $ib) = @_;
17 my @buckets = $ib->buckets->flatten;
18 my %bucket_by_slug;
19 log_info { "Available buckets to import into:" };
20 foreach my $idx (0 .. $#buckets) {
21 my $bucket = $buckets[$idx];
22 $bucket_by_slug{$bucket->slug} = $bucket;
23 log_info { "(${idx}) ${\$bucket->slug} : ${\$bucket->name}" };
24 }
25
26 my $bucket;
27
28 CHOOSE: {
29 my $choice = prompt("Which bucket to import into (by number or slug) ?");
30 if ($choice =~ /^\d+$/) {
31 $bucket = $buckets[$choice];
32 } else {
33 $bucket = $bucket_by_slug{$choice};
34 }
35 unless ($bucket) {
36 log_info {
37 "No bucket for ${choice} - valid options are 0 to ${\$#buckets}"
38 ." or slug (e.g. ${\$buckets[0]->slug})"
39 };
40 redo CHOOSE;
41 }
42 }
43
44 my $ann = $ib->recent_announcements->add(bless({
45 bucket => $bucket,
46 made_at => strftime("%Y-%m-%d %H:%M:%S",localtime),
47 }, 'App::IdiotBox::Announcement'));
48
49 log_info { "Created new announcement, id ".$ann->id };
50
51 my $video_files = $class->video_files_from_dir(my $source_dir = cwd);
52
53 my %videos;
54
55 foreach my $video_file (keys %{$video_files}) {
56
57 log_info { "Processing file ${video_file}" };
58 my @parts = split(/[- ]+/, $video_file);
59 my @options;
60 foreach my $idx (1 .. $#parts) {
61 my @opt = @{$options[$idx] = [
62 join(' ', @parts[0..$idx-1]),
63 join(' ', @parts[$idx..$#parts]),
64 ]};
65 log_info { "(${idx}) ".join(' / ', @opt) };
66 }
67 my $info;
68 CHOICE: {
69 my $choice = prompt(
70 'What author is this for (enter number for pre-selected combination) ?',
71 2
72 );
73 if ($choice =~ /^\d+$/) {
74 @{$info}{qw(author name)} = @{$options[$choice] || redo CHOICE};
75 } else {
76 $info->{author} = $choice;
77 }
78 }
1a1d7b64 79 my $size = get_file_size($video_file . '.' . $video_files->{$video_file}[0]);
ebba317f 80 $info->{name} = prompt('What is the name of this talk?', $info->{name});
81 (my $slug = lc $info->{name}) =~ s/ /-/g;
82 $info->{slug} = prompt('What is the slug for this talk?', $slug);
1a1d7b64 83 $info->{width} = prompt('What is the width of the video resolution?', $size->{width});
84 $info->{height} = prompt('What is the height of the video resolution?', $size->{height});
ebba317f 85 $info->{bucket} = $bucket;
86 $info->{announcement} = $ann;
87 $videos{$video_file} = bless($info, 'App::IdiotBox::Video');
88 }
89 foreach my $video_file (keys %videos) {
90 my $video = $videos{$video_file};
91 my $target_dir = catdir($ib->config->{base_dir}, $video->url_path);
92 io($target_dir)->mkpath;
93 log_info { "Copying video files to ${target_dir}"};
94 foreach my $ext (@{$video_files->{$video_file}}) {
95 no warnings 'void';
96 io(catfile($target_dir, "${\$video->file_name}.${ext}"))
97 < io(catfile($source_dir, "${video_file}.${ext}"));
98 }
99 }
100
101 $bucket->videos->add($_) for values %videos;
102}
103
104sub video_files_from_dir {
105 my ($class, $dir) = @_;
106 my %videos;
107 foreach my $file (io($dir)->all_files) {
108 $file->filename =~ /^([^\.]+)\.([^\.]+)$/ or next;
109 push(@{$videos{$1}||=[]}, $2);
110 }
111 \%videos;
112}
113
1a1d7b64 114sub get_video_size {
115 my ($filename) = @_;
116
117 my $size = {};
118 if($filename =~ /\.flv$/) {
119 eval { $size = Utils::PresentingPerl::get_flv_info($filename, [qw/width height/]);};
120 if($@) {
121 print "Looking for $filename size, failed, returning 0s\n";
122 return { width => 0, height => 0 };
123 }
124 }
125}
126
ebba317f 1271;