tvdb.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. $url = adjustProtocol($url);
  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 and forced IPv4 resolution
  49. global $TVDB_TIMEOUT;
  50. $ch = curl_init();
  51. curl_setopt($ch, CURLOPT_URL, TVDBURL($id, $lid));
  52. curl_setopt($ch, CURLOPT_TIMEOUT, $TVDB_TIMEOUT);
  53. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  54. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  55. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  56. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  57. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  58. $body = curl_exec($ch);
  59. if ($err = curl_error($ch)) {
  60. echo '<!-- cURL Error: ' . $err . "-->\n";
  61. }
  62. curl_close($ch);
  63. return $body;
  64. }
  65. # Plain-text title of a TVDB entity (or FALSE on failure)
  66. function getTVDBTitle($id, $lid) {
  67. $retval = false;
  68. # If TVDB is not enabled return FALSE
  69. global $ENABLE_TVDB;
  70. if (!$ENABLE_TVDB) {
  71. return false;
  72. }
  73. $page = getTVDBPage($id, $lid);
  74. if ($page) {
  75. if (preg_match('/\<h1\>([^\>]+)\<\/h1\>/i', $page, $matches)) {
  76. $retval = $matches[1];
  77. }
  78. }
  79. return $retval;
  80. }
  81. # Get the seasons of a TVDB entity
  82. function getTVDBSeasons($id, $lid) {
  83. $retval = false;
  84. # If TVDB is not enabled return a fake season list
  85. global $ENABLE_TVDB;
  86. if (!$ENABLE_TVDB) {
  87. return array(1 => true);
  88. }
  89. # Download and parse the series page for a list of seasons
  90. $page = getTVDBPage($id, $lid);
  91. if ($page) {
  92. if (preg_match_all('/class=\"seasonlink\"[^\>]*\>([^\<]+)\<\/a\>/i', $page, $matches)) {
  93. $retval = array();
  94. foreach ($matches[1] as $val) {
  95. if (preg_match('/^\d+$/', $val)) {
  96. $retval[ $val ] = true;
  97. } else if ($val == 'Specials') {
  98. $retval[0] = true;
  99. }
  100. }
  101. }
  102. }
  103. return $retval;
  104. }
  105. ?>