-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathalbumen
More file actions
139 lines (106 loc) · 3.4 KB
/
albumen
File metadata and controls
139 lines (106 loc) · 3.4 KB
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
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long::Descriptive;
use List::Util qw(sum);
use Mac::Glue qw(:glue);
my ($opt, $usage) = describe_options(
'%c %o',
[ 'interactive|i!', 'prompt for each album' ],
);
my $itunes = Mac::Glue->new('iTunes');
my $pl = $itunes->obj(playlist => whose(name => equals => 'Regularer'))->get;
my $albumen = $itunes->obj(playlist => whose(name => equals => 'Albumen'))->get;
die "couldn't find target playlist" unless $albumen;
{
my $tracks = $itunes->obj(
track => gAll,
playlist => $albumen->prop('index')->get,
);
$_->delete for $tracks->get;
}
print "getting tracks\n";
my @tracks = $pl->obj('tracks')->get;
my %album;
while (my $track = shift @tracks) {
my $trackid = $track->prop('database ID')->get;
my $album = $track->prop('album')->get;
my $artist = $track->prop('compilation')->get
? '-'
: $track->prop('artist')->get;
next unless defined $album and defined $artist;
next unless length $album and length $artist;
my $rec = $album{ $album, $artist } ||= [];
printf "storing record of $trackid ($album/$artist); %s left\n", 0 + @tracks;
push @$rec, {
id => $trackid,
rating => scalar $track->prop('rating')->get,
played => scalar $track->prop('played date')->get, # epoch sec
};
}
my $DEFAULT_TIME = time - 30 * 86_400;
my %avg_age;
ALBUM: for my $key (keys %album) {
my ($album, $artist) = split $;, $key;
printf "considering (%s/%s)\n", $album, $artist;
my @tracks = @{ $album{ $key } };
unless (@tracks > 4) {
printf "skipping (%s/%s); too few tracks\n", $album, $artist;
delete $album{$key};
next ALBUM;
}
my @lp_dates = map { undef $_ if $_ eq 'msng'; $_ || $DEFAULT_TIME }
map { $_->{played} }
@tracks;
my $avg_age = time - (sum(@lp_dates) / @lp_dates);
$avg_age{ $key } = $avg_age;
if ($avg_age < 86_400 * 30) {
printf "skipping (%s/%s); too recent\n", $album, $artist;
delete $album{$key};
next ALBUM;
}
my @ratings = grep { $_ > 0 } map { $_->{rating} } @tracks;
my $avg_rating = sum(@ratings) / @ratings if @ratings;
if ($avg_rating and $avg_rating < 65) {
printf "skipping (%s/%s); too lousy\n", $album, $artist;
delete $album{$key};
next ALBUM;
}
printf "keeping (%s/%s) @ %s\n", $album, $artist, $avg_rating || '(n/a)';
}
my $total_size = 0;
my %seen_artist;
my %included_track;
ADDITION: for my $key (sort { $avg_age{$b} <=> $avg_age{$a} } keys %album) {
my ($album, $artist) = split $;, $key;
if ($artist ne '-' and $seen_artist{ $artist }) {
printf "skipping (%s/%s); already have album by this artist\n",
$album, $artist;
next ADDITION;
}
if ($opt->{interactive}) {
local $|;
$|++;
for (1) {
print "include ($album by $artist)? ";
my $line = <STDIN>;
next ADDITION if $line =~ /^n/;
redo unless $line =~ /^y/;
}
}
$seen_artist{ $artist }++;
my @tracks = @{ $album{ $key } };
for my $track (@tracks) {
my $t = $itunes->obj(
track => whose('database id' => equals => $track->{id})
)->get;
$total_size += $t->prop('size')->get; # in bytes
my $dupe_track = $itunes->duplicate($t, to => $albumen);
unless ($dupe_track) {
warn "could not dupe track\n";
next;
}
$included_track{ $dupe_track->prop('database id')->get } = 1;
}
last ADDITION if $total_size > 1_000_000_000;
}