From 4aa6278f7fd40fdcbbde2be5c7e0e68c3117b44e Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Fri, 12 Feb 2016 23:18:17 +0000
Subject: [PATCH] [mod_tv] Lepsza obsługa błędów przy okresowym pobieraniu danych
---
modules/70_kino/handler.php | 67 ++++++++++++++++-----------------
1 files changed, 32 insertions(+), 35 deletions(-)
diff --git a/modules/70_kino/handler.php b/modules/70_kino/handler.php
index 79b67ef..6510e1c 100644
--- a/modules/70_kino/handler.php
+++ b/modules/70_kino/handler.php
@@ -1,33 +1,19 @@
<?php
class bot_kino_module implements BotModule {
function cache($url) {
- $time = '+2 hour';
- $dir = './data/kino/cache/';
-
- if(file_exists($dir.md5($url))) {
- $mtime = @filemtime($dir.md5($url));
- }
-
- if($mtime && $mtime > strtotime('today '.$time) && $mtime < strtotime('tomorrow '.$time)) {
- $dane = file_get_contents($dir.md5($url));
- }
- else
- {
- $dane = @file_get_contents($url);
- if(!$dane) {
- return FALSE;
- }
-
- file_put_contents($dir.md5($url), $dane);
- }
+ $down = new DownloadHelper($url);
+ $dane = $down->exec();
libxml_use_internal_errors(TRUE);
$dom = new DOMDocument();
if(!$dom->loadHTML($dane)) {
libxml_use_internal_errors(FALSE);
+ $down->cacheFor(1800);
return FALSE;
}
+
+ $down->cacheUntil(strtotime('tomorrow midnight'));
return $dom;
}
@@ -59,7 +45,7 @@
foreach($dane as $kino) {
$name = trim($kino->textContent);
- $return[$name] = $kino->getAttribute('name');
+ $return[$name] = $kino->getAttribute('href');
}
return $return;
@@ -70,19 +56,34 @@
if(!$xml) return FALSE;
$xpath = new DOMXPath($xml);
- $dane = $xpath->query('//div[@id=\'mainContent\']/table//a[@name=\''.$kino.'\']/../../following-sibling::tr');
+ $dane = $xpath->query('//div[@id=\'mainContent\']/table//a[@href=\''.$kino.'\']/../../following-sibling::tr');
$return = array();
foreach($dane as $film) {
- if($film->firstChild && $film->firstChild->nodeName == 'th') break;
+ if(!$film->firstChild) {
+ break;
+ }
+ if($film->firstChild->nodeName == 'th') {
+ break;
+ }
+ if($film->firstChild->nodeName != 'td') {
+ break;
+ }
$tds = $xpath->query('td', $film);
$name = $xpath->query('a[1]', $tds->item(0));
$more = array();
- $more_xml = $xpath->query('span[@class=\'reper\']/span', $tds->item(0));
+ $more_desc = array(
+ 's3d-movie' => '3D',
+ 'dubbing-movie' => 'dubbing',
+ );
+ $more_xml = $xpath->query('span[@class=\'reper\']/div', $tds->item(0));
foreach($more_xml as $more_x) {
- $more[] = $more_x->textContent;
+ $more_x = $more_x->getAttribute('class');
+ if(isset($more_desc[$more_x])) {
+ $more[] = $more_desc[$more_x];
+ }
}
$return[] = array(
@@ -134,7 +135,6 @@
MIASTO
*/
$miasta = self::getMiasta();
- $found = FALSE;
$miasto_num = $miasto_nazw = '';
if(!$miasta) {
@@ -144,7 +144,6 @@
foreach($miasta as $miasto => $numer) {
$szukaj = funcs::utfToAscii($miasto);
if(($pos = strpos($arg, $szukaj)) !== FALSE) {
- $found = TRUE;
$miasto_nazw = htmlspecialchars($miasto);
$miasto_num = $numer;
@@ -153,11 +152,10 @@
}
}
- if($found===FALSE && !empty($arg2)) {
+ if($miasto_num === '' && !empty($arg2)) {
foreach($miasta as $miasto => $numer) {
$szukaj = funcs::utfToAscii($miasto);
if(($pos = strpos($arg2, $szukaj)) !== FALSE) {
- $found = TRUE;
$miasto_nazw = htmlspecialchars($miasto);
$miasto_num = $numer;
@@ -167,8 +165,9 @@
}
}
- if($found === FALSE) {
+ if($miasto_num === '') {
$txt = 'Wybrane miasto nie został odnalezione. Obsługiwane miejscowości:';
+ $miasto = 'Warszawa';
foreach($miasta as $miasto => $num) {
$txt .= '<br />'."\n".htmlspecialchars($miasto);
}
@@ -209,7 +208,6 @@
KINO
*/
$kina = self::getKina($miasto_num, $czas);
- $found = FALSE;
$kino_num = $kino_nazw = '';
if(!$kina) {
@@ -227,7 +225,6 @@
if(!empty($arg)) {
foreach($kina as $kino => $kino_id) {
if(levenshtein(funcs::utfToAscii($kino), $arg, 1, 1, 0) < 2) {
- $found = TRUE;
$kino_num = $kino_id;
$kino_nazw = htmlspecialchars($kino);
break;
@@ -235,10 +232,9 @@
}
}
- if($found===FALSE && !empty($arg2)) {
+ if($kino_num === '' && !empty($arg2)) {
foreach($kina as $kino => $kino_id) {
if(levenshtein(funcs::utfToAscii($kino), $arg2, 1, 1, 0) < 2) {
- $found = TRUE;
$kino_num = $kino_id;
$kino_nazw = htmlspecialchars($kino);
break;
@@ -246,8 +242,9 @@
}
}
- if($found === FALSE) {
+ if($kino_num === '') {
$txt = (!empty($arg) ? 'Podany obiekt nie został znaleziony. ' : '').'Dostępne kina w pasujących miastach:';
+ $kino = '';
foreach($kina as $kino => $num) {
$txt .= '<br />'."\n".$miasto_nazw.' '.htmlspecialchars($kino);
}
@@ -261,7 +258,7 @@
/*
REPERTUAR
*/
- $filmy = self::getKino($miasto_num, $kino_id, $czas);
+ $filmy = self::getKino($miasto_num, $kino_num, $czas);
if(!$filmy) {
return new BotMsg('Przepraszamy, wystąpił bład przy pobieraniu listy wyświelanych filmów.');
--
Gitblit v1.9.1