]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Youtube.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Youtube.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie
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
20  */
21
22 /**
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.
26  *
27  * @category Phergie
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
34  */
35 class Phergie_Plugin_Youtube extends Phergie_Plugin_Abstract
36 {
37     /**
38      * Checks for dependencies.
39      *
40      * @return void
41      */
42     public function onLoad()
43     {
44         $plugins = $this->getPluginHandler();
45         $plugins->getPlugin('Command');
46         $plugins->getPlugin('Http');
47         if ($url = $plugins->getPlugin('Url')) {
48             $url->registerRenderer($this);
49         }
50     }
51
52     /**
53      * Queries the YouTube video search web service, processes the first
54      * result, and sends a message back to the current event source.
55      *
56      * @param string $query Search term
57      *
58      * @return object YouTube result object
59      */
60     protected function queryYoutube($query)
61     {
62         $url = 'http://gdata.youtube.com/feeds/api/videos';
63         $params = array(
64             'max-results' => '1',
65             'alt' => 'json',
66             'q' => $query
67         );
68         $http = $this->plugins->getPlugin('Http');
69         $response = $http->get($url, $params);
70         $json = $response->getContent();
71
72         $entries = $json->feed->entry;
73         if (!$entries) {
74             $this->doNotice($this->event->getNick(), 'Query returned no results');
75             return;
76         }
77         $entry = reset($entries);
78
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;
87
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);
96
97         $format = $this->getConfig('youtube.format');
98         if (!$format) {
99             $format = '%nick%:'
100                 . ' [ %link% ]'
101                 . ' "%title%" by %author%,'
102                 . ' Length %minutes%:%seconds%,'
103                 . ' Published %published%,'
104                 . ' Views %views%,'
105                 . ' Rating %rating%';
106         }
107
108         $replacements = array(
109             'nick' => $nick,
110             'link' => $link,
111             'title' => $title,
112             'author' => $author,
113             'minutes' => $minutes,
114             'seconds' => $seconds,
115             'published' => $published,
116             'views' => $views,
117             'rating' => $rating
118         );
119
120         $msg = $format;
121         foreach ($replacements as $from => $to) {
122             $msg = str_replace('%' . $from . '%', $to, $msg);
123         }
124         $this->doPrivmsg($this->event->getSource(), $msg);
125     }
126
127     /**
128      * Returns the first result of a YouTube search.
129      *
130      * @param string $query Search query
131      *
132      * @return void
133      */
134     public function onCommandYoutube($query)
135     {
136         $this->queryYoutube($query);
137     }
138
139     /**
140      * Renders YouTube URLs.
141      *
142      * @param array $parsed parse_url() output for the URL to render
143      *
144      * @return boolean TRUE if the URL was rendered successfully, FALSE
145      *         otherwise
146      */
147     public function renderUrl(array $parsed)
148     {
149         switch ($parsed['host']) {
150             case 'youtu.be':
151                 $v = ltrim($parsed['path'], '/');
152                 break;
153             case 'youtube.com':
154             case 'www.youtube.com':
155                 parse_str($parsed['query'], $parsed_query);
156                 if (!empty($parsed_query['v'])) {
157                     $v = $parsed_query['v'];
158                     break;
159                 }
160             default:
161                 return false;
162         }
163
164         $this->queryYoutube($v);
165
166         return true;
167     }
168 }