PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendors/php-irc/modules/imdb/imdb_mod.php

http://github.com/cakephp/cakebot
PHP | 262 lines | 244 code | 13 blank | 5 comment | 9 complexity | 34e8cb77a4375bd1787a7c143379f8a4 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. class imdb_mod extends module {
  4. public $title = "IMDB Parser";
  5. public $author = "Manick";
  6. public $version = "0.1";
  7. public function init()
  8. {
  9. $this->cache = array();
  10. // Add your timer declarations and whatever
  11. // else here...
  12. }
  13. public function destroy()
  14. {
  15. $this->cache = array();
  16. // Put code here to destroy the timers that you created in init()
  17. // and whatever else cleanup code you want.
  18. }
  19. private $cache;
  20. public function parse_imdb($line, $args)
  21. {
  22. if ($args['nargs'] <= 0)
  23. {
  24. $this->ircClass->notice($line['fromNick'], "Usage: !imdb <movie>");
  25. return;
  26. }
  27. $this->ircClass->notice($line['fromNick'], "Processing, please wait...");
  28. $query = strtolower($args['query']);
  29. $lArgs = explode(chr(32), $query);
  30. $tCount = count($lArgs);
  31. if ($tCount > 0)
  32. {
  33. $date = $lArgs[count($lArgs)-1];
  34. $title = strtolower(trim(str_replace($date, "", $query)));
  35. }
  36. if (isset($this->cache[$query]))
  37. {
  38. foreach ($this->cache[$query] AS $date => $link)
  39. break;
  40. $line["link"] = $link;
  41. $line["title"] = $query;
  42. $line["date"] = $date;
  43. $search = socket::generateGetQuery("", "www.imdb.com", "/title/" . $link . "/", "1.0");
  44. $this->ircClass->addQuery("www.imdb.com", 80, $search, $line, $this, "title_response");
  45. }
  46. else if ($tCount > 0 && isset($this->cache[$title]) && isset($this->cache[$title][$date]))
  47. {
  48. $link = $this->cache[$title][$date];
  49. $line["link"] = $link;
  50. $line["title"] = $title;
  51. $line["date"] = $date;
  52. $search = socket::generateGetQuery("", "www.imdb.com", "/title/" . $link . "/", "1.0");
  53. $this->ircClass->addQuery("www.imdb.com", 80, $search, $line, $this, "title_response");
  54. }
  55. else
  56. {
  57. $tquery = "q=" . urlencode($args['query']) . "&tt=on&mx=20";
  58. $search = socket::generateGetQuery($tquery, "www.imdb.com", "/find", "1.0");
  59. $this->ircClass->addQuery("www.imdb.com", 80, $search, $line, $this, "search_response");
  60. }
  61. }
  62. public function search_response($line, $args, $result, $site)
  63. {
  64. if ($result == QUERY_ERROR)
  65. {
  66. $this->ircClass->notice($line['fromNick'], "Error: " . $site);
  67. return;
  68. }
  69. $site = str_replace("&#160;", ";", $site);
  70. $site = str_replace("\r", "", $site);
  71. $site = html_entity_decode($site);
  72. preg_match_all("/<li>\s*<a href=\"\/title\/(.+?)\/.+?>(.+?)<\/a>\s*(\(.+?\)){1}?(\s)?(.+?)*?<\/li>/i", $site, $matches, PREG_PATTERN_ORDER);
  73. $topTen = array();
  74. for ($i = 0; $i < count($matches[1]); $i++)
  75. {
  76. if ($matches[4][$i] == " ")
  77. {
  78. continue;
  79. }
  80. $link = trim($matches[1][$i]);
  81. $title = trim($matches[2][$i]);
  82. $date = trim($matches[3][$i]);
  83. $lTitle = strtolower($title);
  84. if (!isset($this->cache[$lTitle]))
  85. {
  86. $this->cache[$lTitle] = array();
  87. $this->cache[$lTitle][$date] = $link;
  88. }
  89. else
  90. {
  91. if (!isset($this->cache[$lTitle][$date]))
  92. {
  93. $this->cache[$lTitle][$date] = $link;
  94. krsort($this->cache[$lTitle]);
  95. }
  96. }
  97. $topTen[] = $title . " " . $date;
  98. }
  99. $tkey = strtolower($args["query"]);
  100. $lArgs = explode(chr(32), $tkey);
  101. $tCount = count($lArgs);
  102. if ($tCount > 0)
  103. {
  104. $date = $lArgs[count($lArgs)-1];
  105. $title = trim(str_replace($date, "", $tkey));
  106. }
  107. if (isset($this->cache[$tkey]))
  108. {
  109. foreach ($this->cache[$tkey] AS $date => $link)
  110. break;
  111. $line["link"] = $link;
  112. $line["title"] = $tkey;
  113. $line["date"] = $date;
  114. $search = socket::generateGetQuery("", "www.imdb.com", "/title/" . $link . "/", "1.0");
  115. $this->ircClass->addQuery("www.imdb.com", 80, $search, $line, $this, "title_response");
  116. }
  117. else if ($tCount > 0 && isset($this->cache[$title]) && isset($this->cache[$title][$date]))
  118. {
  119. $link = $this->cache[$title][$date];
  120. $line["link"] = $link;
  121. $line["title"] = $title;
  122. $line["date"] = $date;
  123. $search = socket::generateGetQuery("", "www.imdb.com", "/title/" . $link . "/", "1.0");
  124. $this->ircClass->addQuery("www.imdb.com", 80, $search, $line, $this, "title_response");
  125. }
  126. else
  127. {
  128. $total = count($topTen);
  129. $total = $total > 10 ? 10 : $total;
  130. if ($total <= 0)
  131. {
  132. $this->ircClass->notice($line['fromNick'], "No responses from server. Try broadening your search. If you included a date, remove it and try again.");
  133. return;
  134. }
  135. $this->ircClass->notice($line['fromNick'], "Top " . $total . " responses from www.imdb.com");
  136. $resp = "";
  137. for ($i = 0; $i < $total; $i++)
  138. {
  139. $resp .= DARK . "[" . BRIGHT . $topTen[$i] . DARK . "] - ";
  140. }
  141. $multi = irc::multiLine($resp);
  142. foreach($multi AS $mult)
  143. {
  144. $this->ircClass->notice($line['fromNick'], $mult);
  145. }
  146. }
  147. }
  148. public function title_response($line, $args, $result, $site)
  149. {
  150. if ($result == QUERY_ERROR)
  151. {
  152. $this->ircClass->notice($line['fromNick'], "Error: " . $site);
  153. return;
  154. }
  155. $site = html_entity_decode($site);
  156. preg_match("/<title>(.+?)<\/title>/is", $site, $match);
  157. $site = preg_replace("/<(.+?)>/s", "", $site);
  158. $site = str_replace("\r", "", $site);
  159. preg_match_all("/([^\s\n]+?):[\s\n]*([^\n]+?)\n/", $site, $matches, PREG_PATTERN_ORDER);
  160. $var = array();
  161. $var['Title'] = $match[1];
  162. for ($i = 0; $i < count($matches[1]); $i++)
  163. {
  164. if (!isset($var[$matches[1][$i]]))
  165. {
  166. $var[$matches[1][$i]] = $matches[2][$i];
  167. }
  168. }
  169. $n_array = array("Title", "Runtime", "Genre", "Country", "Rating", "Outline");
  170. foreach ($n_array AS $item)
  171. {
  172. if (!isset($var[$item]) || $var[$item] == "")
  173. $var[$item] = "N/A";
  174. }
  175. $offsetA = strpos($var["Outline"], "(");
  176. if ($offsetA !== false)
  177. {
  178. $var["Outline"] = substr($var["Outline"], 0, $offsetA);
  179. }
  180. $Tline = DARK . "[ " . BRIGHT . UNDERLINE . "http://www.imdb.com/title/" . $line["link"] . "/" . UNDERLINE . DARK . " ]".
  181. " - [" . BOLD . "Title:" . BOLD . " " . BRIGHT . trim($var["Title"]) . DARK . "]".
  182. " - [" . BOLD . "Runtime:" . BOLD . " " . BRIGHT . trim($var["Runtime"]) . DARK . "]".
  183. " - [" . BOLD . "Genre:" . BOLD . " " . BRIGHT . trim($var["Genre"]) . DARK . "]".
  184. " - [" . BOLD . "Country:" . BOLD . " " . BRIGHT . trim($var["Country"]) . DARK . "]".
  185. " - [" . BOLD . "Rating:" . BOLD . " " . BRIGHT . trim($var["Rating"]) . DARK . "]";
  186. $Tline2 = DARK . "[" . BOLD . "Outline:" . BOLD . " " . trim($var["Outline"]) . "]";
  187. $this->ircClass->notice($line['fromNick'], $Tline);
  188. if ($var["Outline"] != "N/A")
  189. {
  190. $this->ircClass->notice($line['fromNick'], $Tline2);
  191. }
  192. $resp = "";
  193. $count = 0;
  194. foreach ($this->cache[$line["title"]] AS $date => $link)
  195. {
  196. if ($date != $line["date"])
  197. {
  198. $resp .= DARK . "[" . BRIGHT . $date . DARK . "] -";
  199. $count++;
  200. }
  201. }
  202. if ($count > 0)
  203. {
  204. $this->ircClass->notice($line['fromNick'], DARK . "There are " . $count . " other dates: " . $resp . " [use !imdb title (date)]");
  205. }
  206. }
  207. //Methods here:
  208. }
  209. ?>