-
Notifications
You must be signed in to change notification settings - Fork 1
/
telvue_airing_import.module
428 lines (364 loc) · 11.9 KB
/
telvue_airing_import.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
/**
* Implements hook_permission
*/
function telvue_airing_import_permission() {
return array(
'administer telvue airing import' => array(
'title' => t('Administer Telvue Airing Import'),
'description' => t('Provides access to change Telvue Airing Import Settings')
));
}
/**
* Implements hook_menu().
*/
function telvue_airing_import_menu() {
$items = array();
$items['admin/config/telvue-airing-import'] = array(
'title' => 'Telvue Airing Import',
'description' => 'Settings for Telvue Airing Import.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/telvue-airing-import/feeds'] = array(
'title' => 'Telvue Airing Import',
'description' => 'Settings for Telvue Airing Import',
'page callback' => 'drupal_get_form',
'page arguments' => array('telvue_airing_import_settings_form'),
'access arguments' => array('administer telvue airing import'),
'file' => 'telvue_airing_import.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/config/telvue-airing-import/delete-all'] = array(
'title' => 'Telvue Airing Import Delete All',
'description' => 'Deletes all airing entities and related field data.',
'page callback' => 'drupal_get_form',
'page arguments' => array('telvue_airing_import_delete_all_form'),
'access arguments' => array('administer telvue airing import'),
'file' => 'telvue_airing_import.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/config/telvue-airing-import/import-all'] = array(
'title' => 'Telvue Airing Import: Import All',
'description' => 'Imports All Airings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('telvue_airing_import_import_all_form'),
'access arguments' => array('administer telvue airing import'),
'file' => 'telvue_airing_import.admin.inc',
'type' => MENU_CALLBACK,
);
/*$items['telvue-airing-import/test'] = array(
'page callback' => 'telvue_airing_import_test',
'access callback' => TRUE,
);*/
return $items;
}
/*function telvue_airing_import_test() {
$url = 'http://mc.retn.org/xml/program_schedule_feed?channel_id=1';
// Init curl and pull in rss feed.
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
$xml = curl_exec($curl);
$channel = new SimpleXMLElement($xml);
$namespaces = $channel->getNamespaces(true);
foreach($channel->xpath('//item') as $item) {
// Get data in psg namespace
$psg = $item->children($namespaces['psg']);
$data[] = array(
'guid' => (string) $item->{'guid'},
'title' => (string) $item->{'title'},
'link' => (string) $item->{'link'},
'description' => (string) $item->{'description'},
'pubDate' => (string) $item->{'pubDate'},
'end_datetime' => (string) $psg->end_datetime[0],
);
}
dpm((string) $channel->channel->title);
//dpm($data);
// Close curl
curl_close($curl);
return 'telvue_airing_import_test';
}
*/
/**
* Implements hook_cronapi($op, $job = NULL).
*/
function telvue_airing_import_cronapi($op, $job = NULL) {
$items = array();
// Check if feed 1 url has a value, other set to false.
$feed_1 = variable_get('telvue_airing_import_xml_feed_url_1', FALSE);
// Only add cron task if feed 1 has a value.
// @todo confirm that this is a good idea/stable approach.
if ($feed_1) {
$items['telvue_airing_import_feed_1'] = array(
'description' => 'Telvue Airing Import Feed 1',
'rule' => '0 */3 * * *',
'callback' => '_telvue_airing_import_feed_1',
'weight' => 1,
);
}
// Feed 2
$feed_2 = variable_get('telvue_airing_import_xml_feed_url_2', FALSE);
if ($feed_2) {
$items['telvue_airing_import_feed_2'] = array(
'description' => 'Telvue Airing Import Feed 2',
'rule' => '0 */3 * * *',
'callback' => '_telvue_airing_import_feed_2',
'weight' => 1,
);
}
// Feed 3
$feed_3 = variable_get('telvue_airing_import_xml_feed_url_3', FALSE);
if ($feed_3) {
$items['telvue_airing_import_feed_3'] = array(
'description' => 'Telvue Airing Import Feed 3',
'rule' => '0 */3 * * *',
'callback' => '_telvue_airing_import_feed_3',
'weight' => 1,
);
}
// Feed 4
$feed_4 = variable_get('telvue_airing_import_xml_feed_url_4', FALSE);
if ($feed_4) {
$items['telvue_airing_import_feed_4'] = array(
'description' => 'Telvue Airing Import Feed 4',
'rule' => '0 */3 * * *',
'callback' => '_telvue_airing_import_feed_4',
'weight' => 1,
);
}
return $items;
}
/**
* Feed 1
*/
function _telvue_airing_import_feed_1() {
$url = variable_get('telvue_airing_import_xml_feed_url_1', FALSE);
if ($url) {
_telvue_airing_import_process_feed($url);
}
}
/**
* Feed 2
*/
function _telvue_airing_import_feed_2() {
$url = variable_get('telvue_airing_import_xml_feed_url_2', FALSE);
if ($url) {
_telvue_airing_import_process_feed($url);
}
}
/**
* Feed 3
*/
function _telvue_airing_import_feed_3() {
$url = variable_get('telvue_airing_import_xml_feed_url_3', FALSE);
if ($url) {
_telvue_airing_import_process_feed($url);
}
}
/**
* Feed 4
*/
function _telvue_airing_import_feed_4() {
$url = variable_get('telvue_airing_import_xml_feed_url_4', FALSE);
if ($url) {
_telvue_airing_import_process_feed($url);
}
}
/**
* Process feed function.
*/
function _telvue_airing_import_process_feed($url) {
// @todo should add something validates the RSS feed and rejects invalid feeds?
$data = _telvue_airing_import_get_feed($url);
if ($data) {
foreach($data as $item) {
// Check if airing exists
if (!$airing_exists = _telvue_airing_import_airing_exists($item['guid'])) {
$airing = _telvue_airing_import_create_airing($item/*, $channel*/);
if ($airing) {
// Log success to watchdogg
watchdog(
$type = 'telvue_airing_import',
$message = 'Success! Created airing for GUID: %guid',
$variables = array('%guid' => $item['guid']),
$severity = WATCHDOG_INFO
);
}
else {
// Log failure to watchdog.
watchdog(
$type = 'telvue_airing_import',
$message = 'Failure! Could not create airing for GUID: %guid',
$variables = array('%guid' => $item['guid']),
$severity = WATCHDOG_ALERT
);
}
}
else {
// Log duplicate attemp
watchdog(
$type = 'telvue_airing_import',
$message = 'Airing already exists for GUID: %guid',
$variables = array('%guid' => $item['guid']),
$severity = WATCHDOG_INFO
);
}
}
}
}
/**
* Helper function to get feed.
*/
function _telvue_airing_import_get_feed($url) {
// @todo add some logic to determine if we have a valid source.
// Init curl and pull in rss feed.
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
$xml = curl_exec($curl);
$channel = new SimpleXMLElement($xml);
$namespaces = $channel->getNamespaces(true);
// Get title for use later to match taxonomy term
$channel_name = (string) $channel->channel->title;
//foreach($channel->channel->item as $item) {
foreach($channel->xpath('//item') as $item) {
// Get data in psg namespace
$psg = $item->children($namespaces['psg']);
$data[] = array(
'guid' => (string) $item->{'guid'},
'title' => (string) $item->{'title'},
'link' => (string) $item->{'link'},
'description' => (string) $item->{'description'},
'pubDate' => (string) $item->{'pubDate'},
'end_datetime' => (string) $psg->end_datetime[0],
'episode' => (string) $psg->episode,
'channel_name' => $channel_name,
);
}
// Close curl
curl_close($curl);
return $data;
}
/**
* Helper function to check if airing exists, based on GUID field.
*/
function _telvue_airing_import_airing_exists($guid) {
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'airing')
->fieldCondition('field_airing_guid', 'value', $guid, '=');
$result = $query->execute();
if (isset($result['airing'])) {
$aid = array_keys($result['airing']);
return $aid;
}
else {
return FALSE;
}
}
/**
* Helper function to create airing entity.
*/
function _telvue_airing_import_create_airing($item/*, $channel*/) {
$airing = new stdClass();
$airing->type = 'airing';
$airing->uid = 1;
$airing->field_airing_title[LANGUAGE_NONE][]['value'] = $item['title'];
$airing->field_airing_guid[LANGUAGE_NONE][]['value'] = $item['guid'];
// Process Date
// Shouldn't the cm_airing just handle this in cm_airing_entity_presave() ?
//$airing->field_airing_date[LANGUAGE_NONE][]['value'] = $item['pubDate'];
//$airing->field_airing_date[LANGUAGE_NONE][]['value2'] = $item['end_datetime'];
// Special processing for start date
$mod_start_date = strtotime($item['pubDate']);
$airing->field_airing_date[LANGUAGE_NONE][]['value'] = date("Y-m-d H:i:s",$mod_start_date);
// Special processing for end date
$mod_end_date = strtotime($item['end_datetime']);
// Delta needs to be 0 for value2 or "end date"
$airing->field_airing_date[LANGUAGE_NONE][0]['value2'] = date("Y-m-d H:i:s",$mod_end_date);
// General date field
$airing->field_airing_date[LANGUAGE_NONE][]['timezone'] = 'UTC';
$airing->field_airing_date[LANGUAGE_NONE][]['timezone_db'] = 'UTC';
$airing->field_airing_date[LANGUAGE_NONE][]['offset'] = 0;
$airing->field_airing_date[LANGUAGE_NONE][]['offset2'] = 0;
// Match "episode" to a cm_show
$cm_show_nid = _telvue_airing_import_find_show_by_episode($item['episode']);
if ($cm_show_nid) {
$airing->field_airing_show_ref[LANGUAGE_NONE][]['target_id'] = $cm_show_nid;
}
// No airing show node match, add episode as plain text to field: field_no_reference_show
else {
$airing->field_no_reference_show[LANGUAGE_NONE][]['value'] = $item['episode'];
}
// Match "title" to a series (cm_project)
$cm_project_nid = _telvue_airing_import_find_series_by_title($item['title']);
if ($cm_project_nid) {
$airing->field_airing_project_ref[LANGUAGE_NONE][]['target_id'] = $cm_project_nid;
}
// No series node match, so populate field_no_reference_project with title.
else {
$airing->field_no_reference_project[LANGUAGE_NONE][]['value'] = $item['title'];
}
// Match channel name to taxonomy term id
$channel_tid = _telvue_airing_import_find_tid_by_channel($item['channel_name']);
if ($channel_tid) {
$airing->field_airing_channel[LANGUAGE_NONE][]['tid'] = $channel_tid;
}
// Save airing and return status
// We don't use cm_airing_save() b/c it doesn't return a value
// and it also doesn't really do anything.
if (entity_save('airing', $airing)) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Helper function to find show by title.
*/
function _telvue_airing_import_find_show_by_episode($episode) {
$nid = db_query("SELECT nid FROM {node} WHERE type = 'cm_show' AND title = :title AND status = 1",
array(
':title' => $episode)
)->fetchField();
if (!empty($nid)) {
return $nid;
}
else {
return FALSE;
}
}
/**
* Helper function to find show by title.
*/
function _telvue_airing_import_find_series_by_title($title) {
$nid = db_query("SELECT nid FROM {node} WHERE type = 'cm_project' AND title = :title AND status = 1",
array(
':title' => $title)
)->fetchField();
if (!empty($nid)) {
return $nid;
}
else {
return FALSE;
}
}
/**
* Helper function to find channel tid from channel name
*/
function _telvue_airing_import_find_tid_by_channel($channel_name) {
$tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name",
array(
':name' => $channel_name)
)->fetchField();
if (!empty($tid)) {
return $tid;
}
else {
return FALSE;
}
}