Skip to content

Commit 086c4ca

Browse files
author
krun
committed
Code
0 parents  commit 086c4ca

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-0
lines changed

CanvasStats.php

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
class CanvasStats {
3+
private $user;
4+
private $all_stickers = array();
5+
private $stickercount = 0;
6+
private $numposts = 0;
7+
private $pointscount = 0;
8+
private $api_calls = 0;
9+
private $error = FALSE;
10+
11+
private static $weight = array("fuckyeah" => 150,
12+
"number-oneocle" => 100,
13+
"glove" => 50,
14+
"super-lol" => 30,
15+
"tacnayn" => 25,
16+
"nyancat" => 25,
17+
"hipster" => 20,
18+
"kawaii" => 15,
19+
"forever-alone" => 10,
20+
"banana" => 5,
21+
"cool" => 5 );
22+
23+
const perpage = 100; #number of posts returned for each call to canvas API
24+
const user_api = 'https://canv.as/public_api/users/';
25+
const post_api = 'https://canv.as/public_api/posts/';
26+
const api_limit_message = "Slow down there, cowboy!";
27+
28+
const error_undefined = "There was an unexpected failure";
29+
const error_api_limit = "We have reached API request limit :(";
30+
31+
/**
32+
* Class constructor
33+
*
34+
* This will construct an object of class CanvasStats and load user stats
35+
*/
36+
37+
public function __construct($name) {
38+
$this->user = $name;
39+
$skip = 0;
40+
while(true){
41+
$data_string = '{"ids": [{"skip": '.$skip.', "user": "'.$this->user.'"}]}';
42+
$ch = curl_init(self::user_api);
43+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
44+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
45+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
46+
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
47+
'Content-Type: application/json',
48+
'Content-Length: ' . strlen($data_string))
49+
);
50+
51+
$result = curl_exec($ch);
52+
$this->api_calls++;
53+
curl_close($ch);
54+
$res = json_decode($result,true);
55+
if ($res["success"] == false){
56+
$this->error = self::error_undefined;
57+
if ($res["reason"] == self::api_limit_message) {
58+
$this->error = self::error_api_limit;
59+
}
60+
}
61+
62+
$posts = $res["users"][0]["posts"];
63+
$posts_page = array();
64+
if (count($posts) > 0) {
65+
$skip += self::perpage;
66+
$this->numposts += count($posts);
67+
} else {
68+
break;
69+
}
70+
foreach ($posts as $post) {
71+
$posts_page[] = $post["id"];
72+
}
73+
74+
$postids = json_encode($posts_page);
75+
76+
$data_string = '{"ids": '.$postids.'}';
77+
$ch = curl_init(self::post_api);
78+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
79+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
80+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
81+
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
82+
'Content-Type: application/json',
83+
'Content-Length: ' . strlen($data_string))
84+
);
85+
86+
$result = curl_exec($ch);
87+
$this->api_calls++;
88+
curl_close($ch);
89+
$res = json_decode($result,true);
90+
foreach($res["posts"] as $post){
91+
$stickers = $post["stickers"];
92+
foreach ($stickers as $sticker){
93+
$this->all_stickers[$sticker["name"]] += $sticker["count"];
94+
}
95+
}
96+
97+
}
98+
foreach($this->all_stickers as $sticker) {
99+
$this->stickercount += $sticker;
100+
}
101+
102+
foreach($this->all_stickers as $name => $value) {
103+
$this->pointscount += $value*self::weight($name);
104+
}
105+
}
106+
/**
107+
* getAllStickers()
108+
*
109+
* This method returns a list with detailed sticker count.
110+
*
111+
* Return value is an array of the form [(str)sticker_type] => (int)amount
112+
*/
113+
public function getAllStickers() {
114+
return $this->all_stickers;
115+
}
116+
117+
/**
118+
* getNumStickers()
119+
*
120+
* Returns total number of stickers for the user
121+
*/
122+
public function getNumStickers() {
123+
return $this->stickercount;
124+
}
125+
126+
/**
127+
* getNumPosts()
128+
*
129+
* Returns total number of posts for the user
130+
*/
131+
public function getNumPosts() {
132+
return $this->numposts;
133+
}
134+
135+
/**
136+
* getPoints()
137+
*
138+
* Returns total number of points
139+
*/
140+
public function getPoints() {
141+
return $this->pointscount;
142+
}
143+
144+
/**
145+
* getAvgPoints()
146+
*
147+
* Returns average points per post
148+
*/
149+
public function getAvgPoints() {
150+
return round($this->getPoints()/$this->getNumPosts(),2);
151+
}
152+
153+
/**
154+
* getApiCalls()
155+
*
156+
* Returns number of calls made to Canv.as API
157+
*/
158+
public function getApiCalls() {
159+
return $this->api_calls;
160+
}
161+
162+
/**
163+
* error()
164+
*
165+
* FALSE if user stats were loaded correctly
166+
* Error message otherwise
167+
*/
168+
public function error() {
169+
return $this->error;
170+
}
171+
172+
/**
173+
* weight($type)
174+
*
175+
* Returns number of points for the given sticker type.
176+
*
177+
* Example: weight("fuck-yeah") = 150.
178+
*/
179+
public static function weight($type) {
180+
if (isset(self::$weight[$type])){
181+
return self::$weight[$type];
182+
} else {
183+
return 1;
184+
}
185+
}
186+
}
187+
188+
?>

example.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<html>
2+
<head>
3+
<title>Canv.as stats example</title>
4+
</head>
5+
<body>
6+
<?php
7+
if (!isset($_GET["user"])) { ?>
8+
<form method="GET">
9+
<input type="text" name="user" placeholder="Canvas username">
10+
<input type="submit">
11+
</form>
12+
<?php
13+
} else {
14+
$user = htmlspecialchars($_GET["user"]);
15+
include("CanvasStats.php");
16+
$canvas_stats = new CanvasStats($user);
17+
if ($canvas_stats->error()){
18+
die($canvas_stats->error());
19+
}
20+
echo "<h1>Sticker list:</h1>\r\n";
21+
echo "<ul>";
22+
foreach($canvas_stats->getAllStickers() as $type => $amount) {
23+
echo "<li><b>$type:</b> $amount</li>\r\n";
24+
}
25+
echo "</ul>\r\n\r\n";
26+
echo "<h1>Additional stats:</h1>\r\n";
27+
echo "<p><b>Number of stickers:</b> ".$canvas_stats->getNumStickers()."</p>\r\n";
28+
echo "<p><b>Number of posts:</b> ".$canvas_stats->getNumPosts()."</p>\r\n";
29+
echo "<p><b>Total points:</b> ".$canvas_stats->getPoints()."</p>\r\n";
30+
echo "<p><b>Average points per post:</b> ".$canvas_stats->getAvgPoints()."</p>\r\n";
31+
echo "<p><i>A total of ".$canvas_stats->getApiCalls()." calls to Canv.as api were needed to get these stats.</i></p>\r\n";
32+
33+
}?>
34+
35+
</body>
36+
</html>

0 commit comments

Comments
 (0)