summaryrefslogtreecommitdiff
path: root/lib/AniDB/Manager.pm
blob: 21d0d822486ad93f12d4795524a33d9a0a9e96a7 (plain)
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
package AniDB::Manager; 
use 5.024;
use Moo;
use experimental 'signatures';
use Log::Any '$log';
use Path::Tiny;
use namespace::clean;
 
has datastore => (
    is => 'ro',
    required => 1,
);
 
has api_client => (
    is => 'ro',
    required => 1,
);
 
around BUILDARGS => sub($orig,$class,@args) {
    my $args = $class->$orig(@args);
    if (my $db_name = $args->{database}) {
        require AniDB::Datastore;
        $args->{datastore} = AniDB::Datastore->new({
            database => $db_name,
        });
    }
    if (my $username = $args->{username}) {
        require AniDB::APIClient;;
        $args->{api_client} = AniDB::APIClient->new({
            %{$args}{qw(username password)},
        });
    }
    return $args;
};
 
has renamer => (
    is => 'ro',
    lazy => 1,
);
 
sub _build_renamer {
    require AniDB::Renamer;
    return AniDB::Renamer->new();
}
 
has hash_function => (
    is => 'ro',
    lazy => 1,
);
 
sub _build_hash_function {
    require AniDB::Hashing;
    return \&AniDB::Hashing::hash_fd;
}
 
sub new_name_for($self,$path) {
    $path = path($path)->realpath;
    $self->update($path);
    my $full_info = $self->datastore->full_info_for($path);
    return $self->renamer->new_name_for($path,$full_info);
}
 
sub maybe_new_name_for($self,$path) {
    my $new_name = $self->new_name_for($path);
    return if $new_name eq $path;
    return $new_name;
}
 
sub rename($self,$path,$new_path) {
    $path = path($path)->realpath;
    $new_path ||= $self->new_name_for($path);
    if ($path->move($new_path)) {
        $self->datastore->rename($path,$new_path);
        return 1;
    }
    else {
        $log->errorf(
            'failed to rename %s to %s',
            $path$new_path,
        );
        return;
    }
}
 
sub update($self,$path) {
    $path = path($path)->realpath;
 
    my $stat = $path->stat;
 
    return unless $self->datastore->has_changed($path,$stat);
 
    my $hash = $self->hash_function->($path->openr_raw);
 
    $self->datastore->update_path_info(
        $path => my $path_info = {
            size => $stat->size,
            mtime => $stat->mtime,
            hash => $hash,
        },
    );
 
    my $episode_info = $self->datastore->episode_info_for($path_info);
    unless ($episode_info) {
        $episode_info = $self->api_client->episode_info_for($path_info);
        $self->datastore->update_episode_info(
            $path_info$episode_info,
        );
    }
 
    my $anime_info = $self->datastore->anime_info_for($episode_info);
    unless ($anime_info) {
        $anime_info = $self->api_client->anime_info_for($episode_info);
        $self->datastore->update_anime_info(
            $episode_info$anime_info,
        );
    }
}
 
1;