-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbanshee-lastfm-sync
executable file
·309 lines (253 loc) · 6.82 KB
/
banshee-lastfm-sync
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env perl
use utf8;
binmode STDOUT, ":utf8";
package Banshee::LastFm::Sync;
use Data::Dump;
use common::sense;
use Net::LastFMAPI;
use Data::Types qw(:int);
use DBI;
use Try::Tiny;
use Term::ProgressBar;
use File::Copy;
use Moose;
with 'MooseX::Getopt';
has key => ( is => "rw", isa => "Str", required => 1 );
has username => ( is => "rw", isa => "Str", required => 1 );
has db => ( is => "rw", isa => "Str", required => 1 );
has zero => ( is => "rw", isa => "Bool", required => 0, default => sub { 0 } );
has updateloved => ( is=> "rw", isa => "Bool", required => 0, default => sub { 0 });
has lovedrating => ( is => "rw", isa => "Int", required => 0, default => sub { 5 });
has _config => ( is => "rw", lazy_build => 1, required => 0 );
has debug => ( is => "rw", isa => "Bool", default => sub{ 0 }, accessor => "debug" );
__PACKAGE__->meta->make_immutable;
binmode STDOUT, ":utf8";
#
# builders
#
sub _build_config {
my $self = shift;
lastfm_config(
api_key => $self->key,
secret => $self->secret
);
}
=item backup
create backup of database
=cut
sub backup {
my ( $self ) = @_;
#
# check if database file exists
#
unless( -f $self->db ) {
say "Database file ${\$self->db} doesn't exists";
exit(1);
}
#
# just copy file with .backup postfix
#
my $backup = sprintf "%s.backup", $self->db;
copy( $self->db, $backup );
say "Creating backup $backup";
}
sub sync {
my( $self ) = @_;
#
# independent counter
#
my $counter = 1;
my $total = 0;
my $changed = 0;
#
# allow only rating from 1 to 5
#
unless ( $self->lovedrating >= 1 && $self->lovedrating <= 5 ) {
say "Invalid rating '${\$self->lovedrating}'. A value from 1 to 5 is required.";
exit(1);
}
#
# only tracks with 0 playcount
#
my $zero = "";
$zero = "AND t.PlayCount = 0" if( $self->zero );
#
# check if database file exists
#
unless( -f $self->db ) {
say "Database file ${\$self->db} doesn't exists";
exit(1);
}
#
# iterate on track
#
my $dbh = DBI->connect( sprintf("dbi:SQLite:dbname=%s", $self->db ) , "", "" );
$dbh->{sqlite_unicode} = 1;
#
# count affected rows for progress bar, let me know if You know any other way
#
my $sth = $dbh->prepare( qq{
SELECT
count( * ) AS count
FROM
CoreTracks t,
CoreArtists a,
CorePrimarySources s
WHERE
t.ArtistID = a.ArtistID AND
t.PrimarySourceID = s.PrimarySourceID AND
s.StringID = 'MusicLibrarySource-Library'
$zero
} );
$sth->execute;
$total = $sth->fetchrow_hashref()->{ "count" };
$sth->finish;
say "Found $total tracks to process";
#
# real query
#
$sth = $dbh->prepare( qq{
SELECT
*,
t.Rating AS TrackRating
FROM
CoreTracks t,
CoreArtists a,
CorePrimarySources s
WHERE
t.ArtistID = a.ArtistID AND
t.PrimarySourceID = s.PrimarySourceID AND
s.StringID = 'MusicLibrarySource-Library'
$zero
ORDER BY
TrackID
} );
$sth->execute();
#
# get library tracks and parse them so that we can get play counts faster
#
say "Fetching Last.fm library..." if $self->debug;
my $libResponse = undef;
my $libCounter = 0;
try {
$libResponse = lastfm_iter(
"library.getTracks",
user => $self->username,
limit => 250
);
};
if ( !$libResponse ) {
say "Error: Could not fetch Last.fm user library.";
exit(1);
}
my $libProgress = Term::ProgressBar->new( { count => $Net::LastFMAPI::last_response->{"tracks"}->{"\@attr"}->{"total"}, name => "Fetching Last.fm library", ETA => "linear" } );
my $libTracks = {};
while ( my $libTrack = $libResponse->() ) {
my $artist = $libTrack->{"artist"}->{"name"};
my $track = $libTrack->{"name"};
$libTracks->{lc $artist}->{lc $track}->{"playcount"} = $libTrack->{"playcount"};
$libProgress->update($libCounter++);
}
#
# Fetch loved tracks list
#
if ( $self->updateloved ) {
say "Fetching Last.fm loved tracks..." if $self->debug;
my $lovedResponse = undef;
my $lovedCounter = 0;
try {
$lovedResponse = lastfm_iter(
"user.getLovedTracks",
user => $self->username,
limit => 250
);
};
if ( !$lovedResponse ) {
say "Error: Could not fetch Last.fm loved tracks.";
exit(1);
}
my $lovedProgress = Term::ProgressBar->new( { count => $Net::LastFMAPI::last_response->{"lovedtracks"}->{"\@attr"}->{"total"}, name => "Fetching Last.fm loved tracks", ETA => "linear" } );
while ( my $libTrack = $lovedResponse->() ) {
my $artist = $libTrack->{"artist"}->{"name"};
my $track = $libTrack->{"name"};
$libTracks->{lc $artist}->{lc $track}->{"loved"} = 1;
$lovedProgress->update($lovedCounter++);
}
}
my $progress = Term::ProgressBar->new( { count => $total, name => "checked", ETA => "linear" } );
$dbh->do('BEGIN TRANSACTION');
while( my $row = $sth->fetchrow_hashref ) {
my $trackInfo = defined $row->{ "Name" } && defined $row->{ "Title" } ?
$libTracks->{ lc $row->{ "Name" } }->{ lc $row->{ "Title" } }
: '';
#
# check if there is match
#
if( ref $trackInfo eq "HASH" ) {
my $track_changed = 0;
my $setFields = '';
my $lastfm_count = $trackInfo->{ "playcount"};
unless( is_int( $lastfm_count ) ) {
$lastfm_count = 0;
}
my $lastfm_loved = $trackInfo->{ "loved"};
unless ( is_int( $lastfm_loved) ) {
$lastfm_loved = 0;
}
say "Found track $counter (${\$row->{TrackID}}) with ${ \$row->{ Name }}:${\$row->{ Title }}, play count: ${\$row->{ PlayCount}}, rating: ${\$row->{ TrackRating}}" if $self->debug;
say "Last.fm match is ${\$row->{Name}}:${\$row->{Title}}, play count: $lastfm_count, loved: $lastfm_loved" if $self->debug;
#
# compare playcount
#
if( $row->{ "PlayCount"} < $lastfm_count ) {
say "\tBanshee play count lower than last.fm play count: ${\$row->{ PlayCount}} < $lastfm_count" if $self->debug;
#
#
#
$setFields = $setFields . sprintf( ($setFields ? ", " : '') . "PlayCount = %d",
$lastfm_count
);
$track_changed = 1;
}
#
# add 5 stars for a loved track if rating not set
#
if ( $lastfm_loved eq 1 && $row->{ "TrackRating"} eq 0 ) {
say "\tBanshee unrated song is loved" if $self->debug;
$setFields = $setFields . sprintf( ($setFields ? ", " : '') . "Rating = %d",
$self->lovedrating
);
}
if ( $setFields ) {
print "\tUpdating track... " if $self->debug;
#
#
#
$dbh->do( sprintf("UPDATE CoreTracks SET ${setFields} WHERE TrackID = %d",
$row->{ "TrackID"}
) );
$track_changed = 1;
say "done." if $self->debug;
}
if ( $track_changed eq 1 ) {
$changed++;
}
say "done." if $self->debug;
}
$progress->update( $counter++ );
}
$dbh->do('COMMIT TRANSACTION');
$dbh->disconnect();
say "Updated $changed tracks of $total total tracks";
}
no Moose;
1;
package main;
use common::sense;
=pod cmndline
--key=b25b959554ed76058ac220b7b2e0a026 --username=<lastfm-username>
=cut
my $client = Banshee::LastFm::Sync->new_with_options();
$client->backup();
$client->sync;
1;