Separate out App::IdiotBox::* DB objects and put create/update
[catagits/App-IdiotBox.git] / lib / App / IdiotBox / Importer.pm
CommitLineData
ebba317f 1package App::IdiotBox::Importer;
2
3use strict;
4use warnings FATAL => 'all';
625f105e 5use App::IdiotBox::Common qw(@SupportedFormats);
ebba317f 6use Cwd;
7use IO::All;
8use ExtUtils::MakeMaker qw(prompt);
9use File::Spec::Functions qw(catfile catdir);
10use POSIX qw(strftime);
11
625f105e 12my $supported_formats_re = join('|', @SupportedFormats);
13
ebba317f 14sub log_info (&) { print $_[0]->(), "\n"; }
15
16sub run {
17 my ($class, $ib) = @_;
18 my @buckets = $ib->buckets->flatten;
19 my %bucket_by_slug;
20 log_info { "Available buckets to import into:" };
ebba317f 21
22 my $bucket;
23
8ee9b216 24 BUCKETS: {
25 foreach my $idx (0 .. $#buckets) {
26 my $bucket = $buckets[$idx];
27 $bucket_by_slug{$bucket->slug} = $bucket;
28 log_info { "(${idx}) ${\$bucket->slug} : ${\$bucket->name}" };
ebba317f 29 }
8ee9b216 30 log_info { sprintf("(%d) new : Create new bucket\n", $#buckets + 1) };
31
32 CHOOSE: {
33 my $choice = prompt("Which bucket to import into (by number or slug) ?");
34 if ($choice =~ /^\d+$/) {
35 $bucket = $buckets[$choice];
36 } else {
37 $bucket = $bucket_by_slug{$choice};
38 }
39
40 if (!$bucket && ($choice eq 'new' || $choice eq $#buckets + 1)) {
41 my $sn = prompt("What's the new short name (url path) for the slug ?");
42 my $ln = prompt("What's the new long name (description) for the slug ?");
43
f29e9b6f 44 my ($nb, $err) = App::IdiotBox::Bucket->create(
8ee9b216 45 slug => $sn,
46 name => $ln,
f29e9b6f 47 );
48
49 if ($err) {
50 log_info { "Error creating new bucket: $err" };
51 redo CHOOSE;
52 }
53
54 my $rnb = $ib->buckets->add($nb);
8ee9b216 55
56 log_info { "Created new bucket" };
f29e9b6f 57 push @buckets, $rnb;
8ee9b216 58 redo BUCKETS;
59 }
60
61 unless ($bucket) {
62 log_info {
63 "No bucket for ${choice} - valid options are 0 to ${\$#buckets + 1}"
64 ." or slug (e.g. ${\$buckets[0]->slug})"
65 };
66 redo CHOOSE;
67 }
ebba317f 68 }
69 }
ebba317f 70 my $ann = $ib->recent_announcements->add(bless({
71 bucket => $bucket,
72 made_at => strftime("%Y-%m-%d %H:%M:%S",localtime),
73 }, 'App::IdiotBox::Announcement'));
74
75 log_info { "Created new announcement, id ".$ann->id };
76
77 my $video_files = $class->video_files_from_dir(my $source_dir = cwd);
78
79 my %videos;
80
81 foreach my $video_file (keys %{$video_files}) {
82
83 log_info { "Processing file ${video_file}" };
84 my @parts = split(/[- ]+/, $video_file);
85 my @options;
86 foreach my $idx (1 .. $#parts) {
87 my @opt = @{$options[$idx] = [
88 join(' ', @parts[0..$idx-1]),
89 join(' ', @parts[$idx..$#parts]),
90 ]};
91 log_info { "(${idx}) ".join(' / ', @opt) };
92 }
93 my $info;
94 CHOICE: {
95 my $choice = prompt(
96 'What author is this for (enter number for pre-selected combination) ?',
97 2
98 );
99 if ($choice =~ /^\d+$/) {
100 @{$info}{qw(author name)} = @{$options[$choice] || redo CHOICE};
101 } else {
102 $info->{author} = $choice;
103 }
104 }
105 $info->{name} = prompt('What is the name of this talk?', $info->{name});
106 (my $slug = lc $info->{name}) =~ s/ /-/g;
107 $info->{slug} = prompt('What is the slug for this talk?', $slug);
108 $info->{bucket} = $bucket;
109 $info->{announcement} = $ann;
ce2b1e73 110 $info->{announcement_id} = $ann->id; # Temp fix so INSERT behaves -- alh
ebba317f 111 $videos{$video_file} = bless($info, 'App::IdiotBox::Video');
112 }
113 foreach my $video_file (keys %videos) {
114 my $video = $videos{$video_file};
115 my $target_dir = catdir($ib->config->{base_dir}, $video->url_path);
116 io($target_dir)->mkpath;
117 log_info { "Copying video files to ${target_dir}"};
118 foreach my $ext (@{$video_files->{$video_file}}) {
119 no warnings 'void';
120 io(catfile($target_dir, "${\$video->file_name}.${ext}"))
121 < io(catfile($source_dir, "${video_file}.${ext}"));
122 }
123 }
124
125 $bucket->videos->add($_) for values %videos;
126}
127
128sub video_files_from_dir {
129 my ($class, $dir) = @_;
130 my %videos;
131 foreach my $file (io($dir)->all_files) {
625f105e 132 $file->filename =~ /^([^\.]+)\.($supported_formats_re)$/ or next;
ebba317f 133 push(@{$videos{$1}||=[]}, $2);
134 }
135 \%videos;
136}
137
1381;