-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbball.php
296 lines (240 loc) · 8.69 KB
/
bbball.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
* bbball
* A Simple, Yet Powerful Dribbble PHP Client
*
* @author Ethan Kramer <[email protected]>
* @package A Dribbble PHP Client
* @copyright Ethan Kramer 2012
* @see http://github.com/ekdevdes/bbball
* @link http://dribbble.com/api
*
*/
class BBBall {
/**
* Dribbbble api base url
*
* @var string
*/
var $api_base = "http://api.dribbble.com/";
/**
* Page of results to request from dribbbble api
*
* @var int
*/
var $page = 1;
/**
* Number of results to retrieve per page
*
* @var int
*/
var $per_page = 15;
/**
* Gets a single shot from dribbbble
* @access public
*
* @param int $id The shots id. [Required] [Default: none]
* @param bool $show_rebounds If true will include the given shot's (the shot is specified by the $id param) rebounds [Optional][Default: false]
* @param bool $show_comments If true will include the given shot's (the shot is specified by the $id param) comments [Optional][Default: false]
*
* @return array the parsed form of the resulting JSON
*/
public function get_shot($id,$show_rebounds=false,$show_comments=false){
$url_shot = $this->api_base."shots/".(string)$id;
$url_rebounds = "";
$url_comments = "";
if ($show_rebounds) {
$url_rebounds .= $this->api_base."shots/".(string)$id."/rebounds";
}
if($show_comments){
$url_comments .= $this->api_base."shots/".(string)$id."/comments";
}
if (!$show_rebounds && !$show_comments) {
/**
* Default, both $show_comments and $show_rebounds are false
*/
$result = objectToArray(json_decode($this->get_contents($url_shot)));
return $result;
}else if(!$show_rebounds && $show_comments){
/**
* Don't $show_rebounds but $show_comments
*/
$result_shot = objectToArray(json_decode($this->get_contents($url_shot)));
$result_comments = objectToArray(json_decode($this->get_contents($url_comments)));
$result_shot['comments'] = $result_comments['comments'];
return $result_shot;
}else if($show_rebounds && $show_comments){
/**
* $show_rebounds and $show_comments
*/
$result_shot = objectToArray(json_decode($this->get_contents($url_shot)));
$result_comments = objectToArray(json_decode($this->get_contents($url_comments)));
$result_rebounds = objectToArray(json_decode($this->get_contents($url_rebounds)));
$result_shot['comments'] = $result_comments['comments'];
$result_shot['rebounds'] = $result_rebounds;
return $result_shot;
}else if($show_rebounds && !$show_comments){
/**
* $show_rebounds but not $show_comments
*/
$result_shot = objectToArray(json_decode($this->get_contents($url_shot)));
$result_rebounds = objectToArray(json_decode($this->get_contents($url_rebounds)));
$result_shot['rebounds'] = $result_rebounds;
return $result_shot;
}
}
/**
* Gets shots from dribbble with optionally, a few filters
* @access public
*
* @param string $list The list to fetch the shots from. Either "debuts","everyone","popular","following" or "likes" [Optional][Default: "everyone"]. For "following" or "likes"
* the $player_id must be not NULL
* @param int $player_id A given player's id. If not present then will it will be ignored. If present $list will be ignored. [Optional][Default: none]
* NOTE: In order to specify the "following" or "likes" options the $player_id must not be NULL
* NOTE: $player_id can either the players actual id or their userame without the '@' in the begining
*
* @return array the parsed form of the resulting JSON
*/
public function get_shots($list="everyone",$player_id=NULL){
if (is_null($player_id) && $list == "following" || is_null($player_id) && $list == "likes") {
throw new Exception(" Player id can't be null when requesting shots from the \"$list\" filter.");
}
if ($list != "following" && $list != "likes") {
switch ($list) {
case 'everyone':
return objectToArray(json_decode($this->get_contents($this->api_base."shots/".$list."?page=".$this->page."&per_page=".$this->per_page)));
break;
case 'debuts':
return objectToArray(json_decode($this->get_contents($this->api_base."shots/".$list."?page=".$this->page."&per_page=".$this->per_page)));
break;
case 'popular':
return objectToArray(json_decode($this->get_contents($this->api_base."shots/".$list."?page=".$this->page."&per_page=".$this->per_page)));
break;
}
}else if($list == "following" || $list == "likes"){
switch ($list){
case "following":
return objectToArray(json_decode($this->get_contents($this->api_base."players/".$player_id."/shots/following?page=".$this->page."&per_page=".$this->per_page)));
break;
case "likes":
return objectToArray(json_decode($this->get_contents($this->api_base."players/".$player_id."/shots/likes?page=".$this->page."&per_page=".$this->per_page)));
break;
}
}
}
/**
* Get's a players info given a player id.
* @access public
*
* @param int|string $id The player's id [Required][Default: none]. $id can either be the users actual id or their username
*
* @return array the parsed form of the resulting JSON
*/
public function get_player_info($id){
if (is_null($id)) {
throw new Exception("Can't get player info without a player id");
}else{
$base_url = $this->api_base."/players/".$id;
$following_url = $this->api_base."/players/".$id."/following?page=".$this->page."&per_page=".$this->per_page;
$followers_url = $this->api_base."/players/".$id."/followers?page=".$this->page."&per_page=".$this->per_page;
$draftees_url = $this->api_base."/players/".$id."/draftees?page=".$this->page."&per_page=".$this->per_page;
$base_info = objectToArray(json_decode($this->get_contents($base_url)));
$following_info = objectToArray(json_decode($this->get_contents($following_url)));
$followers_info = objectToArray(json_decode($this->get_contents($followers_url)));
$draftees_info = objectToArray(json_decode($this->get_contents($draftees_url)));
$base_info['followers'] = $followers_info['players'];
$base_info['following'] = $following_info['players'];
$base_info['draftees'] = $draftees_info['players'];
return $base_info;
}
}
/**
* Returns only a given shot's rebounds
* @access public
*
* @param int $id the given shot's id
*
* @return array the given shot's (specified by the $id) rebounds
*
*/
public function get_shot_rebounds($id){
if (is_null($id)) {
throw new Exception("Can't get shot rebounds without a shot id");
}else{
return objectToArray(json_decode($this->get_contents($this->api_base."/shots/$id/rebounds?page=".$this->page."&per_page=".$this->per_page)));
}
}
/**
* Returns a given players shots
* @access public
*
* @param int|string $id the given players id
*
* @return array the of the given player's (specified by $id) shots
*
*/
public function get_player_shots($id){
if(is_null($id)){
throw new Exception("Can't get shots without aa shot id");
}else{
return objectToArray(json_decode($this->get_contents($this->api_base."/players/$id/shots?page=".$this->page."&per_page=".$this->per_page)));
}
}
/**
* Returns only a given shot's comments
* @access public
*
* @param int $id the given shot's id
*
* @return array the given shot's (specified by the $id) comments
*
*/
public function get_shot_comments($id){
if (is_null($id)) {
throw new Exception("Can't get shot comments without a shot id");
}else{
return objectToArray(json_decode($this->get_contents($this->api_base."/shots/$id/comments?page=".$this->page."&per_page=".$this->per_page)));
}
}
/**
* Return the content of a url using curl
* @access private
*
* @param string $url the url which the get the contents from
*
* @return string a string representation the contents of the url
*
*/
private function get_contents($url){
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}
/**
*
* Convert an object to an array
* @access public
*
* @param Object $object The object to convert
*
* @return Array an array representation of $object
*
*/
function objectToArray( $object )
{
if( !is_object( $object ) && !is_array( $object ) )
{
return $object;
}
if( is_object( $object ) )
{
$object = get_object_vars( $object );
}
return array_map( 'objectToArray', $object );
}