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 sub log_info (&) { print $_[0]->(), "\n"; }
14 my ($class, $ib) = @_;
15 my @buckets = $ib->buckets->flatten;
17 log_info { "Available buckets to import into:" };
18 foreach my $idx (0 .. $#buckets) {
19 my $bucket = $buckets[$idx];
20 $bucket_by_slug{$bucket->slug} = $bucket;
21 log_info { "(${idx}) ${\$bucket->slug} : ${\$bucket->name}" };
27 my $choice = prompt("Which bucket to import into (by number or slug) ?");
28 if ($choice =~ /^\d+$/) {
29 $bucket = $buckets[$choice];
31 $bucket = $bucket_by_slug{$choice};
35 "No bucket for ${choice} - valid options are 0 to ${\$#buckets}"
36 ." or slug (e.g. ${\$buckets[0]->slug})"
42 my $ann = $ib->recent_announcements->add(bless({
44 made_at => strftime("%Y-%m-%d %H:%M:%S",localtime),
45 }, 'App::IdiotBox::Announcement'));
47 log_info { "Created new announcement, id ".$ann->id };
49 my $video_files = $class->video_files_from_dir(my $source_dir = cwd);
53 foreach my $video_file (keys %{$video_files}) {
55 log_info { "Processing file ${video_file}" };
56 my @parts = split(/[- ]+/, $video_file);
58 foreach my $idx (1 .. $#parts) {
59 my @opt = @{$options[$idx] = [
60 join(' ', @parts[0..$idx-1]),
61 join(' ', @parts[$idx..$#parts]),
63 log_info { "(${idx}) ".join(' / ', @opt) };
68 'What author is this for (enter number for pre-selected combination) ?',
71 if ($choice =~ /^\d+$/) {
72 @{$info}{qw(author name)} = @{$options[$choice] || redo CHOICE};
74 $info->{author} = $choice;
77 $info->{name} = prompt('What is the name of this talk?', $info->{name});
78 (my $slug = lc $info->{name}) =~ s/ /-/g;
79 $info->{slug} = prompt('What is the slug for this talk?', $slug);
80 $info->{bucket} = $bucket;
81 $info->{announcement} = $ann;
82 $info->{announcement_id} = $ann->id; # Temp fix so INSERT behaves -- alh
83 $videos{$video_file} = bless($info, 'App::IdiotBox::Video');
85 foreach my $video_file (keys %videos) {
86 my $video = $videos{$video_file};
87 my $target_dir = catdir($ib->config->{base_dir}, $video->url_path);
88 io($target_dir)->mkpath;
89 log_info { "Copying video files to ${target_dir}"};
90 foreach my $ext (@{$video_files->{$video_file}}) {
92 io(catfile($target_dir, "${\$video->file_name}.${ext}"))
93 < io(catfile($source_dir, "${video_file}.${ext}"));
97 $bucket->videos->add($_) for values %videos;
100 sub video_files_from_dir {
101 my ($class, $dir) = @_;
103 foreach my $file (io($dir)->all_files) {
104 $file->filename =~ /^([^\.]+)\.([^\.]+)$/ or next;
105 push(@{$videos{$1}||=[]}, $2);