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