9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
15 * @package Phergie_Plugin_Youtube
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie_Plugin_Youtube
23 * Provides commands used to access several services offered by Google
24 * including search, translation, weather, maps, and currency and general
25 * value unit conversion.
28 * @package Phergie_Plugin_Youtube
29 * @author Phergie Development Team <team@phergie.org>
30 * @license http://phergie.org/license New BSD License
31 * @link http://pear.phergie.org/package/Phergie_Plugin_Youtube
32 * @uses Phergie_Plugin_Command pear.phergie.org
33 * @uses Phergie_Plugin_Http pear.phergie.org
35 class Phergie_Plugin_Youtube extends Phergie_Plugin_Abstract
38 * Checks for dependencies.
42 public function onLoad()
44 $plugins = $this->getPluginHandler();
45 $plugins->getPlugin('Command');
46 $plugins->getPlugin('Http');
47 if ($url = $plugins->getPlugin('Url')) {
48 $url->registerRenderer($this);
53 * Queries the YouTube video search web service, processes the first
54 * result, and sends a message back to the current event source.
56 * @param string $query Search term
58 * @return object YouTube result object
60 protected function queryYoutube($query)
62 $url = 'http://gdata.youtube.com/feeds/api/videos';
68 $http = $this->plugins->getPlugin('Http');
69 $response = $http->get($url, $params);
70 $json = $response->getContent();
72 $entries = $json->feed->entry;
74 $this->doNotice($this->event->getNick(), 'Query returned no results');
77 $entry = reset($entries);
79 $nick = $this->event->getNick();
80 $link = $entry->link[0]->href;
81 $title = $entry->title->{'$t'};
82 $author = $entry->author[0]->name->{'$t'};
83 $seconds = $entry->{'media$group'}->{'yt$duration'}->seconds;
84 $published = $entry->published->{'$t'};
85 $views = $entry->{'yt$statistics'}->viewCount;
86 $rating = $entry->{'gd$rating'}->average;
88 $minutes = floor($seconds / 60);
89 $seconds = str_pad($seconds % 60, 2, '0', STR_PAD_LEFT);
90 $parsed_link = parse_url($link);
91 parse_str($parsed_link['query'], $parsed_query);
92 $link = 'http://youtu.be/' . $parsed_query['v'];
93 $published = date('n/j/y g:i A', strtotime($published));
94 $views = number_format($views, 0);
95 $rating = round($rating, 2);
97 $format = $this->getConfig('youtube.format');
101 . ' "%title%" by %author%,'
102 . ' Length %minutes%:%seconds%,'
103 . ' Published %published%,'
105 . ' Rating %rating%';
108 $replacements = array(
113 'minutes' => $minutes,
114 'seconds' => $seconds,
115 'published' => $published,
121 foreach ($replacements as $from => $to) {
122 $msg = str_replace('%' . $from . '%', $to, $msg);
124 $this->doPrivmsg($this->event->getSource(), $msg);
128 * Returns the first result of a YouTube search.
130 * @param string $query Search query
134 public function onCommandYoutube($query)
136 $this->queryYoutube($query);
140 * Renders YouTube URLs.
142 * @param array $parsed parse_url() output for the URL to render
144 * @return boolean TRUE if the URL was rendered successfully, FALSE
147 public function renderUrl(array $parsed)
149 switch ($parsed['host']) {
151 $v = ltrim('/', $parsed['path']);
154 case 'www.youtube.com':
155 parse_str($parsed['query'], $parsed_query);
156 if (!empty($parsed_query['v'])) {
157 $v = $parsed_query['v'];
164 $this->queryYoutube($v);