/
www-spotify
78 lines (66 loc) · 2.08 KB
/
www-spotify
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
#!/usr/bin/env perl
use strict;
use warnings;
# Lookup stuff on spotify
# For my case, this is an album image
# Write-up: https://ology.github.io/2022/10/16/using-www-spotify/
# settings.yml example:
# oauth_client_id: abcdefghijklmnopqrstuvwxyz
# oauth_client_secret: 12345678901234567890
use Data::Dumper::Compact qw(ddc);
use Getopt::Long qw(GetOptions);
use WWW::Spotify ();
use YAML::XS qw(LoadFile);
my %opts = (
artist => undef,
album => undef,
track => undef,
config => 'settings.yml',
limit => 10,
max => 50,
);
GetOptions( \%opts,
'config=s',
'artist=s',
'album=s',
'track=s',
'limit=i',
'max=i',
);
die qq/Usage: perl $0 --artist="David Bowie" [--album="Blackstar"|--track="Lazarus"] [--config="settings.yml"]\n/
unless $opts{artist};
my $cfg = LoadFile($opts{config});
my $spotify = WWW::Spotify->new;
$spotify->oauth_client_id($cfg->{oauth_client_id});
$spotify->oauth_client_secret($cfg->{oauth_client_secret});
$spotify->force_client_auth(1);
$spotify->auto_json_decode(1);
my $key = $opts{track} ? 'track' : $opts{album} ? 'album' : 'artist';
my $keys = $key . 's';
my @query = ($opts{$key}, $key);
my ($limit, $max) = @opts{ qw(limit max) };
BATCH: for (my $i = 0; $i <= $max; $i += $limit) {
my $batch = $max - $i < $limit ? $max - $i + 1 : $limit;
warn "offset=$i, limit=$batch\n";
my $result = $spotify->search(
@query,
{ limit => $batch, offset => $i }
);
for my $item ($result->{$keys}{items}->@*) {
if ($key eq 'artist' && $item->{name} eq $opts{artist}) {
print 'Genres: ', join(', ', $item->{genres}->@*), "\n",
"Image: $item->{images}[1]{url}\n";
last BATCH;
}
elsif ($item->{name} =~ /^$opts{$key}\b/
&& defined $item->{artists}[0]{name}
&& $item->{artists}[0]{name} eq $opts{artist}
) {
my $val = $key eq 'track'
? $item->{album}{images}[1]{url}
: $item->{images}[1]{url};
print "Image: $val\n";
last BATCH;
}
}
}