|
| 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 | +?> |
0 commit comments