PageRenderTime 426ms CodeModel.GetById 52ms RepoModel.GetById 9ms app.codeStats 0ms

/www/lib/tvrage.php

https://github.com/vikjon0/newznab
PHP | 768 lines | 638 code | 75 blank | 55 comment | 79 complexity | 8d5d9a1076c3037d7593708d30a719ed MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. require_once(WWW_DIR."/lib/util.php");
  3. require_once(WWW_DIR."/lib/category.php");
  4. require_once(WWW_DIR."/lib/framework/db.php");
  5. class TvRage
  6. {
  7. const APIKEY = '7FwjZ8loweFcOhHfnU3E';
  8. const MATCH_PROBABILITY = 75;
  9. function TvRage($echooutput=false)
  10. {
  11. $this->echooutput = $echooutput;
  12. // not used anymore
  13. //$this->xmlSearchUrl = "http://services.tvrage.com/feeds/search.php?show=";
  14. //$this->xmlShowInfoUrl = "http://services.tvrage.com/feeds/showinfo.php?sid=";
  15. $this->xmlFullSearchUrl = "http://services.tvrage.com/feeds/full_search.php?show=";
  16. $this->xmlFullShowInfoUrl = "http://services.tvrage.com/feeds/full_show_info.php?sid=";
  17. $this->xmlEpisodeInfoUrl = "http://services.tvrage.com/myfeeds/episodeinfo.php?key=".TvRage::APIKEY;
  18. $this->showInfoUrl = "http://www.tvrage.com/shows/id-";
  19. }
  20. public function getByID($id)
  21. {
  22. $db = new DB();
  23. return $db->queryOneRow(sprintf("select * from tvrage where ID = %d", $id ));
  24. }
  25. public function getByRageID($id)
  26. {
  27. $db = new DB();
  28. return $db->query(sprintf("select * from tvrage where rageID = %d", $id ));
  29. }
  30. public function getByTitle($title)
  31. {
  32. // check if we already have an entry for this show
  33. $db = new DB();
  34. $res = $db->queryOneRow(sprintf("SELECT rageID from tvrage where lower(releasetitle) = lower(%s)", $db->escapeString($title)));
  35. if ($res)
  36. return $res["rageID"];
  37. $title2 = str_replace(' and ', ' & ', $title);
  38. $res = $db->queryOneRow(sprintf("SELECT rageID from tvrage where lower(releasetitle) = lower(%s)", $db->escapeString($title2)));
  39. if ($res)
  40. return $res["rageID"];
  41. return false;
  42. }
  43. public function add($rageid, $releasename, $desc, $genre, $country, $imgbytes)
  44. {
  45. $releasename = str_replace(array('.','_'), array(' ',' '), $releasename);
  46. $db = new DB();
  47. return $db->queryInsert(sprintf("insert into tvrage (rageID, releasetitle, description, genre, country, createddate, imgdata) values (%d, %s, %s, %s, %s, now(), %s)",
  48. $rageid, $db->escapeString($releasename), $db->escapeString($desc), $db->escapeString($genre), $db->escapeString($country), $db->escapeString($imgbytes)));
  49. }
  50. public function update($id, $rageid, $releasename, $desc, $genre, $country, $imgbytes)
  51. {
  52. $db = new DB();
  53. if ($imgbytes != "")
  54. $imgbytes = sprintf(", imgdata = %s", $db->escapeString($imgbytes));
  55. $db->query(sprintf("update tvrage set rageID = %d, releasetitle = %s, description = %s, genre = %s, country = %s %s where ID = %d",
  56. $rageid, $db->escapeString($releasename), $db->escapeString($desc), $db->escapeString($genre), $db->escapeString($country), $imgbytes, $id ));
  57. }
  58. public function delete($id)
  59. {
  60. $db = new DB();
  61. return $db->query(sprintf("delete from tvrage where ID = %d",$id));
  62. }
  63. public function getRange($start, $num, $ragename="")
  64. {
  65. $db = new DB();
  66. if ($start === false)
  67. $limit = "";
  68. else
  69. $limit = " LIMIT ".$start.",".$num;
  70. $rsql = '';
  71. if ($ragename != "")
  72. $rsql .= sprintf("and tvrage.releasetitle like %s ", $db->escapeString("%".$ragename."%"));
  73. return $db->query(sprintf(" SELECT ID, rageID, releasetitle, description, createddate from tvrage where 1=1 %s order by rageID asc".$limit, $rsql));
  74. }
  75. public function getCount($ragename="")
  76. {
  77. $db = new DB();
  78. $rsql = '';
  79. if ($ragename != "")
  80. $rsql .= sprintf("and tvrage.releasetitle like %s ", $db->escapeString("%".$ragename."%"));
  81. $res = $db->queryOneRow(sprintf("select count(ID) as num from tvrage where 1=1 %s ", $rsql));
  82. return $res["num"];
  83. }
  84. public function getEpisodeInfo($rageid, $series, $episode)
  85. {
  86. $result = array('title'=>'', 'airdate'=>'');
  87. $series = str_ireplace("s", "", $series);
  88. $episode = str_ireplace("e", "", $episode);
  89. $xml = getUrl($this->xmlEpisodeInfoUrl."&sid=".$rageid."&ep=".$series."x".$episode);
  90. if ($xml !== false)
  91. {
  92. if (preg_match('/no show found/i', $xml))
  93. return false;
  94. $xmlObj = @simplexml_load_string($xml);
  95. $arrXml = objectsIntoArray($xmlObj);
  96. if (is_array($arrXml))
  97. {
  98. if (isset($arrXml['episode']['airdate']) && $arrXml['episode']['airdate'] != '0000-00-00')
  99. $result['airdate'] = $arrXml['episode']['airdate'];
  100. if (isset($arrXml['episode']['title']))
  101. $result['title'] = $arrXml['episode']['title'];
  102. return $result;
  103. }
  104. return false;
  105. }
  106. return false;
  107. }
  108. public function getRageInfoFromPage($rageid)
  109. {
  110. $result = array('desc'=>'', 'imgurl'=>'');
  111. $page = getUrl($this->showInfoUrl.$rageid);
  112. if ($page !== false)
  113. {
  114. //description
  115. preg_match('@<div class="show_synopsis">(.*?)</div>@is', $page, $matches);
  116. if (isset($matches[1])) {
  117. $desc = $matches[1];
  118. $desc = preg_replace('/<hr>.*/s', '', $desc);
  119. $desc = preg_replace('/&nbsp;?/', '', $desc);
  120. $desc = preg_replace('/<br>(\n)?<br>/', ' / ', $desc);
  121. $desc = preg_replace('/\n/', ' ', $desc);
  122. $desc = preg_replace('/<a href.*?<\/a>/', '', $desc);
  123. $desc = preg_replace('/<script.*?<\/script>/', '', $desc);
  124. $desc = preg_replace('/<.*?>/', '', $desc);
  125. $desc = str_replace('()', '', $desc);
  126. $desc = trim(preg_replace('/\s{2,}/', ' ', $desc));
  127. $result['desc'] = $desc;
  128. }
  129. // image
  130. preg_match("@src=[\"'](http://images.tvrage.com/shows.*?)[\"']@i", $page, $matches);
  131. if (isset($matches[1])) {
  132. $result['imgurl'] = $matches[1];
  133. }
  134. }
  135. return $result;
  136. }
  137. public function getRageInfoFromService($rageid)
  138. {
  139. $result = array('genres'=>'', 'country'=>'', 'showid'=>$rageid);
  140. $xml = getUrl($this->xmlFullShowInfoUrl.$rageid); //full search gives us the akas
  141. if ($xml !== false)
  142. {
  143. $xmlObj = simplexml_load_string($xml);
  144. $arrXml = objectsIntoArray($xmlObj);
  145. if (is_array($arrXml))
  146. {
  147. $result['genres'] = (isset($arrXml['genres'])) ? $arrXml['genres'] : '';
  148. $result['country'] = (isset($arrXml['origin_country'])) ? $arrXml['origin_country'] : '';
  149. return $result;
  150. }
  151. return false;
  152. }
  153. return false;
  154. }
  155. public function updateEpInfo($show, $relid)
  156. {
  157. $db = new Db;
  158. if ($this->echooutput)
  159. echo "TV series - ".$show['name']."-".$show['seriesfull'].(($show['year']!='')?' '.$show['year']:'').(($show['country']!='')?' ['.$show['country'].']':'')."\n";
  160. $tvairdate = (!empty($show['airdate'])) ? $db->escapestring($show['airdate']) : "null";
  161. $db->query(sprintf("update releases set seriesfull = %s, season = %s, episode = %s, tvairdate=%s where ID = %d",
  162. $db->escapeString($show['seriesfull']), $db->escapeString($show['season']), $db->escapeString($show['episode']), $tvairdate, $relid));
  163. }
  164. public function updateRageInfo($rageid, $show, $tvrShow, $relid)
  165. {
  166. $db = new Db;
  167. // try and get the episode specific info from tvrage
  168. $epinfo = $this->getEpisodeInfo($rageid, $show['season'], $show['episode']);
  169. if ($epinfo !== false)
  170. {
  171. $tvairdate = (!empty($epinfo['airdate'])) ? $db->escapeString($epinfo['airdate']) : "null";
  172. $tvtitle = (!empty($epinfo['title'])) ? $db->escapeString($epinfo['title']) : "null";
  173. $db->query(sprintf("update releases set tvtitle=trim(%s), tvairdate=%s, rageID = %d where ID = %d", $tvtitle, $tvairdate, $tvrShow['showid'], $relid));
  174. } else {
  175. $db->query(sprintf("update releases set rageID = %d where ID = %d", $tvrShow['showid'], $relid));
  176. }
  177. $genre = '';
  178. if (isset($tvrShow['genres']) && is_array($tvrShow['genres']) && !empty($tvrShow['genres']))
  179. {
  180. if (is_array($tvrShow['genres']['genre']))
  181. $genre = implode('|', $tvrShow['genres']['genre']);
  182. else
  183. $genre = $tvrShow['genres']['genre'];
  184. }
  185. $country = '';
  186. if (isset($tvrShow['country']) && !empty($tvrShow['country']))
  187. $country = $tvrShow['country'];
  188. $rInfo = $this->getRageInfoFromPage($rageid);
  189. $desc = '';
  190. if (isset($rInfo['desc']) && !empty($rInfo['desc']))
  191. $desc = $rInfo['desc'];
  192. $imgbytes = '';
  193. if (isset($rInfo['imgurl']) && !empty($rInfo['imgurl']))
  194. {
  195. $img = getUrl($rInfo['imgurl']);
  196. if ($img !== false)
  197. {
  198. $im = @imagecreatefromstring($img);
  199. if($im !== false)
  200. $imgbytes = $img;
  201. }
  202. }
  203. $this->add($rageid, $show['cleanname'], $desc, $genre, $country, $imgbytes);
  204. }
  205. public function processTvReleases($lookupTvRage=true)
  206. {
  207. $ret = 0;
  208. $db = new DB();
  209. // get all releases without a rageid which are in a tv category.
  210. $result = $db->queryDirect(sprintf("SELECT searchname, ID from releases where rageID = -1 and categoryID in ( select ID from category where parentID = %d )", Category::CAT_PARENT_TV));
  211. if ($this->echooutput)
  212. {
  213. echo "Processing tv for ".mysql_num_rows($result)." releases\n";
  214. echo "Lookup tv rage from the web - ".($lookupTvRage?"Yes\n":"No\n");
  215. }
  216. while ($arr = mysql_fetch_assoc($result))
  217. {
  218. $show = $this->parseNameEpSeason($arr['searchname']);
  219. if (is_array($show) && $show['name'] != '')
  220. {
  221. // update release with season, ep, and airdate info (if available) from releasetitle
  222. $this->updateEpInfo($show, $arr['ID']);
  223. // find the rageID
  224. $id = $this->getByTitle($show['cleanname']);
  225. if ($id === false && $lookupTvRage)
  226. {
  227. // if it doesnt exist locally and lookups are allowed lets try to get it
  228. if ($this->echooutput)
  229. echo 'didnt find rageid for "'.$show['cleanname'].'" in local db, checking web...'."\n";
  230. $tvrShow = $this->getRageMatch($show);
  231. if ($tvrShow !== false && is_array($tvrShow))
  232. {
  233. // get all tv info and add show
  234. $this->updateRageInfo($tvrShow['showid'], $show, $tvrShow, $arr['ID']);
  235. }
  236. elseif ($tvrShow === false)
  237. {
  238. // no match
  239. //add to tvrage with rageID = -2 and $show['cleanname'] title only
  240. $this->add(-2, $show['cleanname'], '', '', '', '');
  241. }
  242. else
  243. {
  244. // $tvrShow probably equals -1 but we'll do this as a catchall instead of a specific elseif
  245. //skip because we couldnt connect to tvrage.com
  246. }
  247. }
  248. elseif ($id > 0)
  249. {
  250. $tvairdate = (isset($show['airdate']) && !empty($show['airdate'])) ? $db->escapeString($show['airdate']) : "null";
  251. $tvtitle = "null";
  252. if ($lookupTvRage)
  253. {
  254. $epinfo = $this->getEpisodeInfo($id, $show['season'], $show['episode']);
  255. if ($epinfo !== false)
  256. {
  257. if (!empty($epinfo['airdate']))
  258. $tvairdate = $db->escapeString($epinfo['airdate']);
  259. if (!empty($epinfo['title']))
  260. $tvtitle = $db->escapeString($epinfo['title']);
  261. }
  262. }
  263. $db->query(sprintf("update releases set tvtitle=trim(%s), tvairdate=%s, rageID = %d where ID = %d", $tvtitle, $tvairdate, $id, $arr["ID"]));
  264. }
  265. else
  266. {
  267. // cant find rageid, so set rageid to n/a
  268. $db->query(sprintf("update releases set rageID = -2 where ID = %d", $arr["ID"]));
  269. }
  270. }
  271. else
  272. {
  273. // not a tv episode, so set rageid to n/a
  274. $db->query(sprintf("update releases set rageID = -2 where ID = %d", $arr["ID"]));
  275. }
  276. $ret++;
  277. }
  278. return $ret;
  279. }
  280. public function getRageMatch($showInfo)
  281. {
  282. $title = $showInfo['cleanname'];
  283. $xml = getUrl($this->xmlFullSearchUrl.urlencode(strtolower($title))); //full search gives us the akas
  284. if ($xml !== false)
  285. {
  286. $xmlObj = simplexml_load_string($xml);
  287. $arrXml = objectsIntoArray($xmlObj);
  288. if (isset($arrXml['show']) && is_array($arrXml['show']))
  289. {
  290. // we got a valid xml response
  291. $titleMatches = array();
  292. $urlMatches = array();
  293. $akaMatches = array();
  294. if (isset($arrXml['show']['showid']))
  295. {
  296. // we got exactly 1 match so lets convert it to an array so we can use it in the logic below
  297. $newArr = array();
  298. $newArr[] = $arrXml['show'];
  299. unset($arrXml);
  300. $arrXml['show'] = $newArr;
  301. }
  302. foreach ($arrXml['show'] as $arr)
  303. {
  304. $titlepct = $urlpct = $akapct = 0;
  305. // get a match percentage based on our name and the name returned from tvr
  306. $titlepct = $this->checkMatch($title, $arr['name']);
  307. if ($titlepct !== false)
  308. $titleMatches[$titlepct][] = array('title'=>$arr['name'], 'showid'=>$arr['showid'], 'country'=>$arr['country'], 'genres'=>$arr['genres'], 'tvr'=>$arr);
  309. // get a match percentage based on our name and the url returned from tvr
  310. if (isset($arr['link']) && preg_match('/tvrage\.com\/((?!shows)[^\/]*)$/i', $arr['link'], $tvrlink))
  311. {
  312. $urltitle = str_replace('_', ' ', $tvrlink[1]);
  313. $urlpct = $this->checkMatch($title, $urltitle);
  314. if ($urlpct !== false)
  315. $urlMatches[$urlpct][] = array('title'=>$urltitle, 'showid'=>$arr['showid'], 'country'=>$arr['country'], 'genres'=>$arr['genres'], 'tvr'=>$arr);
  316. }
  317. // check if there are any akas for this result and get a match percentage for them too
  318. if (isset($arr['akas']))
  319. {
  320. if (is_array($arr['akas']['aka']))
  321. {
  322. // multuple akas
  323. foreach($arr['akas']['aka'] as $aka)
  324. {
  325. $akapct = $this->checkMatch($title, $aka);
  326. if ($akapct !== false)
  327. $akaMatches[$akapct][] = array('title'=>$aka, 'showid'=>$arr['showid'], 'country'=>$arr['country'], 'genres'=>$arr['genres'], 'tvr'=>$arr);
  328. }
  329. } else {
  330. // one aka
  331. $akapct = $this->checkMatch($title, $arr['akas']['aka']);
  332. if ($akapct !== false)
  333. $akaMatches[$akapct][] = array('title'=>$arr['akas']['aka'], 'showid'=>$arr['showid'], 'country'=>$arr['country'], 'genres'=>$arr['genres'], 'tvr'=>$arr);
  334. }
  335. }
  336. }
  337. // reverse sort our matches so highest matches are first
  338. krsort($titleMatches);
  339. krsort($urlMatches);
  340. krsort($akaMatches);
  341. //print_r($titleMatches);
  342. //print_r($urlMatches);
  343. //print_r($akaMatches)
  344. // look for 100% title matches first
  345. if (isset($titleMatches[100]))
  346. {
  347. if ($this->echooutput)
  348. echo '-found 100% match: "'.$titleMatches[100][0]['title'].'"'."\n";
  349. return $titleMatches[100][0];
  350. }
  351. // look for 100% url matches next
  352. if (isset($urlMatches[100]))
  353. {
  354. if ($this->echooutput)
  355. echo '-found 100% url match: "'.$urlMatches[100][0]['title'].'"'."\n";
  356. return $urlMatches[100][0];
  357. }
  358. // look for 100% aka matches next
  359. if (isset($akaMatches[100]))
  360. {
  361. if ($this->echooutput)
  362. echo '-found 100% aka match: "'.$akaMatches[100][0]['title'].'"'."\n";
  363. return $akaMatches[100][0];
  364. }
  365. // no 100% matches, loop through what we got and if our next closest match is more than TvRage::MATCH_PROBABILITY % of the title lets take it
  366. foreach($titleMatches as $mk=>$mv)
  367. {
  368. // since its not 100 match if we have country info lets use that to make sure we get the right show
  369. if (isset($showInfo['country']) && !empty($showInfo['country']) && !empty($mv[0]['country']))
  370. if (strtolower($showInfo['country']) != strtolower($mv[0]['country']))
  371. continue;
  372. if ($this->echooutput)
  373. echo '-found '.$mk.'% match: "'.$titleMatches[$mk][0]['title'].'"'."\n";
  374. return $titleMatches[$mk][0];
  375. }
  376. // same as above but for akas
  377. foreach($akaMatches as $ak=>$av)
  378. {
  379. if (isset($showInfo['country']) && !empty($showInfo['country']) && !empty($av[0]['country']))
  380. if (strtolower($showInfo['country']) != strtolower($av[0]['country']))
  381. continue;
  382. if ($this->echooutput)
  383. echo '-found '.$ak.'% aka match: "'.$akaMatches[$ak][0]['title'].'"'."\n";
  384. return $akaMatches[$ak][0];
  385. }
  386. if ($this->echooutput)
  387. echo '-no match found online'."\n";
  388. return false;
  389. } else {
  390. if ($this->echooutput)
  391. echo '-nothing returned from tvrage'."\n";
  392. return false;
  393. }
  394. } else {
  395. if ($this->echooutput)
  396. echo '-error connecting to tvrage'."\n";
  397. return -1;
  398. }
  399. if ($this->echooutput)
  400. echo '-no match found online'."\n";
  401. return false;
  402. }
  403. public function checkMatch($ourName, $tvrName)
  404. {
  405. // clean up name ($ourName is already clean)
  406. $tvrName = $this->cleanName($tvrName);
  407. $tvrName = preg_replace('/ of /i', '', $tvrName);
  408. $ourName = preg_replace('/ of /i', '', $ourName);
  409. // create our arrays
  410. $ourArr = explode(' ', $ourName);
  411. $tvrArr = explode(' ', $tvrName);
  412. // set our match counts
  413. $numMatches = 0;
  414. $totalMatches = sizeof($ourArr)+sizeof($tvrArr);
  415. // loop through each array matching again the opposite value, if they match increment!
  416. foreach($ourArr as $oname)
  417. {
  418. if (preg_match('/ '.preg_quote($oname, '/').' /i', ' '.$tvrName.' '))
  419. $numMatches++;
  420. }
  421. foreach($tvrArr as $tname)
  422. {
  423. if (preg_match('/ '.preg_quote($tname, '/').' /i', ' '.$ourName.' '))
  424. $numMatches++;
  425. }
  426. // check what we're left with
  427. if ($numMatches <= 0)
  428. return false;
  429. else
  430. $matchpct = ($numMatches/$totalMatches)*100;
  431. if ($matchpct >= TvRage::MATCH_PROBABILITY)
  432. return $matchpct;
  433. else
  434. return false;
  435. }
  436. public function cleanName($str)
  437. {
  438. $str = str_replace(array('.', '_'), ' ', $str);
  439. $str = str_replace(array('à','á','â','ã','ä','æ','À','Á','Â','Ã','Ä'), 'a', $str);
  440. $str = str_replace(array('ç','Ç'), 'c', $str);
  441. $str = str_replace(array('Σ','è','é','ê','ë','È','É','Ê','Ë'), 'e', $str);
  442. $str = str_replace(array('ì','í','î','ï','Ì','Í','Î','Ï'), 'i', $str);
  443. $str = str_replace(array('ò','ó','ô','õ','ö','Ò','Ó','Ô','Õ','Ö'), 'o', $str);
  444. $str = str_replace(array('ù','ú','û','ü','ū','Ú','Û','Ü','Ū'), 'u', $str);
  445. $str = str_replace('ß', 'ss', $str);
  446. $str = str_replace('&', 'and', $str);
  447. $str = preg_replace('/^(history|discovery) channel/i', '', $str);
  448. $str = str_replace(array('\'', ':', '!', '"', '#', '*', '’', ',', '(', ')', '?'), '', $str);
  449. $str = str_replace('$', 's', $str);
  450. $str = preg_replace('/\s{2,}/', ' ', $str);
  451. return trim($str);
  452. }
  453. public function parseNameEpSeason($relname)
  454. {
  455. $showInfo = array(
  456. 'name' => '',
  457. 'season' => '',
  458. 'episode' => '',
  459. 'seriesfull' => '',
  460. 'airdate' => '',
  461. 'country' => '',
  462. 'year' => '',
  463. 'cleanname' => ''
  464. );
  465. //S01E01-E02
  466. //S01E01-02
  467. if (preg_match('/^(.*?)[\. ]s(\d{1,2})\.?e(\d{1,3})(?:\-e?|\-?e)(\d{1,3})\./i', $relname, $matches)) {
  468. $showInfo['name'] = $matches[1];
  469. $showInfo['season'] = intval($matches[2]);
  470. $showInfo['episode'] = array(intval($matches[3]), intval($matches[4]));
  471. //S01E0102 - lame no delimit numbering, regex would collide if there was ever 1000 ep season
  472. } elseif (preg_match('/^(.*?)[\. ]s(\d{2})\.?e(\d{2})(\d{2})\./i', $relname, $matches)) {
  473. $showInfo['name'] = $matches[1];
  474. $showInfo['season'] = intval($matches[2]);
  475. $showInfo['episode'] = array(intval($matches[3]), intval($matches[4]));
  476. //S01E01
  477. //S01.E01
  478. } elseif (preg_match('/^(.*?)[\. ]s(\d{1,2})\.?e(\d{1,3})\.?/i', $relname, $matches)) {
  479. $showInfo['name'] = $matches[1];
  480. $showInfo['season'] = intval($matches[2]);
  481. $showInfo['episode'] = intval($matches[3]);
  482. //S01
  483. } elseif (preg_match('/^(.*?)[\. ]s(\d{1,2})\./i', $relname, $matches)) {
  484. $showInfo['name'] = $matches[1];
  485. $showInfo['season'] = intval($matches[2]);
  486. $showInfo['episode'] = 'all';
  487. //S01D1
  488. //S1D1
  489. } elseif (preg_match('/^(.*?)[\. ]s(\d{1,2})d\d{1}\./i', $relname, $matches)) {
  490. $showInfo['name'] = $matches[1];
  491. $showInfo['season'] = intval($matches[2]);
  492. $showInfo['episode'] = 'all';
  493. //1x01
  494. } elseif (preg_match('/^(.*?)[\. ](\d{1,2})x(\d{1,3})\./i', $relname, $matches)) {
  495. $showInfo['name'] = $matches[1];
  496. $showInfo['season'] = intval($matches[2]);
  497. $showInfo['episode'] = intval($matches[3]);
  498. //2009.01.01
  499. //2009-01-01
  500. } elseif (preg_match('/^(.*?)[\. ](19|20)(\d{2})[\.\-](\d{2})[\.\-](\d{2})\./i', $relname, $matches)) {
  501. $showInfo['name'] = $matches[1];
  502. $showInfo['season'] = $matches[2].$matches[3];
  503. $showInfo['episode'] = $matches[4].'/'.$matches[5];
  504. $showInfo['airdate'] = $matches[2].$matches[3].'-'.$matches[4].'-'.$matches[5]; //yy-m-d
  505. //01.01.2009
  506. } elseif (preg_match('/^(.*?)[\. ](\d{2}).(\d{2})\.(19|20)(\d{2})\./i', $relname, $matches)) {
  507. $showInfo['name'] = $matches[1];
  508. $showInfo['season'] = $matches[4].$matches[5];
  509. $showInfo['episode'] = $matches[2].'/'.$matches[3];
  510. $showInfo['airdate'] = $matches[4].$matches[5].'-'.$matches[2].'-'.$matches[3]; //yy-m-d
  511. //01.01.09
  512. } elseif (preg_match('/^(.*?)[\. ](\d{2}).(\d{2})\.(\d{2})\./i', $relname, $matches)) {
  513. $showInfo['name'] = $matches[1];
  514. $showInfo['season'] = ($matches[4] <= 99 && $matches[4] > 15) ? '19'.$matches[4] : '20'.$matches[4];
  515. $showInfo['episode'] = $matches[2].'/'.$matches[3];
  516. $showInfo['airdate'] = $showInfo['season'].'-'.$matches[2].'-'.$matches[3]; //yy-m-d
  517. //2009.E01
  518. } elseif (preg_match('/^(.*?)[\. ]20(\d{2})\.e(\d{1,3})\./i', $relname, $matches)) {
  519. $showInfo['name'] = $matches[1];
  520. $showInfo['season'] = '20'.$matches[2];
  521. $showInfo['episode'] = intval($matches[3]);
  522. //2009.Part1
  523. } elseif (preg_match('/^(.*?)[\. ]20(\d{2})\.Part(\d{1,2})\./i', $relname, $matches)) {
  524. $showInfo['name'] = $matches[1];
  525. $showInfo['season'] = '20'.$matches[2];
  526. $showInfo['episode'] = intval($matches[3]);
  527. //Part1/Pt1
  528. } elseif (preg_match('/^(.*?)[\. ](?:Part|Pt)\.?(\d{1,2})\./i', $relname, $matches)) {
  529. $showInfo['name'] = $matches[1];
  530. $showInfo['season'] = 1;
  531. $showInfo['episode'] = intval($matches[2]);
  532. //The.Pacific.Pt.VI.HDTV.XviD-XII / Part.IV
  533. } elseif (preg_match('/^(.*?)[\. ](?:Part|Pt)\.?([ivx]+)/i', $relname, $matches)) {
  534. $showInfo['name'] = $matches[1];
  535. $showInfo['season'] = 1;
  536. $epLow = strtolower($matches[2]);
  537. switch($epLow) {
  538. case 'i': $e = 1; break;
  539. case 'ii': $e = 2; break;
  540. case 'iii': $e = 3; break;
  541. case 'iv': $e = 4; break;
  542. case 'v': $e = 5; break;
  543. case 'vi': $e = 6; break;
  544. case 'vii': $e = 7; break;
  545. case 'viii': $e = 8; break;
  546. case 'ix': $e = 9; break;
  547. case 'x': $e = 10; break;
  548. case 'xi': $e = 11; break;
  549. case 'xii': $e = 12; break;
  550. case 'xiii': $e = 13; break;
  551. case 'xiv': $e = 14; break;
  552. case 'xv': $e = 15; break;
  553. case 'xvi': $e = 16; break;
  554. case 'xvii': $e = 17; break;
  555. case 'xviii': $e = 18; break;
  556. case 'xix': $e = 19; break;
  557. case 'xx': $e = 20; break;
  558. }
  559. $showInfo['episode'] = $e;
  560. //Band.Of.Brothers.EP06.Bastogne.DVDRiP.XviD-DEiTY
  561. } elseif (preg_match('/^(.*?)[\. ]EP?\.?(\d{1,3})/i', $relname, $matches)) {
  562. $showInfo['name'] = $matches[1];
  563. $showInfo['season'] = 1;
  564. $showInfo['episode'] = intval($matches[2]);
  565. //Season.1
  566. } elseif (preg_match('/^(.*?)[\. ]Seasons?\.?(\d{1,2})/i', $relname, $matches)) {
  567. $showInfo['name'] = $matches[1];
  568. $showInfo['season'] = intval($matches[2]);
  569. $showInfo['episode'] = 'all';
  570. }
  571. if (!empty($showInfo['name'])) {
  572. //country or origin matching
  573. if (preg_match('/[\._ ](US|UK|AU|NZ|CA|NL|Canada|Australia|America)/', $showInfo['name'], $countryMatch))
  574. {
  575. if (strtolower($countryMatch[1]) == 'canada')
  576. $showInfo['country'] = 'CA';
  577. elseif (strtolower($countryMatch[1]) == 'australia')
  578. $showInfo['country'] = 'AU';
  579. elseif (strtolower($countryMatch[1]) == 'america')
  580. $showInfo['country'] = 'US';
  581. else
  582. $showInfo['country'] = strtoupper($countryMatch[1]);
  583. }
  584. //clean show name
  585. $showInfo['cleanname'] = $this->cleanName($showInfo['name']);
  586. //check for dates instead of seasons
  587. if (strlen($showInfo['season']) == 4) {
  588. $showInfo['seriesfull'] = $showInfo['season']."/".$showInfo['episode'];
  589. } else {
  590. //get year if present (not for releases with dates as seasons)
  591. if (preg_match('/[\._ ](19|20)(\d{2})/i', $relname, $yearMatch))
  592. $showInfo['year'] = $yearMatch[1].$yearMatch[2];
  593. $showInfo['season'] = sprintf('S%02d', $showInfo['season']);
  594. //check for multi episode release
  595. if (is_array($showInfo['episode'])) {
  596. $tmpArr = array();
  597. foreach ($showInfo['episode'] as $ep) {
  598. $tmpArr[] = sprintf('E%02d', $ep);
  599. }
  600. $showInfo['episode'] = implode('', $tmpArr);
  601. } else {
  602. $showInfo['episode'] = sprintf('E%02d', $showInfo['episode']);
  603. }
  604. $showInfo['seriesfull'] = $showInfo['season'].$showInfo['episode'];
  605. }
  606. $showInfo['airdate'] = (!empty($showInfo['airdate'])) ? $showInfo['airdate'].' 00:00:00' : '';
  607. return $showInfo;
  608. }
  609. return false;
  610. }
  611. public function getGenres()
  612. {
  613. return array(
  614. 'Action',
  615. 'Adult/Porn',
  616. 'Adventure',
  617. 'Anthology',
  618. 'Arts & Crafts',
  619. 'Automobiles',
  620. 'Buy, Sell & Trade',
  621. 'Celebrities',
  622. 'Children',
  623. 'Cinema/Theatre',
  624. 'Comedy',
  625. 'Cooking/Food',
  626. 'Crime',
  627. 'Current Events',
  628. 'Dance',
  629. 'Debate',
  630. 'Design/Decorating',
  631. 'Discovery/Science',
  632. 'Drama',
  633. 'Educational',
  634. 'Family',
  635. 'Fantasy',
  636. 'Fashion/Make-up',
  637. 'Financial/Business',
  638. 'Fitness',
  639. 'Garden/Landscape',
  640. 'History',
  641. 'Horror/Supernatural',
  642. 'Housing/Building',
  643. 'How To/Do It Yourself',
  644. 'Interview',
  645. 'Lifestyle',
  646. 'Literature',
  647. 'Medical',
  648. 'Military/War',
  649. 'Music',
  650. 'Mystery',
  651. 'Pets/Animals',
  652. 'Politics',
  653. 'Puppets',
  654. 'Religion',
  655. 'Romance/Dating',
  656. 'Sci-Fi',
  657. 'Sketch/Improv',
  658. 'Soaps',
  659. 'Sports',
  660. 'Super Heroes',
  661. 'Talent',
  662. 'Tech/Gaming',
  663. 'Teens',
  664. 'Thriller',
  665. 'Travel',
  666. 'Western',
  667. 'Wildlife'
  668. );
  669. }
  670. }
  671. ?>