Skip to content

Instantly share code, notes, and snippets.

@burningtree
Last active December 16, 2015 07:18
Show Gist options
  • Select an option

  • Save burningtree/5397210 to your computer and use it in GitHub Desktop.

Select an option

Save burningtree/5397210 to your computer and use it in GitHub Desktop.
<?php
$url = "http://www.asko-nabytek.cz/katalog-letak-asko";
$template = array
(
"title" => "ASKO nábytek",
"descr" => null,
"datetime_end" => null,
"datetime_begin" => null,
"file_orig" => null,
"source_url" => $url
);
$html = file_get_contents($url);
preg_match_all("/katalog.asko.cz\/([^\"]+)/",$html,$matches_codes);
preg_match_all("/Platnost:<\/strong> ([^<]+)/",$html,$matches_validity);
preg_match_all("/Leták je platný pro(.+)/",$html,$matches_validity_type);
$buffer = array();
for($i=1; $i<=count($matches_codes[1]); $i++)
{
// kazdy sudy ulozime do bufferu
if($i%2==0)
{
$buffer[] = $matches_codes[1][$i-1];
}
}
$find_dates = function($str)
{
$date1 = mb_substr($str, 0, 13);
$date2 = mb_substr($str, 19, 13);
preg_match("/(\d{1,2}).[^\d]+(\d{1,2}).[^\d]+(\d{4})/", $date1, $m1);
preg_match("/(\d{1,2}).[^\d]+(\d{1,2}).[^\d]+(\d{4})/", $date2, $m2);
return array(strtotime("$m1[3]/$m1[2]/$m1[1]"), strtotime("$m2[3]/$m2[2]/$m2[1]"));
};
$out = array();
for($i=0; $i<count($buffer); $i++)
{
$item = $template;
$dates = $find_dates($matches_validity[1][$i]);
$item['datetime_begin'] = date("Y-m-d H:i:s",$dates[0]);
$item['datetime_end'] = date("Y-m-d H:i:s",strtotime("-1 second", $dates[1]));
$item['file_orig'] = "http://katalog.asko.cz/".$buffer[$i]."/includes/".str_replace("-","_",$buffer[$i]).".pdf";
$item['descr'] = "Platnost letáku od ".date("j.m.Y", $dates[0])." do ".date("j.m.Y", $dates[1])." pro ".(preg_match("/E-shop/",$matches_validity_type[1][$i]) ? 'E-Shop' : 'všechny pobočky');
$out[] = $item;
}
var_dump($out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment