Skip to content

Commit 7a8b59e

Browse files
committed
refactor: properly store season metadata in tv library
1 parent 5986b6e commit 7a8b59e

File tree

3 files changed

+39
-41
lines changed

3 files changed

+39
-41
lines changed

App-MHFS/lib/MHFS/Kodi/Season.pm

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ sub _read_season_dir {
4343
}
4444

4545
sub Format {
46-
my ($seeason, $id, $sourcemap, $meta) = @_;
46+
my ($seeason, $id, $sourcemap) = @_;
47+
my $editions = $seeason->{editions};
4748
my @unsorted;
48-
foreach my $path (keys %$seeason) {
49+
foreach my $path (keys %$editions) {
4950
my ($source, $item) = split('/', $path, 2);
50-
if (!$seeason->{$path}{isdir}) {
51-
push @unsorted, _encode_item("$source/".encode_base64url($item), $seeason->{$path}{name});
51+
if (!$editions->{$path}{isdir}) {
52+
push @unsorted, _encode_item("$source/".encode_base64url($item), $editions->{$path}{name});
5253
next;
5354
}
5455
my $b_path = $sourcemap->{$source}{folder}."/$item";
@@ -58,7 +59,8 @@ sub Format {
5859
fold_case($a->{name}) cmp fold_case($b->{name})
5960
} @unsorted;
6061
my %season = (id => $id+0, items => \@items, name => "Season $id");
61-
if ($meta) {
62+
if (exists $seeason->{meta}) {
63+
my $meta = $seeason->{meta};
6264
$season{plot} = $meta->{overview};
6365
my %seen;
6466
foreach my $item (@items) {
@@ -85,12 +87,12 @@ sub Format {
8587

8688
sub TO_JSON {
8789
my ($self) = @_;
88-
{season => Format($self->{season}, $self->{id}, $self->{sourcemap}, $self->{meta})}
90+
{season => Format($self->{season}, $self->{id}, $self->{sourcemap})}
8991
}
9092

9193
sub TO_HTML {
9294
my ($self) = @_;
93-
my $season = Format($self->{season}, $self->{id}, $self->{sourcemap}, $self->{meta});
95+
my $season = Format($self->{season}, $self->{id}, $self->{sourcemap});
9496
my $buf = '<style>ul{list-style: none;} li{margin: 10px 0;}</style><ul>';
9597
foreach my $item (@{$season->{items}}) {
9698
$buf .= html_list_item($item->{name}, 1);

App-MHFS/lib/MHFS/Kodi/SeasonLite.pm

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@ use MIME::Base64 qw(encode_base64url);
66

77
sub Format {
88
my ($seeason, $id) = @_;
9-
my $plot;
10-
# HACK: grab the season plot from a season item and apply to the season
11-
while(my ($key, $value) = each %{$seeason}) {
12-
$plot = $value->{plot} if (exists $value->{plot});
13-
last;
14-
}
9+
my $plot = exists $seeason->{meta} && exists $seeason->{meta}{overview} && $seeason->{meta}{overview};
10+
my $editions = $seeason->{editions};
1511
my @sorteditems = sort {
16-
$seeason->{$a}{name} cmp $seeason->{$b}{name}
17-
} keys %{$seeason};
12+
$editions->{$a}{name} cmp $editions->{$b}{name}
13+
} keys %{$editions};
1814
my @items = map {
1915
my ($source, $item) = split('/', $_, 2);
20-
my %item = %{$seeason->{$_}};
16+
my %item = %{$editions->{$_}};
2117
$item{id} = "$source/".encode_base64url($item);
22-
delete $item{plot}; # HACK: remove the plot from the season item
2318
\%item
2419
} @sorteditems;
2520
{id => $id+0, items => \@items, name => "Season $id", ($plot ? (plot => $plot) : ())}

App-MHFS/lib/MHFS/Plugin/Kodi.pm

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ BEGIN {
3030
}
3131
}
3232

33+
sub _read_season_meta {
34+
my ($self, $showid, $seasonid) = @_;
35+
try {
36+
my $bytes = read_file($self->{tvmeta}."/$showid/$seasonid/season.json");
37+
my $meta = decode_json($bytes);
38+
return $meta;
39+
} catch($e) {}
40+
return;
41+
}
42+
3343
sub readtvdir {
3444
my ($self, $tvshows, $source, $b_tvdir) = @_;
3545
my $dh;
@@ -63,8 +73,14 @@ sub readtvdir {
6373
catch($e) {}
6474
$tvshows->{$showid} = \%show;
6575
}
66-
$tvshows->{$showid}{seasons}{$season} //= {};
67-
$tvshows->{$showid}{seasons}{$season}{"$source/$b_filename"} = {name => $filename, isdir => (-d _ // 0)+0};
76+
$tvshows->{$showid}{seasons}{$season} //= {
77+
editions => {},
78+
do {
79+
my $meta = $self->_read_season_meta($showid, $season);
80+
$meta ? (meta => $meta) : ()
81+
},
82+
};
83+
$tvshows->{$showid}{seasons}{$season}{editions}{"$source/$b_filename"} = {name => $filename, isdir => (-d _ // 0)+0};
6884
}
6985
closedir($dh);
7086
}
@@ -80,21 +96,6 @@ sub _build_tv_library {
8096
my $b_tvdir = $self->{server}{settings}{SOURCES}{$source}{folder};
8197
$self->readtvdir(\%tvshows, $source, $b_tvdir);
8298
}
83-
# load the season metadata, maybe remove this if we allow querying a show without a season
84-
while (my ($showid, $show) = each %tvshows) {
85-
while (my ($seasonid, $season) = each %{$show->{seasons}}) {
86-
my $meta;
87-
try {
88-
my $bytes = read_file($self->{tvmeta}."/$showid/$seasonid/season.json");
89-
$meta = decode_json($bytes);
90-
} catch($e) {}
91-
$meta or next;
92-
# HACK: modifies each season item as there isn't a season object
93-
foreach my $value (values %$season) {
94-
$value->{plot} = $meta->{overview};
95-
}
96-
}
97-
}
9899
\%tvshows
99100
}
100101

@@ -104,15 +105,15 @@ sub _get_tv_item {
104105
exists $tvshows->{$showid}{seasons}{$seasonid} or die "season $seasonid does not exist";
105106
my $seasonitem = $tvshows->{$showid}{seasons}{$seasonid};
106107
my $sourcemap = $self->{server}{settings}{SOURCES};
107-
my $meta;
108-
try {
109-
my $bytes = read_file($self->{tvmeta}."/$showid/$seasonid/season.json");
110-
$meta = decode_json($bytes);
111-
} catch($e) {}
112-
$source or return bless {season => $seasonitem, id => $seasonid, sourcemap => $sourcemap, ($meta ? (meta => $meta) : ())}, 'MHFS::Kodi::Season';
108+
# TODO: Instead of updating the tv library here, the library should be updated when new metadata is loaded
109+
if (!exists $seasonitem->{meta}) {
110+
my $meta = $self->_read_season_meta($showid, $seasonid);
111+
$seasonitem->{meta} = $meta if $meta;
112+
}
113+
$source or return bless {season => $seasonitem, id => $seasonid, sourcemap => $sourcemap}, 'MHFS::Kodi::Season';
113114
$b64_item or die "b64_item not provided";
114115
my $path = abs_path($self->{server}{settings}{SOURCES}{$source}{folder} .'/' . decode_base64url($b64_item));
115-
if (!$path || rindex($path, $self->{server}{settings}{SOURCES}{$source}{folder}, 0) != 0, ! -f $path) {
116+
if (!$path || rindex($path, $self->{server}{settings}{SOURCES}{$source}{folder}, 0) != 0 || ! -f $path) {
116117
die "item not found";
117118
}
118119
{b_path => $path}

0 commit comments

Comments
 (0)