tvdb.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?
  2. require 'config.php';
  3. $LAST_TVDB_DOWNLOAD = 0;
  4. # Parse and input URL for TVDB ID and LID
  5. function parseTVDBURL($url) {
  6. $retval = array(
  7. 'tvdb-id' => false,
  8. 'tvdb-lid' => false,
  9. );
  10. # Accept both the raw and encoded verisons of the URL
  11. if (preg_match('/(?:\?|\&(?:amp;)?)(?:series)?id=(\d+)/', $url, $matches)) {
  12. $retval['tvdb-id'] = $matches[1];
  13. }
  14. if (preg_match('/(?:\?|\&(?:amp;)?)lid=(\d+)/', $url, $matches)) {
  15. $retval['tvdb-lid'] = $matches[1];
  16. }
  17. return $retval;
  18. }
  19. # Build a series URL from the ID and optional LID
  20. function TVDBURL($id, $lid) {
  21. global $TVDB_URL;
  22. $url = false;
  23. if (!$lid) {
  24. $lid = $TVDB_LANG_ID;
  25. }
  26. if ($id) {
  27. $url = $TVDB_URL . '&id=' . $id . '&lid=' . $lid;
  28. }
  29. return $url;
  30. }
  31. function getTVDBPage($id, $lid) {
  32. # If TVDB is not enabled return FALSE
  33. global $ENABLE_TVDB;
  34. if (!$ENABLE_TVDB) {
  35. return false;
  36. }
  37. # Sleep between downloads to avoid TVDB bans
  38. global $TVDB_DELAY;
  39. global $LAST_TVDB_DOWNLOAD;
  40. if (time() - $LAST_TVDB_DOWNLOAD < $TVDB_DELAY) {
  41. sleep(rand($TVDB_DELAY, 2 * $TVDB_DELAY));
  42. }
  43. $LAST_TVDB_DOWNLOAD = time();
  44. # Download with a timeout
  45. global $TVDB_TIMEOUT;
  46. $url = TVDBURL($id, $lid);
  47. $ctx = stream_context_create(array(
  48. 'http' => array( 'timeout' => $TVDB_TIMEOUT )
  49. ));
  50. return @file_get_contents($url, 0, $ctx);
  51. }
  52. # Plain-text title of a TVDB entity (or FALSE on failure)
  53. function getTVDBTitle($id, $lid) {
  54. $retval = false;
  55. # If TVDB is not enabled return FALSE
  56. global $ENABLE_TVDB;
  57. if (!$ENABLE_TVDB) {
  58. return false;
  59. }
  60. $page = getTVDBPage($id, $lid);
  61. if ($page) {
  62. if (preg_match('/\<h1\>([^\>]+)\<\/h1\>/i', $page, $matches)) {
  63. $retval = $matches[1];
  64. }
  65. }
  66. return $retval;
  67. }
  68. # Get the seasons of a TVDB entity
  69. function getTVDBSeasons($id, $lid) {
  70. $retval = false;
  71. # If TVDB is not enabled return a fake season list
  72. global $ENABLE_TVDB;
  73. if (!$ENABLE_TVDB) {
  74. return array(1 => true);
  75. }
  76. # Download and parse the series page for a list of seasons
  77. $page = getTVDBPage($id, $lid);
  78. if ($page) {
  79. if (preg_match_all('/class=\"seasonlink\"[^\>]*\>([^\<]+)\<\/a\>/i', $page, $matches)) {
  80. $retval = array();
  81. foreach ($matches[1] as $val) {
  82. if (preg_match('/^\d+$/', $val)) {
  83. $retval[ $val ] = true;
  84. } else if ($val == 'Specials') {
  85. $retval[0] = true;
  86. }
  87. }
  88. }
  89. }
  90. return $retval;
  91. }
  92. ?>