Subversion Repository Public Repository

Nextrek

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
<?php

function println($data)
{
	$msg = Date('Ymd G:s').' '.$data;
	echo $msg."\n";
	error_log($msg);
	flush();
	ob_flush();
}

function get_page($url)
{
	// create curl resource
	$ch = curl_init();

	// set url
	curl_setopt($ch, CURLOPT_URL, $url);

	//return the transfer as a string
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

	// $output contains the output string
	$output = curl_exec($ch);

	// close curl resource to free up system resources
	curl_close($ch);

	return $output;
}

function get_attribute($tag, $name)
{
	$tag_lower = strtolower($tag);
	$name_lower = strtolower($name);
	$inizio = strpos($tag_lower, $name_lower);
	if ($inizio === FALSE)
	{
		//error_log($tag . ' | ' . $name . ' | non trovato');
		return FALSE; // non trovato
	}

	$inizio = strpos($tag, '"', $inizio);
	if ($inizio === FALSE)
	{
		//error_log($tag . ' | ' . $name . ' | non trovato "');
		return FALSE; // non trovato
	}
	$inizio++;

/*
	$inizio = strpos($tag, '=', $inizio);
*/
	$fine = strpos($tag, '"', $inizio);

	//error_log($tag . '|' . $inizio . '|' . $fine);

	return substr($tag, $inizio, $fine-$inizio);
}

function get_tag($div, $name, &$cursore)
{
	$div_lower = strtolower($div);
	$name_lower = strtolower($name);

	$label = '<'.$name_lower;
	$inizio = strpos($div_lower, $label, $cursore);
	if ($inizio === FALSE) {
		return FALSE;
	}

	$inizio+=strlen($label);
	$inizio = strpos($div_lower, '>', $inizio);
	$inizio++;

	$label = '</'.$name_lower;
	$fine = strpos($div_lower, $label, $inizio);

	$cursore = $fine+strlen($label);

	return trim(substr($div, $inizio, $fine-$inizio));
}

function get_channels_epg_source(&$main_page)
{
	$result = array();

	$inizio = '<strong>Televisione Digitale Terrestre</strong>';
	$inizio = strpos($main_page, $inizio);
	if ($inizio === FALSE)
	{
		// ERRORE!
		println('non trovo inizio sezione Digitale Terrestre');
		return FALSE;
	}

	$fine = '</div>';
	$fine = strpos($main_page, $fine, $inizio);
	if ($fine === FALSE)
	{
		println('non trovo fine sezione Digitale Terrestre');
		// ERRORE!
		return FALSE;
	}

	$sezione = substr($main_page, $inizio, $fine-$inizio);
	$cursore = 0;

	while (TRUE)
	{
		//error_log('cerco link');
		$inizio_link = '<a';
		$inizio_link = strpos($sezione, $inizio_link, $cursore);
		if ($inizio_link === FALSE)
		{
			//error_log('finito');
			break;
		}

		$fine_link = '</a>';
		$fine_link = strpos($sezione, $fine_link, $inizio_link);
		if ($fine_link === FALSE)
		{
			//error_log('non trovo fine link');
			// ERRORE!
			break;
		}

		$link = substr($sezione, $inizio_link, $fine_link-$inizio_link);
		//error_log($link);
		// <a href="" title="" > stringa
		$channel_name = get_attribute($link, 'title');
		$result[$channel_name] = array(
			'name' => $channel_name,
			'epg_url' => get_attribute($link, 'href')
		);

		$cursore = $fine_link;
	}

	return $result;
}

function get_channel_id(&$epg_page)
{
	$label_inizio = 'var_id_canale=';
	$inizio = strpos($epg_page, $label_inizio);
	if ($inizio === FALSE)
	{
		println('id canale non trovato');
		return FALSE;
	}
	$inizio+=strlen($label_inizio);

	$fine = strpos($epg_page, '&', $inizio);

	return substr($epg_page, $inizio, $fine-$inizio);
}

function leva_tag_esterni($div)
{
	$inizio = strpos($div, '>');
	if ($inizio === FALSE) {
		return $div;
	}
	$inizio++;

	$fine = strrpos($div, '</');

	return trim(substr($div, $inizio, $fine-$inizio));
}

function parse_programma(&$div)
{
	$div = trim($div);
	$div = str_replace("\n",'',$div);
	$div = str_replace("\r",'',$div);
	$div = leva_tag_esterni($div);

	$result = array();

	/*
	 * Un scheda programma può essere strutturata in maniera diversa dipendentemente dal tipo di programma:
	 * 1) solo Titolo e Descrizione per programmi generici
	 * 2) Scheda Film
	 * 3) Scheda SerieTV
	 * 4) Partita di Calcio
	 * etc..
	 *
	 * Fortunatamente gli elementi principali sono <h2> per il titolo e <h3> per la descrizione del tipo di programma
	 */
	$pattern = '{<div[^>]*>((?:(?:(?!<div[^>]*>|</div>).)++|<div[^>]*>(?1)</div>)*)</div>}si';
	preg_match_all($pattern, $div, $matches);

	$matches = $matches[0];
	if (count($matches) > 2)
	{
		$div = leva_tag_esterni($matches[2]);


/*
		$cursore = 0;

		// se c'è questo tag probabilmente è il logo del programma
		$a = get_tag($div, 'a', $cursore);
		if ($a !== FALSE)
		{
			$a=get_attribute($a, 'src');
		}
*/
		$cursore = 0;
		$title = leva_tag_esterni(get_tag($div, 'h2', $cursore));
		$tipo = leva_tag_esterni(get_tag($div, 'h3', $cursore));

		$tipo = str_replace('  ', ' ', $tipo);
		$tipo = str_replace('  ', ' ', $tipo);
		$tipo = str_replace('  ', ' ', $tipo);
		$tipo = str_replace('  ', ' ', $tipo);

		$result['titolo'] = $title;
		$result['tipo'] = $tipo;
/*
		if ($a !== FALSE)
		{
			$result['logo'] = $a;
		}*/
	}

	return $result;
}

function parse_ora(&$div)
{
	$label = '<strong>Ore ';
	$inizio = strpos($div, $label);
	$inizio += strlen($label);
	$fine = strpos($div, '</strong>', $inizio);

	$hm = explode(',',trim(substr($div, $inizio, $fine-$inizio)));

	$h = $hm[0]*1; if ($h<10) $h='0'.$h;
	$m = $hm[1]*1; if ($m<10) $m='0'.$m;

	return $h.$m;
}

function get_epg(&$channel_data, $quando, &$palinsesto)
{
	$rnd = rand()/getrandmax();
	$url = 'http://www.mymovies.it/v9/tv/ajax/programmatv.asp?var_tipoprogramma=&fascia=(PROGRAMMAZIONE_TV.ORA_FILM>0)&var_id_canale='.$channel_data['id'].'&var_tipo_canale=D&variabile3='.$quando.'&div=idprogramma&rand='.$rnd;
	$epg = get_page($url);

	$programmi = array();

	$pattern = '{<div[^>]*>((?:(?:(?!<div[^>]*>|</div>).)++|<div[^>]*>(?1)</div>)*)</div>}si';
	preg_match_all($pattern, $epg, $matches);

	$matches = $matches[0];

	$giorni_da_aggiungere = $quando=='' ? 0 : 1;

	$reference = mktime(0,0,0,date("m"),date("d")+$giorni_da_aggiungere,date("Y"));

	$amg = date('Ymd', $reference);

	$last_hm="0000";

	for($index = 0; $index < count($matches); $index+=2)
	{
		$div_ora = $matches[$index];
		$div_programma = $matches[$index+1];

		$programma = parse_programma($div_programma);
		$hm = parse_ora($div_ora, $quando);

		if ($hm<$last_hm)
		{
			$giorni_da_aggiungere++;
			$reference = mktime(0,0,0,date("m"),date("d")+$giorni_da_aggiungere,date("Y"));
			$amg = date('Ymd', $reference);
		}

		$timestamp = $amg.$hm;
		$programma['timestamp'] = $timestamp;

		$last_hm = $hm;

		$palinsesto[$timestamp] = $programma;
	}
}

?>
<html>
<body>
<pre>
<?php
require_once 'database.php';

if (sql_connect())
{
	println('recupero lista canali');
	$main_page = get_page('http://www.mymovies.it/tv/digitaleterrestre/');
	$channels_epg_source = get_channels_epg_source($main_page);

	println('recupero dati canali');

	foreach ($channels_epg_source as $channel_name=>&$channel_data)
	{
		$epg_page = get_page($channel_data['epg_url']);

		$channel_id = get_channel_id($epg_page);

		$channel_data['id'] = $channel_id;
		println('ch:'.$channel_id.' '.$channel_data['name']);

		$palinsesto = array();

		println('ch:'.$channel_id.' '.$channel_data['name'].' recupero programmi di oggi');
		get_epg($channel_data, '', $palinsesto);
		println('ch:'.$channel_id.' '.$channel_data['name'].' recupero programmi di domani');
		get_epg($channel_data, 'domani', $palinsesto);
		//$channel_data['epg'] = $palinsesto;
		println('ch:'.$channel_id.' '.$channel_data['name'].' caricamento su db');
		sql_begin();
		sql_delete('palinsesto', '`canale`=\''.sql_escape($channel_data['name']).'\'');
		foreach($palinsesto as $timestamp => $programma)
		{
			$dati = array(
				'timestamp' => $programma['timestamp'],
				'canale' => $channel_data['name'],
				'programma' => $programma['titolo'],
				'tipo' => $programma['tipo']
			);
			sql_insert('palinsesto',$dati);

			println('ch:'.$channel_id.' '.$channel_data['name'].' '.$programma['timestamp'].' '.$programma['titolo'].' '.$programma['tipo']);
		}
		sql_commit();
	}

	sql_disconnect();
} else {
	println(sql_last_error());
}
?>
</body>
</pre>
</html>

Commits for Nextrek/Web/epg/index_old.php

Diff revisions: vs.
Revision Author Commited Message
9 DRuega picture DRuega Mon 06 May, 2013 21:54:02 +0000

Ripristinato EPG