| 1 | package Dancer::SearchApp::Extractor::Audio; | ||
| 2 | use strict; | ||
| 3 | use Carp 'croak'; | ||
| 4 | use Promises 'deferred'; | ||
| 5 | #no warnings 'experimental'; | ||
| 6 | #use feature 'signatures'; | ||
| 7 | use MP3::Tag; | ||
| 8 | use POSIX 'strftime'; | ||
| 9 | use HTML::Entities; | ||
| 10 | |||
| 11 | use vars qw($VERSION); | ||
| 12 | $VERSION = '0.06'; | ||
| 13 | |||
| 14 | =head1 File types | ||
| 15 | |||
| 16 | This uses L<MP3::Tag> and thus likely only handles mp3 files. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | sub examine { | ||
| 21 | my( $class, %options ) = @_; | ||
| 22 | my $info = $options{info}; | ||
| 23 | my $meta = $options{ meta }; | ||
| 24 | |||
| 25 | my $result = deferred; | ||
| 26 | my $mime_type = $meta->{"Content-Type"}; | ||
| 27 | |||
| 28 | if( $mime_type =~ m!^audio/mpeg$! ) { | ||
| 29 | my $mp3; | ||
| 30 | my %res = ( | ||
| 31 | url => $options{ url }, | ||
| 32 | file => $options{ filename }, | ||
| 33 | folder => $options{ folder }, | ||
| 34 | ); | ||
| 35 | my $file = $options{ filename }; | ||
| 36 | |||
| 37 | # If we have a filename, use that | ||
| 38 | if( $file ) { | ||
| 39 | $mp3 = MP3::Tag->new("$file"); | ||
| 40 | |||
| 41 | my $ctime = (stat $file)[10]; | ||
| 42 | $res{ creation_date } = strftime('%Y-%m-%d %H:%M:%S', localtime($ctime)); | ||
| 43 | |||
| 44 | } elsif( $options{ content }) { | ||
| 45 | # If we have in-memory content, just open a filehandle to it | ||
| 46 | # This involves writing MP3::Tag::File::InMemory, which overrides | ||
| 47 | # the ->filename and ->open method | ||
| 48 | croak "In-memory handling is not yet supported, sorry"; | ||
| 49 | } | ||
| 50 | |||
| 51 | if( $mp3) { | ||
| 52 | |||
| 53 | # go go go | ||
| 54 | my ($title, $track, $artist, $album, $comment, $year, $genre) = $mp3->autoinfo(); | ||
| 55 | $res{ title } = $title || $file->basename; | ||
| 56 | $res{ author } = $artist; | ||
| 57 | $res{ language } = 'en'; # ... | ||
| 58 | $res{ content } = encode_entities( join "-", $artist, $album, $track, $comment, $genre, $file->basename, 'mp3' ); | ||
| 59 | $res{ mime_type } = $mime_type; | ||
| 60 | # We should also calculate the duration here, and some more information | ||
| 61 | |||
| 62 | $result->resolve( \%res ); | ||
| 63 | } else { | ||
| 64 | # Nothing found | ||
| 65 | $result->resolve(); | ||
| 66 | } | ||
| 67 | } else { | ||
| 68 | $result->resolve(); | ||
| 69 | } | ||
| 70 | |||
| 71 | $result->promise | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; |