| 1 | package WWW::PSN; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | @EXPORT = qw/profile trophies/; | |||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use HTTP::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use constant ORIGIN => 'https://www.playstation.com'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use constant IO => 'https://io.playstation.com'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use constant URL_USER_DATA => '/playstation/psn/profile/public/userData'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use constant URL_TROPHIES_API => '/playstation/psn/public/trophies/'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use constant URL_TROPHIES_PAGE => '/en-us/my/public-trophies/'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | 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 | ▶ | . ' Chrome/51.0.2704.103 Safari/537.36'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | sub profile { | ||||||||||
| 22 | 1 | ▶ | my $id = shift; | ||||||||
|
Subroutine
profile: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 23 | 1 | return psn_get( URL_USER_DATA, $id ); | |||||||||
| 24 | |||||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | sub trophies { | ||||||||||
| 28 | 0 | ▶ | my $id = shift; | ||||||||
|
Subroutine
trophies: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 29 | 0 | return psn_get( URL_TROPHIES_API, $id ); | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | sub psn_get { | ||||||||||
| 33 | 1 | ▶ | my ( $url, $id ) = @_; | ||||||||
|
Subroutine
psn_get: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 34 | 1 | my $headers = { | |||||||||
| 35 | 'User-Agent' => UA_OSX_CHROME, | ||||||||||
| 36 | 'Referer' => ORIGIN . URL_TROPHIES_PAGE, | ||||||||||
| 37 | }; | ||||||||||
| 38 | |||||||||||
| 39 | 1 | $url = IO . $url . "?onlineId=$id"; | |||||||||
| 40 | 1 | my $response = HTTP::Tiny->new->get( $url, { 'headers' => $headers } ); | |||||||||
| 41 | 1 | ▶ | die "Unable to open >$url<. :(\n" unless $response->{success}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 1 | my $json_data = $response->{content}; | |||||||||
| 43 | 1 | $json_data =~ s/^\s+|\s+$//g; | |||||||||
| 44 | 1 | return JSON::decode_json($json_data); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 1 | 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<WWW::PSN> 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 <zshengli@cpan.org> | ||||||||||
| 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__ | ||||||||||