Skip to content

Instantly share code, notes, and snippets.

@Behinder
Created March 19, 2021 18:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Behinder/3c00570213787326fe94d9940b41ac09 to your computer and use it in GitHub Desktop.
Save Behinder/3c00570213787326fe94d9940b41ac09 to your computer and use it in GitHub Desktop.
CWSeed EPG generator
<?php
$jsonfile = "https://www.cwseed.com/shows/linear-schedule/cwseed/2";
$decodedguide = json_decode(file_get_contents($jsonfile),true);
$guidefile = fopen("cwseed.xml","w") or die("Unable to open file");
$tv = new SimpleXMLElement('<tv> </tv>');
$channel = $tv->addChild("channel");
$channel->addAttribute("id","cw-seed");
$channel->addChild("display-name","CW SEED");
$arr = $decodedguide["assets"];
foreach ($arr as $program) {
var_dump($program);
// sleep(30);
$pr = $tv->addChild("programme");
$pr->addAttribute("channel","cw-seed");
$startstring = $program["start"];
$epoch = $program["start_ts"];
$endepoch = $epoch+$program["duration_secs"];
$endstring =date("Y-m-d H:i:s",$endepoch);
if (preg_match('/(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)(-\d\d:\d\d)/', $startstring, $output)) {
$timezone = str_replace(":","",$output[7]);
} else {
echo "time zone niedopasowane dla: ".$startstring;
}
$startstringconverted = preg_replace('/(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)(-\d\d:\d\d)/', '$1$2$3$4$5$6 $7', $startstring);
$startstringconverted = str_replace(":","",$startstringconverted);
$endstring = preg_replace('/(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/', '$1$2$3$4$5$6', $endstring)." ".$timezone;
$pr->addAttribute("start",$startstringconverted);
$pr->addAttribute("stop",$endstring);
//unique series identificator
$s_id =$pr->addChild("series-id",$program["id"]);
$s_id->addAttribute("system","cwseed");
$pr->addChild("date",date("Ymd",$endepoch));
//if tvshow
if (array_key_exists("season",$program)) {
$season = $program["season"];
$episodetotalnumber = $program["ep_number"];
$episode = substr($episodetotalnumber,-2);
$ep = $pr->addChild("episode-num","S".$season."E".$episode);
$ep->addAttribute("system","onscreen");
}
$pr->addChild("title",htmlspecialchars($program["title"]));
if (array_key_exists("ep_title",$program)) {
$pr->addChild("sub-title",htmlspecialchars($program["ep_title"]));
}
$pr->addChild("desc",$program["description"]);
$ic = $pr->addChild("icon");
$ic->addAttribute("src",$program["thumb_url"]);
}
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($tv->asXML());
$tvstring = $dom->saveXML();
// var_dump($tvstring);
fwrite($guidefile,$tvstring);
fclose($guidefile);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment