commit | author | age
|
8091b1
|
1 |
<?php |
JK |
2 |
class wp_parse { |
|
3 |
var $document, $xpath, $context; |
|
4 |
var $name = ''; |
|
5 |
var $program = array(); |
|
6 |
var $months = array( |
|
7 |
'stycznia' => 1, |
|
8 |
'lutego' => 2, |
|
9 |
'marca' => 3, |
|
10 |
'kwietnia' => 4, |
|
11 |
'maja' => 5, |
|
12 |
'czerwca' => 6, |
|
13 |
'lipca' => 7, |
|
14 |
'sierpnia' => 8, |
|
15 |
'września' => 9, |
|
16 |
'października' => 10, |
|
17 |
'listopada' => 11, |
|
18 |
'grudnia' => 12, |
|
19 |
); |
|
20 |
var $weekdays = array( |
|
21 |
'poniedziałek' => 'next Monday', |
|
22 |
'wtorek' => 'next Tuesday', |
|
23 |
'środa' => 'next Wednesday', |
|
24 |
'czwartek' => 'next Thursday', |
|
25 |
'piątek' => 'next Friday', |
|
26 |
'sobota' => 'next Saturday', |
|
27 |
'niedziela' => 'next Sunday', |
|
28 |
); |
|
29 |
|
|
30 |
function __construct(DOMDocument $document) { |
|
31 |
$this->document = $document; |
|
32 |
$this->xpath = new DOMXPath($this->document); |
|
33 |
|
|
34 |
$context = $this->xpath->query('//div[@class="ramowka"]'); |
|
35 |
if($context->length != 1) { |
|
36 |
throw new Exception('Nie znaleziono ramówki!'); |
|
37 |
} |
|
38 |
$this->context = $context->item(0); |
|
39 |
|
4c7016
|
40 |
$name = $this->xpath->query('.//*[@class="sh2"]//span//text()', $this->context); |
8091b1
|
41 |
if($name->length != 1) { |
JK |
42 |
throw new Exception('Nie znaleziono nazwy stacji, błędny HTML.'); |
|
43 |
} |
|
44 |
$this->name = $name->item(0)->nodeValue; |
|
45 |
} |
|
46 |
|
|
47 |
function parse_date($date) { |
|
48 |
if($date == 'dzisiaj') { |
|
49 |
// 'dzisiaj' |
|
50 |
return mktime(0, 0, 0); |
|
51 |
} |
|
52 |
elseif(isset($this->weekdays[$date])) { |
|
53 |
// data przyszła: 'poniedziałek' |
|
54 |
return strtotime($this->weekdays[$date]); |
|
55 |
} |
|
56 |
else |
|
57 |
{ |
|
58 |
// data przeszła: 'pon. 18 czerwca' |
|
59 |
$date = explode(' ', $date); |
|
60 |
if(!isset($this->months[$date[2]])) { |
|
61 |
throw new Exception('Nie udało się przetworzyć daty ('.$date[2].')'); |
|
62 |
} |
|
63 |
$timestamp = mktime(0, 0, 0, $this->months[$date[2]], $date[1]); |
|
64 |
|
|
65 |
// Należy przesunąć się o rok |
|
66 |
if($timestamp > time()) { |
|
67 |
$timestamp = strtotime('-1 year', $timestamp); |
|
68 |
} |
|
69 |
|
|
70 |
return $timestamp; |
|
71 |
} |
|
72 |
} |
|
73 |
|
|
74 |
function xmltv($id, $fp) { |
|
75 |
$program = array(); |
|
76 |
|
|
77 |
$days_dom = $this->xpath->query('.//ul[@class="lsDay"]//li', $this->context); |
|
78 |
$days = array(); |
|
79 |
foreach($days_dom as $day) { |
|
80 |
$days[] = $this->parse_date($day->nodeValue); |
|
81 |
$program[] = array(); |
|
82 |
} |
|
83 |
unset($days_dom, $day); |
|
84 |
|
|
85 |
$hours_dom = $this->xpath->query('.//div[@class="hrsOut"]//div[@class="hour"]', $this->context); |
|
86 |
// Kolejne wiersze (pełne godziny) |
|
87 |
foreach($hours_dom as $in => $hour) { |
|
88 |
$days_dom = $this->xpath->query('.//div[@class="col"]', $hour); |
|
89 |
// Zbiory programów w tych godzinach dla kolejnych dni |
|
90 |
foreach($days_dom as $num => $day) { |
|
91 |
$programs_dom = $this->xpath->query('.//div[@class="prog"]', $day); |
|
92 |
// Kolejne programy w danej godzinie i dniu |
|
93 |
foreach($programs_dom as $n => $programs) { |
|
94 |
$godzina = $this->xpath->query('.//div[@class="tm"]', $programs)->item(0)->textContent; |
|
95 |
$nazwa = $this->xpath->query('.//h3', $programs)->item(0)->textContent; |
|
96 |
$opis = $this->xpath->query('.//p', $programs)->item(0)->textContent; |
|
97 |
|
c786da
|
98 |
$program[$num][] = array($godzina, $nazwa, $opis); |
8091b1
|
99 |
} |
JK |
100 |
unset($programs_dom, $programs); |
|
101 |
} |
|
102 |
unset($days_dom, $day); |
|
103 |
} |
|
104 |
unset($hours_dom, $hour, $godzina, $nazwa, $opis); |
|
105 |
|
|
106 |
fwrite($fp, "\t".'<channel id="'.$id.'">'."\n" |
|
107 |
."\t\t".'<display-name>'.htmlspecialchars($this->name).'</display-name>'."\n" |
|
108 |
."\t".'</channel>'."\n"); |
|
109 |
|
|
110 |
$last_timestamp = $timestamp = $days[0]; |
|
111 |
$last_prog = NULL; |
|
112 |
foreach($program as $day => $dayprog) { |
|
113 |
foreach($dayprog as $prog) { |
|
114 |
$timestamp = strtotime($prog[0], $last_timestamp); |
|
115 |
if($timestamp < $last_timestamp) { |
|
116 |
$timestamp = strtotime('+1 day', $timestamp); |
|
117 |
} |
|
118 |
while($timestamp < $days[$day]) { |
|
119 |
$timestamp = strtotime('+1 day', $timestamp); |
|
120 |
} |
|
121 |
|
|
122 |
if($program !== NULL) |
|
123 |
fwrite($fp, "\t".'<programme channel="'.$id.'" start="'.date('YmdHis O', $last_timestamp).'"' |
|
124 |
.' stop="'.date('YmdHis O', $timestamp).'">'."\n" |
|
125 |
."\t\t".'<title>'.htmlspecialchars($last_prog[1]).'</title>'."\n" |
|
126 |
."\t\t".'<desc>'.htmlspecialchars($last_prog[2]).'</desc>'."\n" |
|
127 |
."\t".'</programme>'."\n"); |
|
128 |
|
|
129 |
$last_prog = $prog; |
|
130 |
$last_timestamp = $timestamp; |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
} |
4c7016
|
135 |
?> |