Skip to content

Instantly share code, notes, and snippets.

@Behinder
Last active March 12, 2021 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Behinder/01580091d2fd7f1ec4b411d7b4b1e7a7 to your computer and use it in GitHub Desktop.
Save Behinder/01580091d2fd7f1ec4b411d7b4b1e7a7 to your computer and use it in GitHub Desktop.
Watch Your TV EPG generator
<?php
/**
* Class for dealing with Trakt API
*/
class Trakt
{
public $clientid;
public $content_type;
public $apiurl;
public $ch;
public $notfoundarray;
public $slug_found;
function __construct($client_id)
{
$this->clientid=$client_id;
$this->content_type="application/json";
$this->apiurl="https://api.trakt.tv";
$this->notfoundarray=[];
$this->slug_found=0;
$this->ch=curl_init();
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, [
'trakt-api-key:'.$client_id,
'trakt-api-version: 2',
'Content-Type: application/json;charset=utf-8'
]);
}
public function findTraktID($movie)
{
$movie = rtrim($movie);
$check = preg_match('/(.+)\s\((\d\d\d\d)\)/', $movie, $output);
if ($check) {
$title = $output[1];
$year = $output[2];
$url = $this->apiurl."/search/movie?query=".urlencode($title)."&fields=title,aliases,overview";
curl_setopt($this->ch,CURLOPT_URL,$url);
$response = curl_exec($this->ch);
$object = json_decode($response,true);
sleep(2);
if ($object !=NULL && count($object) > 0) {
if (in_array($title,$this->notfoundarray,true)) {
echo "Value exists in table\n";
} else {
for ($i=0; $i < count($object); $i++) {
if (array_key_exists("ids",$object[$i]["movie"])) {
echo "TraktID=".$object[$i]["movie"]["ids"]["trakt"];
} else {
echo "IDS not exists";
}
if (array_key_exists("movie",$object[$i]) && $object[$i]["movie"]["title"] == $title && $object[$i]["movie"]["year"]==$year) {
$this->slug_found=1;
$trakt = $object[$i]["movie"]["ids"]["trakt"];
return $trakt;
} else {
echo "Not found ID for film ".$movie."\n";
}
}//koniec for
array_push($this->notfoundarray,$title);
}
} else {
echo "Empty array\n";
}
} //koniec $check
}
public function getMovieDescription($traktid)
{
$url = $this->apiurl."/search/trakt/".$traktid."?type=movie&extended=full";
curl_setopt($this->ch,CURLOPT_URL,$url);
$response = curl_exec($this->ch);
$object = json_decode($response,true);
if ($object != NULL && array_key_exists("overview",$object[0]["movie"])) {
return $object[0]["movie"]["overview"];
}
}
public function findShowSlug($searchterm)
{
$searchterm = rtrim($searchterm);
echo "Warunek wyszukiwania->".$searchterm."|\n";
$url=$this->apiurl."/search/show?query=".urlencode($searchterm)."&fields=title&type=show";
curl_setopt($this->ch,CURLOPT_URL,$url);
$response = curl_exec($this->ch);
$object =json_decode($response,true); //array or null
// var_dump($object);
sleep(3);
if ($object != NULL && count($object) > 0) {
var_dump(in_array($searchterm,$this->notfoundarray,true));
if (in_array($searchterm,$this->notfoundarray,true)) {
echo "Value exists in table\n";
} else {
for ($i=0; $i < count($object); $i++) {
if (array_key_exists("ids",$object[$i]["show"])) {
echo "Slug = ".$object[$i]["show"]["ids"]["slug"]."\n";
} else {
echo "IDS nie istnieje\n";
}
$a = $searchterm;
$b = rtrim($object[$i]["show"]["title"]);
echo "Searchterm = ".$searchterm."\n";
echo "Tytuł show = ".$b."\n";
echo "levenstein a,b=".levenshtein($a,$b)."\n";
if (array_key_exists("show",$object[$i]) && $object[$i]["show"]["title"] == $searchterm) {
//matched
$this->slug_found=1;
$slug = $object[$i]["show"]["ids"]["slug"];
return $slug;
} else {
// var_dump($object[$i]["show"]["title"]);
echo "Slug not found dla ".$searchterm."\n";
$this->slug_found=0;
}
}//koniec for
array_push($this->notfoundarray,rtrim($searchterm));
}
} else {
echo "Empty array\n";
return;
}
}
public function getEpisodeDescription($slug,$season,$episode)
{
$url = $this->apiurl."/shows/".$slug."/seasons/".$season."/episodes/".$episode."?extended=full";
curl_setopt($this->ch,CURLOPT_URL,$url);
$response = curl_exec($this->ch);
$object = json_decode($response,true);
if ($object !=NULL && array_key_exists("overview",$object)) {
return $object["overview"];
} else {
echo "Overview not found\n";
return;
}
}
}
$t = new Trakt("aa7cff8942d8c51b584142ca3d87d0e22d7df7c63646c11f537a545f5701049b");
// $r = $t->getEpisodeDescription("hong-kong",1,1);
// $r = $t->findShowSlug("Hong Kong");
?>
<?php
include_once 'trakt.php';
$path =dirname(__FILE__);
$jsonfile = "https://www.watchyour.tv/guide.json";
$dekodujguide = json_decode(mb_convert_encoding(file_get_contents($jsonfile),'UTF-8'),true);
// var_dump($dekodujguide);
$guidefile = fopen("wytv.xml","w") or die("Unable to open file");
$tv = new SimpleXMLElement('<tv> </tv>');
$t = new Trakt("aa7cff8942d8c51b584142ca3d87d0e22d7df7c63646c11f537a545f5701049b");
foreach ($dekodujguide as $element) {
$nazwa_kanalu = $element["name"]; // display-name
$ikona = $element["icon"]; // icon src=
$id = $element["id"]; // another display-name
$skrot = strtolower($nazwa_kanalu);
$skrot = str_replace(" ","-",$skrot);
$channel = $tv->addChild("channel");
$channel->addAttribute("id",$skrot);
$channel->addChild("display-name",$nazwa_kanalu);
$channel->addChild("display-name",$id);
$ic =$channel->addChild("icon");
$ic->addAttribute("src",$ikona);
// dla każdego kanału jego program jest w znaczniku <programme>
// atrybuty start, stop, channel
// format czasu yyyymmddhhmmss +timezonediff (GMT+1)
// <title lang="en"> </title>
// <date> format yyyymmdd
// <category lang="en"> (if present)
// <desc lang="en"> episode description
// <subtitle lang="en"> (if present)
//<icon src> okladka do poszczegolnego odcinka/programu
// <episode-num> attr system=
// onscreen !! high priority -> season.episode
// original-air-date yyyy-mm-ddThh:mm:ss.000Z
$programs = $element["shows"];
foreach ($programs as $show) {
$pr = $tv->addChild("programme");
$pr->addAttribute("channel",$skrot);
$timezone = $show["timezone"];
$startday = str_replace("-","",$show["start_day"]);
$start = str_replace(":","",$show["start"]);
$endday = str_replace("-","",$show["end_day"]);
$end = str_replace(":","",$show["end"]);
$startstring = $startday.$start." ".$timezone;
$endstring = $endday.$end." ".$timezone;
$pr->addAttribute("start",$startstring);
$pr->addAttribute("stop",$endstring);
$pr->addChild("category",htmlspecialchars($show["category"]));
$pr->addChild("date",$startday);
// Checking naming scenarios
$check = preg_match('/(.*)[Ss](\d{1,})\s{0,}[Ee](\d{1,})(.*)/', $show["name"], $output);
if ($check) {
$se = "S".$output[2]."E".$output[3];
$ep = $pr->addChild("episode-num",$se);
$ep->addAttribute("system","onscreen");
$pr->addChild("sub-title",htmlspecialchars($output[4]));
$pr->addChild("title",htmlspecialchars($output[1]));
$slug = $t->findShowSlug($output[1]);
sleep(4);
if ($slug !="") {
$desc = $t->getEpisodeDescription($slug,$output[2],$output[3]);
var_dump($desc);
$pr->addChild("desc",htmlspecialchars($desc));
} else {
$slug = $t->findShowSlug("The ".$output[1]);
if ($slug !="") {
$desc = $t->getEpisodeDescription($slug,$output[2],$output[3]);
$pr->addChild("desc",htmlspecialchars($desc));
}
}
} else {
$check2 = preg_match('/(.*)(\d{1,})\s{0,}x\s{0,}(\d{1,})(.*)/', $show["name"], $output);
if ($check2) {
$se = "S".$output[2]."E".$output[3];
$ep = $pr->addChild("episode-num",$se);
$ep->addAttribute("system","onscreen");
$pr->addChild("sub-title",htmlspecialchars($output[4]));
$pr->addChild("title",htmlspecialchars($output[1]));
$slug = $t->findShowSlug($output[1]);
sleep(4);
if ($slug !="") {
$desc = $t->getEpisodeDescription($slug,$output[2],$output[3]);
$pr->addChild("desc",htmlspecialchars($desc));
} else {
$slug = $t->findShowSlug("The ".$output[1]);
if ($slug!="") {
$desc = $t->getEpisodeDescription($slug,$output[2],$output[3]);
$pr->addChild("desc",htmlspecialchars($desc));
}
}
} else {
$check3 = preg_match('/(.+)\s(\d{3,4}) - (.*)/', $show["name"], $output);
if ($check3) {
$lngth = strlen($output[2]);
$epno = $lngth -2;
$episode =substr($output[2],-2);
$season = substr($output[2],0,$epno);
$se = "S".$season."E".$episode;
$ep = $pr->addChild("episode-num",$se);
$ep->addAttribute("system","onscreen");
$pr->addChild("sub-title",htmlspecialchars($output[3]));
$pr->addChild("title",htmlspecialchars($output[1]));
$slug = $t->findShowSlug($output[1]);
sleep(4);
if ($slug !="") {
$desc = $t->getEpisodeDescription($slug,$output[2],$output[3]);
$pr->addChild("desc",htmlspecialchars($desc));
} else {
$slug = $t->findShowSlug("The ".$output[1]);
if ($slug!="") {
$desc = $t->getEpisodeDescription($slug,$output[2],$output[3]);
$pr->addChild("desc",htmlspecialchars($desc));
}
}
} else {
$pr->addChild("title",htmlspecialchars($show["name"]));
$pr->addChild("category","Movie");
// procedure for getting movie description
//
$traktid = $t->findTraktID($show["name"]);
if ($traktid !="") {
$desc = $t->getMovieDescription($traktid);
$pr->addChild("desc",htmlspecialchars($desc));
}
}
}
}
}
}
$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