1 package App::IdiotBox::Importer;
4 use warnings FATAL => 'all';
7 use ExtUtils::MakeMaker qw(prompt);
8 use File::Spec::Functions qw(catfile catdir);
9 use POSIX qw(strftime);
11 use Utils::PresentingPerl;
13 sub log_info (&) { print $_[0]->(), "\n"; }
16 my ($class, $ib) = @_;
17 my @buckets = $ib->buckets->flatten;
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}" };
29 my $choice = prompt("Which bucket to import into (by number or slug) ?");
30 if ($choice =~ /^\d+$/) {
31 $bucket = $buckets[$choice];
33 $bucket = $bucket_by_slug{$choice};
37 "No bucket for ${choice} - valid options are 0 to ${\$#buckets}"
38 ." or slug (e.g. ${\$buckets[0]->slug})"
44 my $ann = $ib->recent_announcements->add(bless({
46 made_at => strftime("%Y-%m-%d %H:%M:%S",localtime),
47 }, 'App::IdiotBox::Announcement'));
49 log_info { "Created new announcement, id ".$ann->id };
51 my $video_files = $class->video_files_from_dir(my $source_dir = cwd);
55 foreach my $video_file (keys %{$video_files}) {
57 log_info { "Processing file ${video_file}" };
58 my @parts = split(/[- ]+/, $video_file);
60 foreach my $idx (1 .. $#parts) {
61 my @opt = @{$options[$idx] = [
62 join(' ', @parts[0..$idx-1]),
63 join(' ', @parts[$idx..$#parts]),
65 log_info { "(${idx}) ".join(' / ', @opt) };
70 'What author is this for (enter number for pre-selected combination) ?',
73 if ($choice =~ /^\d+$/) {
74 @{$info}{qw(author name)} = @{$options[$choice] || redo CHOICE};
76 $info->{author} = $choice;
79 my $size = get_file_size($video_file . '.' . $video_files->{$video_file}[0]);
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);
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});
85 $info->{bucket} = $bucket;
86 $info->{announcement} = $ann;
87 $videos{$video_file} = bless($info, 'App::IdiotBox::Video');
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}}) {
96 io(catfile($target_dir, "${\$video->file_name}.${ext}"))
97 < io(catfile($source_dir, "${video_file}.${ext}"));
101 $bucket->videos->add($_) for values %videos;
104 sub video_files_from_dir {
105 my ($class, $dir) = @_;
107 foreach my $file (io($dir)->all_files) {
108 $file->filename =~ /^([^\.]+)\.([^\.]+)$/ or next;
109 push(@{$videos{$1}||=[]}, $2);
118 if($filename =~ /\.flv$/) {
119 eval { $size = Utils::PresentingPerl::get_flv_info($filename, [qw/width height/]);};
121 print "Looking for $filename size, failed, returning 0s\n";
122 return { width => 0, height => 0 };