tvdb.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?
  2. $TVDB_DOWNLOAD_TIME = 0;
  3. $TVDB_DOWNLOAD_COUNT = 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 $TVDB_DELAY_COUNT;
  40. global $TVDB_DOWNLOAD_TIME;
  41. global $TVDB_DOWNLOAD_COUNT;
  42. if ($TVDB_DOWNLOAD_COUNT >= $TVDB_DELAY_COUNT && time() - $TVDB_DOWNLOAD_TIME < $TVDB_DELAY) {
  43. sleep(rand($TVDB_DELAY, 2 * $TVDB_DELAY));
  44. }
  45. $TVDB_DOWNLOAD_COUNT++;
  46. $TVDB_DOWNLOAD_TIME = time();
  47. # Download with a timeout
  48. global $TVDB_TIMEOUT;
  49. $url = TVDBURL($id, $lid);
  50. $ctx = stream_context_create(array(
  51. 'http' => array( 'timeout' => $TVDB_TIMEOUT )
  52. ));
  53. return @file_get_contents($url, 0, $ctx);
  54. }
  55. # Plain-text title of a TVDB entity (or FALSE on failure)
  56. function getTVDBTitle($id, $lid) {
  57. $retval = false;
  58. # If TVDB is not enabled return FALSE
  59. global $ENABLE_TVDB;
  60. if (!$ENABLE_TVDB) {
  61. return false;
  62. }
  63. $page = getTVDBPage($id, $lid);
  64. if ($page) {
  65. if (preg_match('/\<h1\>([^\>]+)\<\/h1\>/i', $page, $matches)) {
  66. $retval = $matches[1];
  67. }
  68. }
  69. return $retval;
  70. }
  71. # Get the seasons of a TVDB entity
  72. function getTVDBSeasons($id, $lid) {
  73. $retval = false;
  74. # If TVDB is not enabled return a fake season list
  75. global $ENABLE_TVDB;
  76. if (!$ENABLE_TVDB) {
  77. return array(1 => true);
  78. }
  79. # Download and parse the series page for a list of seasons
  80. $page = getTVDBPage($id, $lid);
  81. if ($page) {
  82. if (preg_match_all('/class=\"seasonlink\"[^\>]*\>([^\<]+)\<\/a\>/i', $page, $matches)) {
  83. $retval = array();
  84. foreach ($matches[1] as $val) {
  85. if (preg_match('/^\d+$/', $val)) {
  86. $retval[ $val ] = true;
  87. } else if ($val == 'Specials') {
  88. $retval[0] = true;
  89. }
  90. }
  91. }
  92. }
  93. return $retval;
  94. }
  95. ?>