Assembla home | Assembla project page
 

root/trunk/Phergie/Plugin/UrbanDictionary.php

Revision 335, 3.0 kB (checked in by tobias382, 8 months ago)

Fixes #83 Updated scraping logic for the Urban Dictionary plugin (thanks to Greg Schoen)

Line 
1 <?php
2
3 /**
4  * Performs a lookup on request for a given term on the Urban Dictionary web
5  * site and responds with a message containing the first result.
6  */
7 class Phergie_Plugin_UrbanDictionary extends Phergie_Plugin_Abstract_Command
8 {
9     /**
10      * Returns whether or not the plugin's dependencies are met.
11      *
12      * @param Phergie_Driver_Abstract $client Client instance
13      * @param array $plugins List of short names for plugins that the
14      *                       bootstrap file intends to instantiate
15      * @see Phergie_Plugin_Abstract_Base::checkDependencies()
16      * @return bool TRUE if dependencies are met, FALSE otherwise
17      */
18     public static function checkDependencies(Phergie_Driver_Abstract $client, array $plugins)
19     {
20         if (!self::staticPluginLoaded('TinyUrl', $client, $plugins)) {
21             return 'TinyUrl plugin must be enabled';
22         }
23
24         return true;
25     }
26
27     /**
28      * Handles Urban Dictionary definition requests.
29      *
30      * @param string $term Term to search for
31      * @return void
32      */
33     public function onDoUd($term)
34     {
35         $target = $this->event->getNick();
36         $source = $this->event->getSource();
37
38         $url = 'http://www.urbandictionary.com/define.php?term=' . urlencode($term);
39         $contents = @file_get_contents($url);
40
41         if ($contents === false) {
42             $this->doNotice($target, 'Urban Dictionary is currently inaccessible');
43         } elseif (strpos($contents, 'isn\'t defined') !== false) {
44             $url = Phergie_Plugin_TinyUrl::get('http://urbandictionary.com/insert.php?word=' . $term);
45             $this->doNotice($target, $term . ' is not defined yet [ ' . $url . ' ]');
46         } else {
47             $start = strpos($contents, '<div class=\'definition\'>');
48             $end = strpos($contents, '<div', $start + 1);
49             if ($end === false) {
50                 $end = strpos($contents, '</div>', $start);
51             }
52             $contents = substr($contents, $start, $end - $start);
53             $contents = html_entity_decode(strip_tags($contents));
54             $contents = $term . ': ' . trim(preg_replace('/[\r\n\t ]+/', ' ', $contents));
55
56             $url = '[ ' . Phergie_Plugin_TinyUrl::get($url) . ' ] ';
57
58             /**
59              * Not sure why, but this seems to be the magic number for
60              * ensuring that the text isn't truncated. The hostmask isn't
61              * included in what's sent to the server. The maximum message
62              * length should be 510 characters according to the IRC RFC.
63              */
64             $max = 445 - strlen($source) - strlen($url);
65             if (strlen($contents) > $max) {
66                 $contents = substr($contents, 0, $max);
67                 $end = strrpos($contents, ' ');
68                 if ($end === false) {
69                     $end = $max;
70                 }
71                 $contents = substr($contents, 0, $end) . '...';
72             }
73             $contents = $url . $contents;
74
75             $this->doPrivmsg($source, $contents);
76         }
77     }
78 }
79
Note: See TracBrowser for help on using the browser.