aboutsummaryrefslogtreecommitdiff
path: root/types/video_google/video_google.module
blob: 280275e34dfb9974a9ccbd30f96bc88889586367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
//$Id$
/**
 * @file
 * Enable Google Video support for video module.
 *
 * @author Fabio Varesano <fvaresano at yahoo dot it>
 * @author Heshan Wanigasooriya <heshan at heidisoft.com><heshanmw at gmail dot com>
 * @todo
 */


/**
 * Implementation of hook_menu
*/
function video_google_menu() {
    $items = array();
    $items['node/add/video/google'] = array(
      'title' => 'Google',
      'access arguments' => array('create video')
    );
    return $items;
}


/**
 * Implementation of hook_v_help
*/
function video_google_v_help() {

  $help = array();
  $help['google']['data'] = '<b><a href="http://video.google.com" name="video_google">' . t('Google Video support') . '</a></b>';
  $help['google']['children'] = array(t('You can host videos on video.google.com and put them on your site.
  To do this, after you upload the video on Google video you just have to get the URL of the video.'));

  return $help;
}


/**
 * Implementation of hook_v_info()
*/
function video_google_v_info() {
  $info['google'] = array(
    '#name' => 'Google Video',
    '#description' => t('Post a video available on !link to this website.', array('!link' => l(t('Google Video'), 'http://video.google.com'), NULL, NULL, NULL, TRUE)),
    '#autothumbable' => true,
    '#autoresolution' => true,
    '#autoplaytime' => false, // seems that thereisn't a video lenght field in the google video xml
  );

  return $info;
}


/**
 * Implementation of hook_v_form()
*/
function video_google_v_form(&$node, &$form) {

  $form['video']['vidfile'] = array(
    '#type' => 'textfield',
    '#title' => t('Google Video URL'),
    '#default_value' => $node->vidfile,
    '#maxlength' => 700,
    '#required' => TRUE,
    '#weight' => -20,
    '#description' => t('Insert the URL to the google video. ') . l(t('More information.'), 'video/help', array('fragment' => 'videofile')));

  return $form;
}



/**
 * implementation of hook_v_validate
*/
function video_google_v_validate($node) {
  // TODO: use youtube REST or XML-RPC to query youtube: check video available and embeddable
  if(!preg_match("/^http:\/\/video\.google\.com\/videoplay\?docid=/", $node->vidfile)) {
    form_set_error('vidfile', t('The Google Video URL field must be similar to <em>http://video.google.com/videoplay?docid=1806507480014945777</em>'));
  }
  else {
    //get the video id
    $id = _video_google_get_id($node->vidfile);
    
    $response = _video_apiclient_google_request($id);
    if(count($response) == 0) { // google video wasn't able to find the video
      form_set_error('vidfile', t('The system was not able to find this video on Google Video. Please check the URL of your Google video.'));
    }
  }
}


/**
 * Implementation of hook_v_play
*/
function video_google_v_play($node) {
  return theme('video_google_play', $node);
}



/** AUTOTHUMBNAILING LOGIC */

define('VIDEO_GOOGLE_XML', 'http://video.google.com/videofeed');

function _video_apiclient_google_request($id, $cacheable = TRUE) {
  $args = array('docid' => $id);
  return _video_apiclient_request_xml('google', VIDEO_GOOGLE_XML, $args, $cacheable);
}

function _video_apiclient_google_get_thumbnail_url($id) {
  
  $xml = _video_apiclient_google_request($id);
  
  // we *should* be able to use media:thumbnail
  // but unfortunately, that is stripped out from the request hook
  // so instead, we'll parse it from the description, where it's repeated.
  // TODO: look into how to fix this...
  $desc = $xml['ITEM']['DESCRIPTION'][0];
  $regex = '@<img src="([^"]*)"@';
  if (preg_match($regex, $desc, $matches)) {
    return $matches[1];
  }
  return null;
}


/**
 * Implementation of hook_v_auto_thumbnail
*/
function video_google_v_auto_thumbnail($node) {
  if (count($_POST)) {
    if ($_POST['vidfile'] == $node->vidfile) {
      _video_image_thumbnail_debug(t('No new video to thumbnail'));
      return NULL;
    }
    if ($_POST['tempimage']['fids']['_original']) {
      _video_image_thumbnail_debug(t('Video already thumbnailed'));
      return NULL;
    }
  }
  // let's include apiclient logic
  
  //get the video id
  if (!$node->vidfile && count($_POST)) {
    $vidfile = $_POST['vidfile'];
  } else {
    $vidfile = $node->vidfile;
  }
  $id = _video_google_get_id($vidfile);
  // get thumbnail url
  $thumbnail_url = _video_apiclient_google_get_thumbnail_url($id);

  return _video_image_get_thumb_file_object($thumbnail_url, $id);
}


/**
 * Implementation of hook_v_auto_resolution
*/
function video_google_v_auto_resolution(&$node) {
  // we set google videos to 400x326 by default
  return array(400, 326);
}



/** THEMEABLE FUNCTIONS */

/**
 * Play videos hosted on video.google.com
 * Allows users to host videos on video.google.com and then use the video ID to post it in the module.
 *
 * @param $node
 *   object with node information
 *
 * @return
 *   string of content to display
 */
function theme_video_google_play($node) {
  $width = ($node->video_scaled_x ? $node->video_scaled_x : '425');
  $height = ($node->video_scaled_y ? $node->video_scaled_y : '350');
  // Strip heading "google:"
  $videoid = _video_google_get_id(check_plain($node->vidfile));
  //$videoid = substr($node->vidfile, 7);

  // this will be executed by not Internet Explorer browsers
  $output = '<!--[if !IE]> <-->
<object type="application/x-shockwave-flash" width="'. $width .'" height="'. $height .'"
data="http://video.google.com/googleplayer.swf?docId='. check_plain($videoid) .'">
<!--> <![endif]-->' . "\n";

  // this will be executed by Internet Explorer
  $output .= '<!--[if IE]>
<object type="application/x-shockwave-flash" width="'. $width .'" height="'. $height .'"
classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
<![endif]-->' . "\n";

  // params will be passed to both IE or not IE browsers
  $output .= '<param name="movie" value="http://video.google.com/googleplayer.swf?docId=' . check_plain($videoid) . '" />' . "\n";
  // following a list of params simply copied from old embed tag params. I don't know if this are really needed.
  $output .= '<param name="quality" value="best" />
  <param name="bgcolor" value="#ffffff" />
  <param name="allowScriptAccess" value="sameDomain" />
  <param name="scale" value="noScale" />
  <param name="wmode" value="transparent" />
  <param name="salign" value="TL" />
  <param name="FlashVars" value="playerMode=embedded" />'
  . _video_get_parameters($node) .
  '<p>'. t('Your browser is not able to display this multimedia content.') .'</p>
</object>';


  $output = theme('video_format_play', $output, t('http://video.google.com/support'), t('Link to video.google.com'), t('video.google.com'));
  return $output;
}



/** HELPER FUNCTIONS */

/**
 * Get the docid from an URL
*/
function _video_google_get_id($url) {
  $pattern = '/-?[0-9]+/'; // maybe too weak? some id have a leading -
  preg_match_all($pattern, $url, $matches, PREG_PATTERN_ORDER);
  return $matches[0][0];
}

/**
 * Implementation of hook_theme().
 */
function video_google_theme() {
  return array(
    'video_google_play' => array(
      'arguments' => array('node' => NULL),
    ),
  );
}

module_load_include('inc', 'video', 'includes/apiclient');