summaryrefslogtreecommitdiffstats
path: root/lib/LastFM/Export.pm
blob: 8847020d7ed8d3264d9929bc017507d3f6f3ca61 (plain) (blame)
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
package LastFM::Export;
use Moose;
# ABSTRACT: data exporter for last.fm

use Data::Stream::Bulk::Callback;
use Net::LastFM;

=head1 SYNOPSIS

  use LastFM::Export;

  my $exporter = LastFM::Export->new(user => 'doyster');
  my $stream = $exporter->tracks;

  while (my $block = $stream->next) {
      for my $track (@$block) {
          # ...
      }
      sleep 1;
  }

=head1 DESCRIPTION

This module uses the L<http://last.fm/> API to allow you to export your
scrobbling data from your account. Currently, the only thing this lets you
export is your actual scrobble data, but more features may be added in the
future (especially if the feature requests come with patches!).

=cut

=attr user

last.fm user to export data for. Required.

=cut

has user => (
    is       => 'ro',
    isa      => 'Str',
    required => 1,
);

has api_key => (
    is       => 'ro',
    isa      => 'Str',
    default  => '30b55f2e2e78056b16dbb15cb0899c2d',
);

has lastfm => (
    is      => 'ro',
    isa     => 'Net::LastFM',
    lazy    => 1,
    default => sub {
        my $self = shift;
        Net::LastFM->new(
            api_key    => $self->api_key,
            api_secret => '',
        );
    },
);

=method track_count(%params)

Returns the number of tracks the user has scrobbled.

C<%params> can contain C<from> and C<to> keys, as documented
L<here|http://www.last.fm/api/show/user.getRecentTracks>.

=cut

sub track_count {
    my $self = shift;
    my (%params) = @_;

    $params{method} = 'user.getRecentTracks';
    $params{user}   = $self->user;
    $params{limit}  = 1;

    return $self->lastfm->request(%params)->{recenttracks}{'@attr'}{total};
}

=method tracks(%params)

Returns a L<Data::Stream::Bulk> object, which will stream the entire list of
tracks that the user has scrobbled. Note that calling C<all> on this object is
B<not> recommended, since you will likely hit the last.fm API's rate limit.
Each call to C<next> on this stream will require a separate API call.

C<%params> can contain C<page>, C<limit>, C<from>, and C<to> keys, as
documented L<here|http://www.last.fm/api/show/user.getRecentTracks>. C<page>
will default to C<1> and C<limit> will default to C<200> if not specified.

Returns

=cut

sub tracks {
    my $self = shift;
    my (%params) = @_;

    $params{method}   = 'user.getRecentTracks';
    $params{user}     = $self->user;
    $params{limit}  ||= 200;
    $params{page}   ||= 1;

    return Data::Stream::Bulk::Callback->new(
        callback => sub {
            my $data = $self->lastfm->request(%params);

            return if $params{page} > $data->{recenttracks}{'@attr'}{totalPages};
            $params{page}++;

            return $data->{recenttracks}{track};
        },
    );
}

__PACKAGE__->meta->make_immutable;
no Moose;

=head1 BUGS

No known bugs.

Please report any bugs to GitHub Issues at
L<https://github.com/doy/lastfm-export/issues>.

=head1 SEE ALSO

L<Net::LastFM>

L<http://last.fm/>

=head1 SUPPORT

You can find this documentation for this module with the perldoc command.

    perldoc LastFM::Export

You can also look for information at:

=over 4

=item * MetaCPAN

L<https://metacpan.org/release/LastFM-Export>

=item * Github

L<https://github.com/doy/lastfm-export>

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=LastFM-Export>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/LastFM-Export>

=back

=cut

1;