-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocial_ag.module
268 lines (220 loc) · 7.42 KB
/
social_ag.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
<?php
/**
* @file
* Code for the social_ag feature.
*/
include_once 'social_ag.features.inc';
function social_ag_init(){
drupal_add_css(drupal_get_path('module', 'social_ag') . '/social-ag.css');
drupal_add_js(drupal_get_path('module', 'social_ag') .'/social-ag.js');
}
function social_ag_menu(){
$items['social-ag'] = array(
'title' => "Social Ag Test",
'access arguments' => array("access content"),
'page callback' => 'social_ag_run',
'type' => MENU_CALLBACK,
);
$items['admin/structure/social-ag'] = array(
'title' => 'Social Media Aggregator Settings',
'description' => t('The settings for the social media aggregator'),
'page callback' => 'drupal_get_form', // assuming that you want to use a form
'page arguments' => array('social_ag_settings'),
'access callback' => 'user_access',
'access arguments' => array('moderate social media'), // or use hook_perm() to make your own
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function social_ag_delete_all(){
$query = new EntityFieldQuery;
$result = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'post')
->execute();
$nids = array_keys($result['node']);
node_delete_multiple($nids);
dsm($nids);
return '';
}
function social_ag_permission(){
return array(
'moderate social media' => array(
'title' => t('Moderate Social Media'),
'description' => t('Perform tasks necessary to moderate social media.'),
),
);
}
function social_ag_cron(){
$x = social_ag_run();
watchdog("social_ag","$x posts saved!");
}
function social_ag_run(){
$x = 0;
$posts = social_ag_tw_retrieve();
foreach($posts as $post){
if(social_ag_create($post)){ $x++; }
}
$posts = social_ag_fb_retrieve();
foreach($posts as $post){
if(social_ag_create($post)){ $x++; }
}
return strval($x);
}
function social_ag_tw_retrieve(){
$twitter_acct = variable_get('social_ag_twitter_account', '');
if($twitter_acct <> ''){
$url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name='. urlencode($twitter_acct) .'&include_rts=true&count=50';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
$results = curl_exec($ch);
$results = json_decode($results);
$posts = array();
foreach($results as $entry){
$body = preg_replace('/[^a-zA-Z0-9_ %#@\/\:\[\]\.\(\)%&-]/s', '', $entry->text);
$posts[] = array(
'id' => $entry->id,
'title' => $body,
'source_name' => 'Twitter',
'source_link' => "https://twitter.com/" . $entry->user->screen_name . "/status/" . $entry->id,
'body' => $body,
'user_name' => $entry->user->name,
'user_link' => "https://twitter.com/" . $entry->user->screen_name,
'user_photo' => $entry->user->profile_image_url,
'timestamp' => strtotime($entry->created_at),
);
}
return $posts;
}
}
function social_ag_fb_retrieve(){
$fb_acct = variable_get('social_ag_facebook_page_id', '');
if($fb_acct <> ''){
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => '291519234281965',
'secret' => 'ad1e4a0669676c3696b1dba86eeeea03',
));
$results = $facebook->api($fb_acct . '/feed');
dsm($results);
$posts = array();
foreach($results['data'] as $key => $entry){
//$body = preg_replace('/[^a-zA-Z0-9_ %#@\/\:\[\]\.\(\)%&-]/s', '', $entry->content);
if($entry['message'] <> "" || $entry['name'] <> ""){
$posts[$key] = array(
'id' => $entry['id'],
'title' => $entry['name'],
'source_name' => 'Facebook',
'source_link' => 'http://fb.com/' . $entry['id'],
'user_name' => $entry['from']['name'],
'user_link' => 'http://fb.com/' . $entry['from']['id'],
'user_photo' => "https://graph.facebook.com/" . $entry['from']['id'] . "/picture",
'timestamp' => strtotime($entry['created_time']),
);
}
if($entry['message'] <> ""){
$posts[$key]['body'] = $entry['message'];
}else{
$posts[$key]['body'] = $entry['name'];
}
}
return $posts;
}
}
/*********
// Post Array Example
$post = array(
'id' => 'string',
'title' => 'string',
'source_name' => 'string',
'source_link' => 'http://',
'location' => array('lat','long'),
'body' => 'string',
'user_name' => 'string',
'user_photo' => 'http://',
'timestamp' => 000,
);
**********/
function social_ag_create($post){
$result = db_select('field_data_field_post_id','c')
->fields('c')
->condition('field_post_id_value',check_plain($post['id']),"=")
->condition('bundle','post',"=")
->execute()
->fetchAssoc();
if(!isset($result['field_post_id_value'])){
$node = new stdClass();
$node->status = 0;
$node->language = LANGUAGE_NONE;
$node->title = substr($post['body'], 0, 100);
$node->type = 'post';
$node->body[LANGUAGE_NONE][0]['value'] = $post['body'];
$node->field_post_id[LANGUAGE_NONE][0]['value'] = check_plain($post['id']);
$node->field_source_name[LANGUAGE_NONE][0]['value'] = $post['source_name'];
$node->field_source_link[LANGUAGE_NONE][0]['url'] = $post['source_link'];
$node->field_date[LANGUAGE_NONE][0]['value'] = date("Y-m-d H:i:s", $post['timestamp']);
$node->field_date[LANGUAGE_NONE][0]['value2'] = date("Y-m-d H:i:s", $post['timestamp']);
$node->field_date[LANGUAGE_NONE][0]['timezone'] = "America/Chicago";
$node->field_date[LANGUAGE_NONE][0]['timezone_db'] = "UTC";
$node->field_user_name[LANGUAGE_NONE][0]['value'] = $post['user_name'];
$node->field_user_image[LANGUAGE_NONE][0]['url'] = $post['user_photo'];
$node->field_user_link[LANGUAGE_NONE][0]['url'] = $post['user_link'];
preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $post['body'], $result);
if(count($result[0])){
$node->field_preview_link[LANGUAGE_NONE][0]['url'] = check_plain($result[0][0]);
$node->field_preview_thumb[LANGUAGE_NONE][0]['url'] = social_ag_preview($result[0][0]);
}
node_save($node);
return true;
}
}
function social_ag_preview($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
$doc = new DOMDocument();
@$doc->loadHTML($data);
foreach( $doc->getElementsByTagName('meta') as $meta ) {
$metadata[$meta->getAttribute('property')] = $meta->getAttribute('content');
}
if(isset($metadata['og:image'])){
return $metadata['og:image'];
}else{
return "";
}
}
function social_ag_settings(){
$form = array();
$form['social_ag_twitter_hashtag'] = array(
'#type' => 'textfield',
'#title' => t('Twitter Hashtag'),
'#size' => 60,
'#maxlength' => 128,
'#required' => FALSE,
'#default_value' => variable_get('social_ag_twitter_hashtag', ''),
);
$form['social_ag_twitter_account'] = array(
'#type' => 'textfield',
'#title' => t('Twitter Account Name'),
'#size' => 60,
'#maxlength' => 128,
'#required' => FALSE,
'#default_value' => variable_get('social_ag_twitter_account', ''),
);
$form['social_ag_facebook_page_id'] = array(
'#type' => 'textfield',
'#title' => t('Twitter Facebook Page ID'),
'#size' => 60,
'#maxlength' => 128,
'#required' => FALSE,
'#default_value' => variable_get('social_ag_facebook_page_id', ''),
);
return system_settings_form($form);
}