-
Notifications
You must be signed in to change notification settings - Fork 0
/
social_stats.module
380 lines (344 loc) · 13.5 KB
/
social_stats.module
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* @file
* File containing the implemented hooks.
*/
/**
* Implementation of hook_help().
*/
function social_stats_help($path, $arg) {
$help_text = '';
switch ($path) {
case 'admin/help#social_stats':
$help_text = '<p>' . t('This module provides an API to get the social statistics of the nodes of the selected content types. The data includes number of shares of a particular node on Facebook, Twitter, LinkedIn, Google Plus. This module does a collection of data on cron run. Using modules like Elysia Cron this can be set to some convinient time (for e.g. once or twice a day).') . '</p><p>' . t('Use Social Stats Views module for integration of this data in the Views module : like sorting according to the number of shares, using the number of shares as a field, having a filter criteria with this data, etc.') . '</p>';
break;
}
return $help_text;
}
/**
* Implementation of hook_menu().
*/
function social_stats_menu() {
$items['admin/settings/services/social-stats'] = array(
'title' => 'Social Stats',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'drupal_get_form',
'page arguments' => array('social_stats_general_settings_form'),
'access arguments' => array('administer social stats content types'),
'file' => 'social_stats.admin.inc',
);
$items['admin/settings/services/social-stats/general'] = array(
'title' => 'Social Stats',
'description' => 'Administrative settings for social stats module',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/settings/services/social-stats/content-types'] = array(
'title' => 'Cron Settings',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array('social_stats_content_types_form'),
'access arguments' => array('administer social stats cron'),
'file' => 'social_stats.admin.inc',
'weight' => 1,
);
return $items;
}
/**
* Implementation of hook_perm().
*/
function social_stats_perm() {
return array('administer social stats content types', 'administer social stats cron');
}
/**
* Fetch the data from Facebook and save it to local table.
*
* @param array $node
* Contains $node->nid.
* @param string $node_path
* The URL alias.
*/
function _social_stats_facebook($node, $node_path = '') {
$fql_query = 'SELECT share_count, like_count, commentsbox_count, total_count ';
$fql_query .= "FROM link_stat WHERE url='" . $node_path . "'";
// TODO: Implement multi-query
// https://developers.facebook.com/docs/technical-guides/fql/
$fql_queries = array('query1' => $fql_query);
$params = array(
'q' => json_encode($fql_queries),
'format' => 'json',
);
$request_url = 'https://graph.facebook.com/fql?' . http_build_query($params);
$fql_response = drupal_http_request($request_url);
if (!empty($fql_response->error)) {
watchdog(t('Social Stats'),
'Problem updating data from Facebook for %node_path. Error: %err',
array('%node_path' => $node_path, '%err' => $fql_response->error),
NULL, WATCHDOG_ERROR);
}
else {
$fql_data = json_decode($fql_response->data);
foreach ($fql_data->data as $fql_data_row) {
$facebook_data = $fql_data_row->fql_result_set;
// Only update table if counter > 0
if (intval($facebook_data[0]->total_count)) {
$fb_data = $facebook_data[0];
$result = db_query('SELECT * FROM {social_stats_facebook} WHERE nid = ' . $node->nid);
if (!db_result($result)) {
$sql = 'INSERT INTO {social_stats_facebook} (nid, fb_likes, fb_shares, fb_comments, fb_total) VALUES('. $node->nid .', '. $fb_data->like_count .', '. $fb_data->share_count .', '. $fb_data->commentsbox_count .', '. $fb_data->total_count .')';
}
else {
$sql = 'UPDATE {social_stats_facebook} SET fb_likes = ' . $fb_data->like_count . ', fb_shares = ' . $fb_data->share_count . ', fb_comments = ' . $fb_data->commentsbox_count . ', fb_total = ' . $fb_data->total_count . ' WHERE nid = ' . $node->nid;
}
db_query($sql);
return $facebook_data[0]->total_count;
}
return 0;
}
}
}
/**
* Fetch the data from Twitter and save it to local table.
*
* @param array $node
* Contains $node->nid.
* @param string $node_path
* The URL alias.
*/
function _social_stats_twitter($node, $node_path = '') {
$twitter_shares = 0;
$tweets_query = 'http://urls.api.twitter.com/1/urls/count.json?url=';
$tweets_query .= $node_path;
$tweets_response = drupal_http_request($tweets_query);
if (!empty($tweets_response->error)) {
watchdog(t('Social Stats'),
'Problem updating data from Twitter for %node_path. Error: %err',
array('%node_path' => $node_path, '%err' => $tweets_response->error),
NULL, WATCHDOG_ERROR);
}
else {
$tweets_data = json_decode($tweets_response->data);
// Only update table if counter > 0
$twitter_shares = intval($tweets_data->count);
if ($twitter_shares) {
$result = db_query('SELECT * FROM {social_stats_twitter} WHERE nid = ' . $node->nid);
if (!db_result($result)) {
$sql = 'INSERT INTO {social_stats_twitter} (nid, tweets) VALUES(' . $node->nid . ', ' . $twitter_shares . ')';
}
else {
$sql = 'UPDATE {social_stats_twitter} SET tweets = ' . $twitter_shares . ' WHERE nid = ' . $node->nid;
}
db_query($sql);
}
}
return $twitter_shares;
}
/**
* Fetch the data from LinkedIn and save it to local table.
*
* @param array $node
* Contains $node->nid.
* @param string $node_path
* The URL alias.
*/
function _social_stats_linkedin($node, $node_path = '') {
$linkedin_shares = 0;
// Write a query to fetch the data from LinkedIn.
$linkedin_query = 'http://www.linkedin.com/countserv/count/share?';
$linkedin_query .= 'format=json&url=' . $node_path;
$linkedin_response = drupal_http_request($linkedin_query);
if (!empty($linkedin_response->error)) {
watchdog(t('Social Stats'),
'Problem updating data from LinkedIn for %node_path. Error: %err',
array('%node_path' => $node_path, '%err' => $linkedin_response->error),
NULL, WATCHDOG_ERROR);
}
else {
$linkedin_data = json_decode($linkedin_response->data);
// Only update table if counter > 0
$linkedin_shares = intval($linkedin_data->count);
if ($linkedin_shares) {
$result = db_query('SELECT * FROM {social_stats_linkedin} WHERE nid = ' . $node->nid);
if (!db_result($result)) {
$sql = 'INSERT INTO {social_stats_linkedin} (nid, linkedin_shares) VALUES(' . $node->nid . ', ' . $linkedin_shares . ')';
}
else {
$sql = 'UPDATE {social_stats_linkedin} SET linkedin_shares = ' . $linkedin_shares . ' WHERE nid = ' . $node->nid;
}
db_query($sql);
}
}
return $linkedin_shares;
}
/**
* Fetch the data from Google+ and save it to local table.
*
* @param array $node
* Contains $node->nid.
* @param string $node_path
* The URL alias.
*/
function _social_stats_googleplus($node, $node_path = '') {
$gplus_like_count = _social_stats_googleplus_plusone($node_path);
$gplus_share_count = _social_stats_googleplus_share($node_path);
$gplus_count = $gplus_like_count + $gplus_share_count;
// Only update table if counter > 0
if ($gplus_count) {
$result = db_query('SELECT * FROM {social_stats_gplus} WHERE nid = ' . $node->nid);
if (!db_result($result)) {
$sql = 'INSERT INTO {social_stats_gplus} (nid, plusone, share, total) VALUES(' . $node->nid .', ' . $gplus_like_count . ', ' . $gplus_share_count . ', ' . $gplus_count . ')';
}
else {
$sql = 'UPDATE {social_stats_gplus} SET plusone = ' . $gplus_like_count . ', share = ' . $gplus_share_count . ', total = ' . $gplus_count . ' WHERE nid = ' . $node->nid;
}
db_query($sql);
}
return $gplus_count;
}
/**
* Function returning the number of times the node was +1ed.
*/
function _social_stats_googleplus_plusone($node_path) {
$gplus_like_count = 0;
// Build the JSON data for the API request.
$data['method'] = 'pos.plusones.get';
$data['id'] = 'p';
$data['params']['nolog'] = TRUE;
$data['params']['id'] = $node_path;
$data['params']['source'] = 'widget';
$data['params']['userId'] = '@viewer';
$data['params']['groupId'] = '@self';
$data['jsonrpc'] = '2.0';
$data['key'] = 'p';
$data['apiVersion'] = 'v1';
$url = 'https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ';
$options['data'] = json_encode($data);
$options['method'] = 'POST';
$options['headers']['Content-Type'] = 'application/json';
$request = drupal_http_request($url, $options);
if (!empty($request->error) || empty($request->data)) {
watchdog(t('Social Stats'),
'Problem updating data from Google+ for %node_path. Error: %err',
array('%node_path' => $node_path, '%err' => $request->error),
NULL, WATCHDOG_ERROR);
}
else {
$request->data = json_decode($request->data);
if (isset($request->data->result->metadata->globalCounts->count)) {
$gplus_like_count = intval($request->data->result->metadata->globalCounts->count);
}
}
return $gplus_like_count;
}
/**
* Function returning the number of times the node was shared.
*/
function _social_stats_googleplus_share($node_path) {
$gplus_share_count = 0;
$data = "f.req=%5B%22" . $node_path . "%22%2Cnull%5D&";
$url = "https://plus.google.com/u/0/ripple/update";
$response = drupal_http_request($url, array(
'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
'data' => $data,
'method' => 'POST',
));
$response->data = substr($response->data, 6);
$response->data = str_replace(",,", ",null,", $response->data);
$response->data = str_replace(",,", ",null,", $response->data);
$result = json_decode($response->data, TRUE);
$gplus_share_count = $result[0][1][4];
return $gplus_share_count;
}
/**
* The queue worker function to update the data.
*/
function _social_stats_update($result) {
$facebook_total = 0;
$twitter_shares = 0;
$linkedin_shares = 0;
$google_plusone = 0;
$variable = variable_get('social_stats_' . $result->type, array(0));
// Create absolute node path using the node path.
$node_path = url('node/' . $result->nid, array('absolute' => TRUE));
// Getting data from Facebook for nodes of the selected node type.
if ($variable['Facebook']) {
$facebook_total = _social_stats_facebook($result, $node_path);
}
// Getting data from Twitter for nodes of selected node type.
if ($variable['Twitter']) {
$twitter_shares = _social_stats_twitter($result, $node_path);
}
// Getting data from LinkedIn for nodes of selected node type.
if ($variable['LinkedIn']) {
$linkedin_shares = _social_stats_linkedin($result, $node_path);
}
// Getting data from Google Plus for nodes of selected node type.
if ($variable['Google Plus']) {
$google_plusone = _social_stats_googleplus($result, $node_path);
}
$count_total = $facebook_total + $twitter_shares;
$count_total += $linkedin_shares + $google_plusone;
// Adding the total data from all above to get total virality.
// Update table only when counter > 0
if ($count_total) {
$query_result = db_query('SELECT * FROM {social_stats_total} WHERE nid = ' . $result->nid);
if (!db_result($query_result)) {
$sql = 'INSERT INTO {social_stats_total} (nid, total_virality) VALUES(' . $result->nid . ', ' . $count_total . ')';
}
else {
$sql = 'UPDATE {social_stats_total} SET total_virality = ' . $count_total . ' WHERE nid = ' . $result->nid;
}
db_query($sql);
}
}
/**
* Implementation of hook_cron().
*/
function social_stats_cron() {
$interval = variable_get('social_stats_cron_interval', 60 * 60 * 24);
if (time() >= variable_get('social_stats_next_execution', 0)) {
variable_set('social_stats_next_execution', time() + $interval);
// Get list of content types in the site.
$node_types = node_get_types();
$content_types = array();
foreach ($node_types as $types) {
$variable = variable_get('social_stats_' . $types->type, array(0));
if ($variable['Facebook'] ||
$variable['Twitter'] ||
$variable['Google Plus'] ||
$variable['LinkedIn']) {
$content_types[] = $types->type;
}
}
$start_date = '';
$social_stats_options = variable_get('social_stats_options', 0);
if ($social_stats_options == 0) {
$start_date = variable_get('social_stats_start_date', '01/01/1970');
}
elseif ($social_stats_options == 1) {
$start_date = variable_get('social_stats_date_offset', '-100 days');
}
$start_date = strtotime($start_date);
// Fetch node id and type for the content types,
// which has at least one social media selected and
// which are created after the date mentioned in module's configuration.
if (!empty($content_types)) {
$query = db_query(db_rewrite_sql("SELECT n.nid, n.type FROM {node} n WHERE n.type IN (" . db_placeholders($content_types, 'text') . ")"), $content_types);
while ($row = db_fetch_array($query)) {
_social_stats_update($row);
}
}
}
}
/**
* Implementation of hook_node_delete().
*/
function social_stats_nodeapi(&$node, $op) {
// Delete all the social data on node deletion.
if ($op == 'delete') {
db_query('DELETE FROM {social_stats_facebook} WHERE nid = ' . $node->nid);
db_query('DELETE FROM {social_stats_twitter} WHERE nid = ' . $node->nid);
db_query('DELETE FROM {social_stats_linkedin} WHERE nid = ' . $node->nid);
db_query('DELETE FROM {social_stats_gplus} WHERE nid = ' . $node->nid);
db_query('DELETE FROM {social_stats_total} WHERE nid = ' . $node->nid);
}
}