File Coverage

blib/lib/WWW/PSN.pm
Criterion Covered Total %
statement 40 45 88.8
branch 1 2 50.0
condition n/a
subroutine 13 14 92.8
pod 2 3 66.6
total 56 64 87.5


line stmt bran cond sub pod time code
1             package WWW::PSN;
2              
3 1     1   292 use Exporter 'import';
  1         2  
  1         36  
4             @EXPORT = qw/profile trophies/;
5              
6 1     1   3 use strict;
  1         1  
  1         15  
7 1     1   3 use warnings;
  1         5  
  1         19  
8 1     1   562 use HTTP::Tiny;
  1         39005  
  1         32  
9 1     1   547 use JSON;
  1         9157  
  1         4  
10              
11 1     1   117 use constant ORIGIN => 'https://www.playstation.com';
  1         1  
  1         40  
12 1     1   3 use constant IO => 'https://io.playstation.com';
  1         2  
  1         37  
13 1     1   3 use constant URL_USER_DATA => '/playstation/psn/profile/public/userData';
  1         1  
  1         31  
14 1     1   3 use constant URL_TROPHIES_API => '/playstation/psn/public/trophies/';
  1         2  
  1         30  
15 1     1   3 use constant URL_TROPHIES_PAGE => '/en-us/my/public-trophies/';
  1         2  
  1         38  
16 1         187 use constant UA_OSX_CHROME =>
17             'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5)'
18             . ' AppleWebKit/537.36 (KHTML, like Gecko)'
19 1     1   3 . ' Chrome/51.0.2704.103 Safari/537.36';
  1         1  
20              
21             sub profile {
22 1     1 1 14 my $id = shift;
23 1         2 return psn_get( URL_USER_DATA, $id );
24              
25             }
26              
27             sub trophies {
28 0     0 1 0 my $id = shift;
29 0         0 return psn_get( URL_TROPHIES_API, $id );
30             }
31              
32             sub psn_get {
33 1     1 0 2 my ( $url, $id ) = @_;
34 1         2 my $headers = {
35             'User-Agent' => UA_OSX_CHROME,
36             'Referer' => ORIGIN . URL_TROPHIES_PAGE,
37             };
38              
39 1         4 $url = IO . $url . "?onlineId=$id";
40 1         6 my $response = HTTP::Tiny->new->get( $url, { 'headers' => $headers } );
41 1 50         die "Unable to open >$url<. :(\n" unless $response->{success};
42 0           my $json_data = $response->{content};
43 0           $json_data =~ s/^\s+|\s+$//g;
44 0           return JSON::decode_json($json_data);
45             }
46              
47             1;
48              
49             =pod
50              
51             =encoding UTF-8
52              
53             =head1 NAME
54              
55             WWW::PSN - Perl Module for fetching PSN profile and trophy data.
56              
57             =head1 VERSION
58              
59             version 0.01
60              
61             =head1 SYNOPSIS
62              
63             use WWW::PSN;
64             my $profile = profile('hajimuc'); # replace hajimuc with your PSN ID.
65             print "Current Level: $profile->{'curLevel'}\n";
66              
67             # print all the titles
68             my $detailed_trophies = trophies('hajimuc');
69             for my $t ( @{$detailed_trophies->{list}} ) {
70             print encode('utf-8',$t->{title}), "\n";
71             }
72              
73             =head1 DESCRIPTION
74              
75             C is Perl Module for fetching PSN profile and trophy data.
76              
77             =head1 METHODS
78              
79             =head2 profile($psn_id)
80              
81             User profile including overall trophy progress and numbers.
82              
83             {
84             "isPlusUser" : "1",
85             "curLevel" : "4",
86             "handle" : "hajimuc",
87             "progress" : "90",
88             "avatarUrl" : "//static-resource.np.community.playstation.net/avatar_m/WWS_J/J0003_m.png",
89             "trophies" : {
90             "bronze" : "98",
91             "silver" : "15",
92             "gold" : "4",
93             "platinum" : "0"
94             },
95             "totalLevel" : ""
96             }
97              
98             =head2 trophies($psn_id)
99              
100             Detailed trophies data.
101              
102             {
103             "overallprogress" : "90",
104             "isPlusUser" : "1",
105             "avatarUrl" : "//static-resource.np.community.playstation.net/avatar_m/WWS_J/J0003_m.png",
106             "handle" : "hajimuc",
107             "curLevel" : "4",
108             "totalResults" : "15",
109             "list" : [
110             {
111             "title" : "Far Cry® Primal",
112             "imgUrl" : "//trophy01.np.community.playstation.net/trophy/np/NPWR09687_00_0090890723F98AD458C3F4EC288C1888A48F880D21/08B0C827B453E9D45ED0DCBE0CB6FFA59BFFD53E.PNG",
113             "progress" : 85,
114             "platform" : "ps4",
115             "gameId" : "NPWR09687_00",
116             "trophies" : {
117             "silver" : 9,
118             "bronze" : 30,
119             "gold" : 2,
120             "platinum" : 0
121             }
122             }
123             ]
124             }
125              
126             =head1 AUTHOR
127              
128             Zhu Sheng Li
129              
130             =head1 COPYRIGHT AND LICENSE
131              
132             This software is Copyright (c) 2016 by Zhu Sheng Li.
133              
134             This is free software, licensed under:
135              
136             The MIT (X11) License
137              
138             =cut
139              
140             __END__