tvdb.php 2.4 KB

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