/
TV.pm
177 lines (141 loc) · 5.22 KB
/
TV.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use 5.010;
use strict;
use warnings;
package App::MP4Meta::TV;
# ABSTRACT: Add metadata to a TV Series
use App::MP4Meta::Base;
our @ISA = 'App::MP4Meta::Base';
use File::Spec '3.33';
use AtomicParsley::Command::Tags;
use App::MP4Meta::Source::Data::TVEpisode;
# a list of regexes to try to parse the file
my @file_regexes = (
qr/^S(?<season>\d)-E(?<episode>\d)\s+-\s+(?<show>.*)$/,
qr/^(?<show>.*)\s+S(?<season>\d\d)(\s|)E(?<episode>\d\d)$/,
qr/^(?<show>.*)\.S(?<season>\d\d)E(?<episode>\d\d)/i,
qr/^(?<show>.*)\s*-?\s*S(?<season>\d\d?)E(?<episode>\d\d?)/i,
qr/^(?<show>.*)-S(?<season>\d\d?)E(?<episode>\d\d?)/,
qr/^(?<show>.*)_S(?<season>\d\d?)E(?<episode>\d\d?)/,
qr/S(?<season>\d\d?)E(?<episode>\d\d?)/,
qr/^(?<show>.*)\s*-?\s*(?<season>\d\d?)x(?<episode>\d\d?)/,
);
sub new {
my $class = shift;
my $args = shift;
my $self = $class->SUPER::new($args);
# args for skipping
$self->{'continue_without_any'} = $args->{'continue_without_any'};
$self->{'continue_without_overview'} = $args->{'continue_without_overview'};
$self->{'continue_without_genre'} = $args->{'continue_without_genre'};
$self->{'continue_without_year'} = $args->{'continue_without_year'};
$self->{'continue_without_cover'} = $args->{'continue_without_cover'};
# attributes
$self->{'season'} = $args->{'season'};
$self->{'episode'} = $args->{'episode'};
$self->{'overview'} = $args->{'overview'};
$self->{'year'} = $args->{'year'};
# we are a TV Show
$self->{'media_type'} = 'TV Show';
return $self;
}
sub apply_meta {
my ( $self, $path ) = @_;
my %tags = (
show_title => $self->{'title'},
season => $self->{'season'},
episode => $self->{'episode'},
);
# get the file name
my ( $volume, $directories, $file ) = File::Spec->splitpath($path);
unless ( $tags{show_title} && $tags{season} && $tags{episode} ) {
# parse the filename for the title, season and episode
( $tags{show_title}, $tags{season}, $tags{episode} ) =
$self->_parse_filename($file);
unless ( $tags{show_title} && $tags{season} && $tags{episode} ) {
return "Error: could not parse the filename for $path";
}
}
my $episode = App::MP4Meta::Source::Data::TVEpisode->new(
genre => $self->{'genre'},
cover => $self->{'cover'},
overview => $self->{'overview'},
year => $self->{'year'},
);
unless ( _episode_is_complete($episode) ) {
for my $source ( @{ $self->{'sources_objects'} } ) {
say sprintf( "trying source '%s'", $source->name )
if $self->{verbose};
# merge new epiosde into previous
$episode->merge( $source->get_tv_episode( \%tags ) );
# last if we have everything
last
if ( _episode_is_complete($episode) );
}
}
# check what we have
unless ( $episode->overview ) {
if ( $self->{'continue_without_any'}
|| $self->{'continue_without_overview'} )
{
say 'no overview found; continuing';
}
else {
return sprintf( 'no overview found for %s, season %d, episode %d',
$tags{show_title}, $tags{season}, $tags{episode} );
}
}
my $show_title = $episode->show_title // $tags{show_title};
my $apTags = AtomicParsley::Command::Tags->new(
artist => $show_title,
albumArtist => $show_title,
title => $episode->title,
album => sprintf( "%s, Season %s", $show_title, $tags{season} ),
tracknum => $tags{episode},
TVShowName => $show_title,
TVEpisode => $tags{episode},
TVEpisodeNum => $tags{episode},
TVSeasonNum => $tags{season},
stik => $self->{'media_type'},
description => $episode->overview,
genre => $episode->genre,
year => $episode->year,
artwork => $episode->cover
);
say 'writing tags' if $self->{verbose};
return $self->_write_tags( $path, $apTags );
}
# Parse the filename in order to get the series title the and season and episode number.
sub _parse_filename {
my ( $self, $file ) = @_;
# strip suffix
$file =~ s/\.m4v$//;
# see if we have a regex that matches
for my $r (@file_regexes) {
if ( $file =~ $r ) {
my $show = $self->{title} // $+{show};
my $season = $self->{season} // $+{season};
my $episode = $self->{episode} // $+{episode};
if ( $show && $season && $episode ) {
return ( $self->_clean_title($show), int $season,
int $episode );
}
}
}
return;
}
# true if we have all we need in an episode
sub _episode_is_complete {
my $episode = shift;
return ( $episode->overview
&& $episode->genre
&& $episode->year
&& $episode->cover );
}
1;
=head1 SYNOPSIS
my $tv = App::MP4Meta::TV->new({ genre => 'Comedy' });
$tv->apply_meta( '/path/to/THE_MIGHTY_BOOSH_S1E1.m4v' );
=method apply_meta( $path )
Apply metadata to the file at this path.
Returns undef if success; string if error.
=cut