-
Notifications
You must be signed in to change notification settings - Fork 3
/
calculate.php
241 lines (209 loc) · 6.82 KB
/
calculate.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
<?php
class com_meego_planet_calculate
{
private static function prepare_return($value, $modifier)
{
return (float) $value * $modifier;
}
public static function delicious($url, $modifier = 1)
{
$json = @file_get_contents('http://badges.del.icio.us/feeds/json/url/data?hash=' . md5($url));
if (empty($json))
{
return self::prepare_return(0, $modifier);
}
$item_data = json_decode($json);
if (!isset($item_data[0]->total_posts))
{
return self::prepare_return(0, $modifier);
}
return self::prepare_return($item_data[0]->total_posts, $modifier);
}
public static function twitter($url, $modifier = 1)
{
$json = @file_get_contents('http://search.twitter.com/search.json?q=' . urlencode($url));
if (empty($json))
{
return self::prepare_return(0, $modifier);
}
$item_data = json_decode($json);
if (!isset($item_data->results))
{
return self::prepare_return(0, $modifier);
}
$total_count = 0;
while (isset($item_data->next_page))
{
$total_count += count($item_data->results);
$json = @file_get_contents('http://search.twitter.com/search.json' . $item_data->next_page);
if (empty($json))
{
$item_data = (object) array('results' => array());
break;
}
$item_data = json_decode($json);
}
$total_count += count($item_data->results);
return self::prepare_return($total_count, $modifier);
}
public static function facebook($url, $modifier = 1)
{
$fql = 'SELECT total_count from link_stat where url="' . rawurlencode($url) . '"';
$json = @file_get_contents('http://api.facebook.com/method/fql.query?format=json&query=' . urlencode($fql));
if (empty($json))
{
return self::prepare_return(0, $modifier);
}
$item_data = json_decode($json);
if (!is_array($item_data))
{
return self::prepare_return(0, $modifier);
}
if ( !isset($item_data[0])
|| !isset($item_data[0]->total_count))
{
return self::prepare_return(0, $modifier);
}
return self::prepare_return($item_data[0]->total_count, $modifier);
}
public static function hackernews($url, $modifier = 1)
{
$json = @file_get_contents('http://api.ihackernews.com/getid?url=' . urlencode($url));
$score = 0;
if (empty($json))
{
return self::prepare_return($score, $modifier);
}
$post_ids = json_decode($json);
if ( empty($post_ids)
|| !is_array($post_ids))
{
return self::prepare_return($score, $modifier);
}
foreach ($post_ids as $post_id)
{
$json = file_get_contents('http://api.ihackernews.com/post/' . $post_id);
if (empty($json))
{
continue;
}
$post = json_decode($json);
if (isset($post->commentCount))
{
$score = $score + $post->commentCount;
}
if (isset($post->points))
{
$score = $score + $post->points;
}
}
return self::prepare_return($score, $modifier);
}
public static function buzz($url, $modifier = 1)
{
$json = @file_get_contents('https://www.googleapis.com/buzz/v1/activities/count?alt=json&url=' . urlencode($url));
if (empty($json))
{
return self::prepare_return(0, $modifier);
}
$item_data = json_decode($json);
if ( !isset($item_data->data)
|| !isset($item_data->data->counts->$url))
{
return self::prepare_return(0, $modifier);
}
foreach ($item_data->data->counts->$url as $counts)
{
return self::prepare_return($counts->count, $modifier);
}
}
public static function votes_for($url, $modifier = 1)
{
if (!class_exists('midgard_query_select'))
{
return self::prepare_return(0, $modifier);
}
try
{
$item = com_meego_planet_utils::get_item($url);
}
catch (Exception $e)
{
return self::prepare_return(0, $modifier);
}
$votes = com_meego_planet_votes::get($item);
return self::prepare_return($votes['1'], $modifier);
}
public static function votes_against($url, $modifier = 1)
{
if (!class_exists('midgard_query_select'))
{
return self::prepare_return(0, $modifier);
}
try
{
$item = com_meego_planet_utils::get_item($url);
}
catch (Exception $e)
{
return self::prepare_return(0, $modifier);
}
$votes = com_meego_planet_votes::get($item);
return self::prepare_return($votes['-1'], $modifier);
}
public static function reddit($url, $modifier = 1)
{
$json = @file_get_contents('http://www.reddit.com/api/info.json?url='.urlencode($url));
if (empty($json))
{
return self::prepare_return(0, $modifier);
}
$item_data = json_decode($json);
if (!isset($item_data->data))
{
return self::prepare_return(0, $modifier);
}
$sum = 0;
foreach ($item_data->data->children as $record) {
$sum += $record->data->score;
$sum += $record->data->num_comments;
}
return self::prepare_return($sum, $modifier);
}
public function age(DateTime $published, $penalty = 0.1)
{
$article_age = round((time() - $published->getTimestamp()) / 3600);
return self::prepare_return(-$article_age, $penalty);
}
public static function all($url)
{
$services = array
(
'facebook' => 0.5,
'delicious' => 0.5,
'twitter' => 0.6,
'hackernews' => 0.7,
'buzz' => 0.6,
'reddit' => 0.5,
'votes_for' => 0.7,
'votes_against' => -5,
);
return (float) array_reduce
(
array_map
(
function ($service, $modifier) use ($url)
{
return call_user_func(__CLASS__ . "::{$service}", $url, $modifier);
},
array_keys($services),
$services
),
function ($x, $y)
{
return $x + $y;
},
0.0
);
}
}