/** * Performs a Google search to convert a value from one unit to another. * * @param string $unit Source metric * @param string $to Value to be converted * @param string $unit2 Destination metric * * @return void */ public function onCommandConvert($unit, $to, $unit2) { $url = 'http://www.google.com/search?q=' . urlencode($unit . ' ' . $to . ' ' . $unit2); $response = $this->http->get($url); $contents = $response->getContent(); $event = $this->getEvent(); $source = $event->getSource(); $nick = $event->getNick(); if (empty($contents)) { $this->doPrivmsg($target, $nick . ', sorry, I can\'t give you an answer right now.'); return; } $doc = new DomDocument(); $doc->loadHTML($contents); foreach ($doc->getElementsByTagName('h2') as $element) { if ($element->getAttribute('class') == 'r') { $children = $element->childNodes; $text = str_replace(array(chr(195), chr(151), chr(160)), array('x', '', ' '), $children->item(0)->nodeValue); if ($children->length >= 3) { $text .= '^' . $children->item(1)->nodeValue . $children->item(2)->nodeValue; } } } if (isset($text)) { $this->doPrivmsg($source, $nick . ': ' . $text); } else { $this->doPrivmsg($target, $nick . ', sorry I can\'t do that.'); } }
/** * Simple Scrobbler API function to get a formatted string of the most * recent track played by a user. * * @param string $user Username to look up * @param string $url Base URL of the scrobbler service * @param string $key Scrobbler service API key * * @return string Formatted string of the most recent track played */ public function getScrobbled($user, $url, $key) { $event = $this->getEvent(); $user = $user ? $user : $event->getNick(); $url = sprintf($url . $this->query, urlencode($user), urlencode($key)); $response = $this->http->get($url); if ($response->isError()) { $this->doNotice($event->getNick(), 'Can\'t find status for ' . $user . ': HTTP ' . $response->getCode() . ' ' . $response->getMessage()); return false; } $xml = $response->getContent(); if ($xml->error) { $this->doNotice($event->getNick(), 'Can\'t find status for ' . $user . ': API ' . $xml->error); return false; } $recenttracks = $xml->recenttracks; $track = $recenttracks->track[0]; // If the user exists but has not scrobbled anything, the result will // be empty. if (empty($track->name) && empty($track->artist)) { $this->doNotice($event->getNick(), 'Can\'t find track information for ' . $recenttracks['user']); return false; } if (isset($track['nowplaying'])) { $msg = sprintf('%s is listening to %s by %s', $recenttracks['user'], $track->name, $track->artist); } else { $msg = sprintf('%s, %s was listening to %s by %s', date('j M Y, H:i', (int) $track->date['uts']), $recenttracks['user'], $track->name, $track->artist); } if ($track->streamable == 1) { $msg .= ' - ' . $track->url; } return $msg; }
/** * Simple Scrobbler API function to get a formatted string of the most * recent track played by a user. * * @param string $user Username to look up * @param string $url Base URL of the scrobbler service * @param string $key Scrobbler service API key * * @return string Formatted string of the most recent track played */ public function getScrobbled($user, $url, $key) { $event = $this->getEvent(); $user = $user ? $user : $event->getNick(); $url = sprintf($url . $this->query, urlencode($user), urlencode($key)); $response = $this->http->get($url); if ($response->isError()) { $this->doNotice($event->getSource(), 'Can\'t find status for ' . $user . ': HTTP ' . $response->getCode() . ' ' . $response->getMessage()); return false; } $xml = $response->getContent(); if ($xml->error) { $this->doNotice($event->getSource(), 'Can\'t find status for ' . $user . ': API ' . $xml->error); return false; } $recenttracks = $xml->recenttracks; $track = $recenttracks->track[0]; if (isset($track['nowplaying'])) { $msg = sprintf('%s is listening to %s by %s', $recenttracks['user'], $track->name, $track->artist); } else { $msg = sprintf('%s, %s was listening to %s by %s', $track->date, $recenttracks['user'], $track->name, $track->artist); } if ($track->streamable == 1) { $msg .= ' - ' . $track->url; } return $msg; }
/** * Get the necessary content and returns the search result. * * @param string $location Location term * * @return string The search result * @todo Try to optimize pregs */ protected function getWeather($location) { $url = $this->url . urlencode($location); $response = $this->http->get($url); $content = $response->getContent(); preg_match_all("#<div><span class=\"small\">(.*?)<\\/span><\\/div>#im", $content, $matches); $location = $matches[1][0]; if (!empty($location)) { preg_match_all("#<div class=\"large\" >(.*?)<br \\/>#im", $content, $matches); $temp_numb = $matches[1][0]; preg_match_all("#<br \\/>(.*?)<\\/div><div id=\"remark\"><br \\/>#im", $content, $matches); $temp_desc = $matches[1][0]; preg_match_all("#<div id=\"remark\"><br \\/>\n<span>(.*?)<\\/span><\\/div>#im", $content, $matches); $remark = $matches[1][0]; $result = "{$location}: {$temp_numb} {$temp_desc} ({$remark})"; $result = preg_replace('/</', ' <', $result); $result = strip_tags($result); return html_entity_decode($result); } else { return "I have no idea where is this f*****g location!"; } }
/** * Get the necessary content and returns the search result. * * @param string $location Location term * * @return string|bool Search result or FALSE if none is found * @todo Try to optimize pregs */ protected function getWeather($location) { $url = $this->url . urlencode($location); $response = $this->http->get($url); $content = $response->getContent(); preg_match_all('#<div><span class="small">(.*?)<\\/span><\\/div>#im', $content, $matches); $location = $matches[1][0]; if (!empty($location)) { preg_match_all('#<div class="large" >(.*?)<br \\/>#im', $content, $matches); $temp_numb = (int) $matches[1][0]; $temp_numb .= ' F / ' . round(($temp_numb - 32) / 1.8, 0) . ' C?!'; preg_match_all('#<br \\/>(.*?)<\\/div><div id="remark"><br \\/>#im', $content, $matches); $temp_desc = $matches[1][0]; preg_match_all('#<div id="remark"><br \\/>\\n<span>(.*?)<\\/span><\\/div>#im', $content, $matches); $remark = $matches[1][0]; $result = "{$location}: {$temp_numb} {$temp_desc} ({$remark})"; $result = preg_replace('/</', ' <', $result); $result = strip_tags($result); return html_entity_decode($result); } else { return 'No f*****g clue where that is.'; } }
/** * Fetches a chayism. * * @return string|bool Fetched chayism or FALSE if the operation failed */ public function getChayism() { return $this->http->get(self::URL)->getContent(); }
/** * Handles beerscore commands. * * @param string $searchstring String to use in seaching for beer scores * * @return void */ public function onCommandBeerscore($searchstring) { $event = $this->getEvent(); $target = $event->getNick(); $source = $event->getSource(); $apiurl = self::API_BASE_URL . rawurlencode($searchstring); $response = $this->http->get($apiurl); if ($response->isError()) { $this->doNotice($target, 'Score not found (or failed to contact API)'); return; } $result = $response->getContent(); switch ($result->type) { case self::TYPE_SCORE: // small enough number to get scores foreach ($result->beer as $beer) { if ($beer->score === -1) { $score = '(not rated)'; } else { $score = $beer->score; } $str = "{$target}: rating for {$beer->name}" . " = {$score} ({$beer->url})"; $this->doPrivmsg($source, $str); } break; case self::TYPE_SEARCH: // only beer names, no scores $str = ''; $found = 0; foreach ($result->beer as $beer) { if (isset($beer->score)) { ++$found; if ($beer->score === -1) { $score = '(not rated)'; } else { $score = $beer->score; } $str = "{$target}: rating for {$beer->name}" . " = {$score} ({$beer->url})"; $this->doPrivmsg($source, $str); } else { $str .= "({$beer->name} -> {$beer->url}) "; } } $foundnum = $result->num - $found; $more = $found ? 'more ' : ''; $str = "{$target}: {$foundnum} {$more}results... {$str}"; $this->doPrivmsg($source, $str); break; case self::TYPE_REFINE: // Too many results; only output search URL if ($result->num < 100) { $num = $result->num; } else { $num = 'at least 100'; } $resultsword = $result->num > 1 ? 'results' : 'result'; $str = "{$target}: {$num} {$resultsword}; {$result->searchurl}"; $this->doPrivmsg($source, $str); break; } }