tvdb.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?
  2. require 'config.php';
  3. # Parse and input URL for TVDB ID and LID
  4. function parseTVDBURL($url) {
  5. $retval = array(
  6. 'tvdb-id' => false,
  7. 'tvdb-lid' => false,
  8. );
  9. # Accept both the raw and encoded verisons of the URL
  10. if (preg_match('/(?:\?|\&(?:amp;)?)(?:series)?id=(\d+)/', $url, $matches)) {
  11. $retval['tvdb-id'] = $matches[1];
  12. }
  13. if (preg_match('/(?:\?|\&(?:amp;)?)lid=(\d+)/', $url, $matches)) {
  14. $retval['tvdb-lid'] = $matches[1];
  15. }
  16. return $retval;
  17. }
  18. # Build a series URL from the ID and optional LID
  19. function TVDBURL($id, $lid) {
  20. global $TVDB_URL;
  21. $url = false;
  22. if (!$lid) {
  23. $lid = $TVDB_LANG_ID;
  24. }
  25. if ($id) {
  26. $url = $TVDB_URL . '&id=' . $id . '&lid=' . $lid;
  27. }
  28. return $url;
  29. }
  30. function getTVDBPage($id, $lid) {
  31. $url = TVDBURL($id, $lid);
  32. $ctx = stream_context_create(array(
  33. 'http' => array( 'timeout' => 15 )
  34. ));
  35. return @file_get_contents($url, 0, $ctx);
  36. }
  37. # Plain-text title of a TVDB entity (or FALSE on failure)
  38. function getTVDBTitle($id, $lid) {
  39. $retval = false;
  40. $page = getTVDBPage($id, $lid);
  41. if ($page) {
  42. if (preg_match('/\<h1\>([^\>]+)\<\/h1\>/i', $page, $matches)) {
  43. $retval = $matches[1];
  44. }
  45. }
  46. return $retval;
  47. }
  48. # Get the seasons of a TVDB entity
  49. function getTVDBSeasons($id, $lid) {
  50. $retval = false;
  51. $page = getTVDBPage($id, $lid);
  52. if ($page) {
  53. if (preg_match_all('/class=\"seasonlink\"[^\>]*\>([^\<]+)\<\/a\>/i', $page, $matches)) {
  54. $retval = array();
  55. foreach ($matches[1] as $val) {
  56. if (preg_match('/^\d+$/', $val)) {
  57. $retval[ $val ] = true;
  58. } else if ($val == 'Specials') {
  59. $retval[0] = true;
  60. }
  61. }
  62. }
  63. }
  64. return $retval;
  65. }
  66. ?>