1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17:
18: class ClipitVideo extends ClipitResource{
19: 20: 21:
22: const SUBTYPE = "ClipitVideo";
23: const REL_RESOURCE_TAG = "ClipitVideo-ClipitTag";
24: const REL_RESOURCE_PERFORMANCE = "ClipitVideo-ClipitPerformance";
25: const REL_RESOURCE_LABEL = "ClipitVideo-ClipitLabel";
26: const REL_RESOURCE_USER = "ClipitVideo-ClipitUser";
27: const REL_EXAMPLE_RESOURCE = ClipitExample::REL_EXAMPLE_VIDEO;
28: const REL_GROUP_RESOURCE = ClipitGroup::REL_GROUP_VIDEO;
29: const REL_TASK_RESOURCE = ClipitTask::REL_TASK_VIDEO;
30: const REL_ACTIVITY_RESOURCE = ClipitActivity::REL_ACTIVITY_VIDEO;
31: const REL_TRICKYTOPIC_RESOURCE = ClipitTrickyTopic::REL_TRICKYTOPIC_VIDEO;
32: const REL_SITE_RESOURCE = ClipitSite::REL_SITE_VIDEO;
33: public $preview = "";
34: public $duration = 0;
35: public $overlay_metadata = "";
36:
37: 38: 39: 40: 41: 42: 43: 44:
45: static function upload_to_youtube($local_video_path, $title)
46: {
47: if (!get_config("google_refresh_token")) {
48: return false;
49: }
50: set_include_path(
51: get_include_path() . PATH_SEPARATOR . elgg_get_plugins_path() . "z02_clipit_api/libraries/google_api/src/"
52: );
53: $lib_path = elgg_get_plugins_path() . "z02_clipit_api/libraries/";
54: require_once($lib_path . "google_api/src/Google/Client.php");
55: require_once($lib_path . "google_api/src/Google/Service/YouTube.php");
56:
57: $client = new Google_Client();
58: $client->setClientId(get_config("google_id"));
59: $client->setClientSecret(get_config("google_secret"));
60: try {
61: $client->setAccessToken(get_config("google_token"));
62: } catch (Exception $e) {
63: error_log($e);
64: }
65: if ($client->isAccessTokenExpired()) {
66: $refresh_token = get_config("google_refresh_token");
67: $client->refreshToken($refresh_token);
68: }
69: if (!$client->getAccessToken()) {
70: return null;
71: }
72:
73: $youtube = new Google_Service_YouTube($client);
74:
75:
76:
77:
78: $snippet = new Google_Service_YouTube_VideoSnippet();
79: $snippet->setTitle($title);
80:
81:
82: $snippet->setCategoryId("28");
83:
84:
85: $status = new Google_Service_YouTube_VideoStatus();
86: $status->privacyStatus = "unlisted";
87:
88: $video = new Google_Service_YouTube_Video();
89: $video->setSnippet($snippet);
90: $video->setStatus($status);
91:
92:
93:
94: $chunkSizeBytes = 1 * 1024 * 1024;
95:
96:
97: $client->setDefer(true);
98:
99: $insertRequest = $youtube->videos->insert("status,snippet", $video);
100:
101: $media = new Google_Http_MediaFileUpload($client, $insertRequest, 'video/*', null, true, $chunkSizeBytes);
102: $media->setFileSize(filesize($local_video_path));
103:
104: $status = false;
105: $handle = fopen($local_video_path, "rb");
106: while (!$status && !feof($handle)) {
107: $chunk = fread($handle, $chunkSizeBytes);
108: $status = $media->nextChunk($chunk);
109: }
110: fclose($handle);
111:
112: $client->setDefer(false);
113: $_SESSION['token'] = $client->getAccessToken();
114: set_config("google_token", $_SESSION['token']);
115: return (string)"http://www.youtube.com/watch?v=" . $status['id'];
116: }
117:
118: 119: 120: 121: 122:
123: protected function copy_from_elgg($elgg_entity)
124: {
125: parent::copy_from_elgg($elgg_entity);
126: $this->preview = (string)$elgg_entity->get("preview");
127: $this->duration = (int)$elgg_entity->get("duration");
128: $this->overlay_metadata = (string)$elgg_entity->get("overlay_metadata");
129: }
130:
131: 132: 133: 134: 135:
136: protected function copy_to_elgg($elgg_entity)
137: {
138: parent::copy_to_elgg($elgg_entity);
139: $video_metadata = static::video_url_parser($this->url);
140: $elgg_entity->set("url", (string)$video_metadata["url"]);
141: if (empty($this->preview)) {
142: $this->preview = (string)$video_metadata["preview"];
143: }
144: $elgg_entity->set("preview", (string)$this->preview);
145: $elgg_entity->set("duration", (int)$this->duration);
146: $elgg_entity->set("overlay_metadata", (string)$this->overlay_metadata);
147: }
148:
149: 150: 151: 152:
153: static function video_url_parser($url)
154: {
155: if ($parse_url = parse_url($url)) {
156: if (!isset($parts["scheme"])) {
157: $url = "http://$url";
158: }
159: }
160: if (!isset($url) || !filter_var($url, FILTER_VALIDATE_URL)) {
161: return false;
162: }
163: $video_patterns = array('#(((http://)?)|(^./))(((www.)?)|(^./))youtube\.com/watch[?]v=([^\[\]()<.,\s\n\t\r]+)#i'
164: , '#(((http://)?)|(^./))(((www.)?)|(^./))youtu\.be/([^\[\]()<.,\s\n\t\r]+)#i'
165: , '/(http:\/\/)(www\.)?(vimeo\.com\/groups)(.*)(\/videos\/)([0-9]*)/'
166: , '/(http:\/\/)(www\.)?(vimeo.com\/)([0-9]*)/'
167: , '/(https:\/\/)(www\.)?(vimeo.com\/)([0-9]*)/');
168: $favicon_url_base = "http://www.google.com/s2/favicons?domain=";
169:
170: $output = array();
171: foreach ($video_patterns as $video_pattern) {
172: if (preg_match($video_pattern, $url) > 0) {
173:
174: if (strpos($url, 'youtube.com') != false || strpos($url, 'youtu.be') != false) {
175: preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $matches);
176: $output = array(
177: 'id' => $matches[0],
178: 'url' => 'http://www.youtube.com/watch?v=' . $matches[0],
179: 'preview' => "http://i1.ytimg.com/vi/{$matches[0]}/mqdefault.jpg",
180: 'favicon' => $favicon_url_base . $parse_url['host']
181: );
182:
183: } else if (strpos($url, 'vimeo.com') != false) {
184: preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=vimeo.com/)[^&\n]+#", $url, $matches);
185: $data = file_get_contents("http://vimeo.com/api/v2/video/$matches[0].json");
186: $data = array_pop(json_decode($data));
187: $output = array(
188: 'id' => $matches[0],
189: 'url' => "http://vimeo.com/{$matches[0]}",
190: 'preview' => $data->thumbnail_large,
191: 'favicon' => $favicon_url_base . $parse_url['host']
192: );
193: }
194: }
195: }
196: if (!$output['id']) {
197: return false;
198: }
199:
200: return $output;
201: }
202: }