-
Notifications
You must be signed in to change notification settings - Fork 0
/
spnn_tv_schedule.module
241 lines (208 loc) · 6.05 KB
/
spnn_tv_schedule.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
<?php
/**
* Implements hook_theme_registry_alter(&$theme_registry).
*
* Fixes issue w/ schdules needing different TPL based on site.
*/
function spnn_tv_schedule_theme_registry_alter(&$theme_registry) {
$path = drupal_get_path('module', 'spnn_tv_schedule') . '/templates/cm_tv_schedules/';
$theme_registry['calendar_day']['theme path'] = drupal_get_path('module', 'spnn_tv_schedule');
$theme_registry['calendar_day']['path'] = $path;
}
/**
* Implements hook_cronapi($op, $job = NULL).
*/
function spnn_tv_schedule_cronapi($op, $job = NULL) {
$items = array();
/*
* CHANNEL KEY *
CH14 [ col_no=1 ]
CH15 [ col_no=2 ]
CH16 [ col_no=3 ]
CH19 [ col_no=4 ]
*/
// Channel 14
$items['spnn_tv_schedule_process_ch_14'] = array(
'description' => 'Channel 14 RSS Feed',
'rule' => '0 */3 * * *',
'callback' => '_spnn_tv_schedule__channel_14',
'weight' => 1,
);
// Channel 15
$items['spnn_tv_schedule_process_ch_15'] = array(
'description' => 'Channel 15 RSS Feed',
'rule' => '0 */3 * * *',
'callback' => '_spnn_tv_schedule__channel_15',
'weight' => 2,
);
// Channel 16
$items['spnn_tv_schedule_process_ch_16'] = array(
'description' => 'Channel 16 RSS Feed',
'rule' => '0 */3 * * *',
'callback' => '_spnn_tv_schedule__channel_16',
'weight' => 3,
);
// Channel 19
$items['spnn_tv_schedule_process_ch_19'] = array(
'description' => 'Channel 19 RSS Feed',
'rule' => '0 */3 * * *',
'callback' => '_spnn_tv_schedule__channel_19',
'weight' => 4,
);
return $items;
}
/**
* Channel 14 Feed
*/
function _spnn_tv_schedule__channel_14() {
_spnn_tv_schedule_process($channel = 'col_no=1');
}
/**
* Channel 15 Feed
*/
function _spnn_tv_schedule__channel_15() {
_spnn_tv_schedule_process($channel = 'col_no=2');
}
/**
* Channel 16 Feed
*/
function _spnn_tv_schedule__channel_16() {
_spnn_tv_schedule_process($channel = 'col_no=3');
}
/**
* Channel 19 Feed
*/
function _spnn_tv_schedule__channel_19() {
_spnn_tv_schedule_process($channel = 'col_no=4');
}
/**
* Cron callback function.
*/
function _spnn_tv_schedule_process($channel) {
// @todo should add something validates the RSS feed and rejects invalid feeds?
$data = spnn_tv_schedule_get_rss_feed($channel);
if ($data) {
foreach($data as $item) {
// Check if airing exists
if (!$airing_exists = spnn_tv_schedule_airing_exists($item['guid'])) {
$airing = spnn_tv_schedule_create_airing($item, $channel);
if ($airing) {
// Log success to watchdogg
watchdog(
$type = 'spnn_tv_schedule',
$message = 'Success! Created airing for GUID: %guid',
$variables = array('%guid' => $item['guid']),
$severity = WATCHDOG_INFO
);
}
else {
// Log failure to watchdog.
watchdog(
$type = 'spnn_tv_schedule',
$message = 'Failure! Could not create airing for GUID: %guid',
$variables = array('%guid' => $item['guid']),
$severity = WATCHDOG_ALERT
);
}
}
else {
// Log duplicate attemp
watchdog(
$type = 'spnn_tv_schedule',
$message = 'Airing already exists for GUID: %guid',
$variables = array('%guid' => $item['guid']),
$severity = WATCHDOG_INFO
);
}
}
}
}
/**
* Helper function to get RSS feed.
*/
function spnn_tv_schedule_get_rss_feed($channel) {
// Get current timesstamp
$timestamp = time();
$month = date('n', $timestamp);
$day = date('j', $timestamp);
$year = date('Y', $timestamp);
// Build URL
$url = 'http://pw.myersinfosys.com/spnn/day.rss?month=' . $month . '&day=' . $day . '&year=' . $year . '&' . $channel;
// 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);
foreach($channel->channel->item as $item) {
$data[] = array(
'guid' => (string) $item->{'guid'},
'title' => (string) $item->{'title'},
'link' => (string) $item->{'link'},
'description' => (string) $item->{'description'},
'pubDate' => (string) $item->{'pubDate'},
);
}
// Close curl
curl_close($curl);
return $data;
}
/**
* Helper function to check if airing exists, based on GUID field.
*/
function spnn_tv_schedule_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 spnn_tv_schedule_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
// Why tho?
$mod_start_date = strtotime($item['pubDate']) + 60*60;
$airing->field_airing_date[LANGUAGE_NONE][]['value'] = date("Y-m-d H:i:s",$mod_start_date);
$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;
// Channel
switch ($channel) {
case 'col_no=1':
$term_id = 1846;
break;
case 'col_no=2':
$term_id = 1847;
break;
case 'col_no=3':
$term_id = 1848;
break;
case 'col_no=4':
$term_id = 1849;
break;
}
$airing->field_airing_channel[LANGUAGE_NONE][]['tid'] = $term_id;
// 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;
}
}