Ignore:
Timestamp:
Nov 28, 2012, 12:49:06 PM (11 years ago)
Author:
mistic100
Message:
  • add safe mode
  • fix little error when adding video without thumbnail
File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/gvideo/include/functions.inc.php

    r19056 r19213  
    22if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
    33
    4 function parse_video_url($source_url)
     4function parse_video_url($source_url, $safe_mode=false)
    55{
    66  $source_url = 'http://'.preg_replace('#^http(s?)://#', null, $source_url);
     
    1010  $url['host'] = explode('.', $url['host']);
    1111 
    12   $video = array();
     12  $video = array(
     13    'type' => null,
     14    'video_id' => null,
     15    'url' => null,
     16    'title' => null,
     17    'description' => null,
     18    'thumbnail' => null,
     19    'author' => null,
     20    'tags' => null,
     21  );
     22 
    1323  switch ($url['host'][0])
    1424  {
     
    2636    case 'youtu': // youtu.be (short-url service)
    2737    {
    28       if (empty($video))
     38      if (empty($video['video_id']))
    2939      {
    3040        $video['type'] = 'youtube';
     
    3444      }
    3545     
    36       $api_url = 'http://gdata.youtube.com/feeds/api/videos/'.$video['video_id'].'?v=2&alt=json';
    37       $json = download_remote_file($api_url, true);
    38       if ($json === false or $json == 'file_error') return false;
    39      
    40       $json = json_decode($json, true);
    41       $video = array_merge($video, array(
    42         'url' => 'http://youtube.com/watch?v='.$video['video_id'],
    43         'title' => $json['entry']['title']['$t'],
    44         'description' => $json['entry']['media$group']['media$description']['$t'],
    45         'thumbnail' => $json['entry']['media$group']['media$thumbnail'][2]['url'],
    46         'author' => $json['entry']['author'][0]['name']['$t'],
    47         'tags' => null,
    48         ));
     46      $video['url'] = 'http://youtube.com/watch?v='.$video['video_id'];
     47     
     48      if (!$safe_mode)
     49      {
     50        $api_url = 'http://gdata.youtube.com/feeds/api/videos/'.$video['video_id'].'?v=2&alt=json';
     51        $json = download_remote_file($api_url, true);
     52        if ($json === false or $json == 'file_error') return false;
     53       
     54        $json = json_decode($json, true);
     55        $video = array_merge($video, array(
     56          'title' => $json['entry']['title']['$t'],
     57          'description' => $json['entry']['media$group']['media$description']['$t'],
     58          'thumbnail' => $json['entry']['media$group']['media$thumbnail'][2]['url'],
     59          'author' => $json['entry']['author'][0]['name']['$t'],
     60          ));
     61      }
     62      else
     63      {
     64        $video['title'] = 'YouTube #'.$video['video_id'];
     65      }
     66     
    4967      break;
    5068    }
     
    5876      $video['video_id'] = $url['path'][1];
    5977     
    60       $api_url = 'http://vimeo.com/api/v2/video/'.$video['video_id'].'.json';
    61       $json = download_remote_file($api_url, true);
    62       if ($json === false or $json == 'file_error') return false;
    63      
    64       $json = json_decode($json, true);
    65       $video = array_merge($video, array(
    66         'url' => 'http://vimeo.com/'.$video['video_id'],
    67         'title' => $json[0]['title'],
    68         'description' => $json[0]['description'],
    69         'thumbnail' => $json[0]['thumbnail_large'],
    70         'author' => $json[0]['user_name'],
    71         'tags' => array_map('trim', explode(',', $json[0]['tags'])),
    72         ));
     78      $video['url'] = 'http://vimeo.com/'.$video['video_id'];
     79     
     80      if (!$safe_mode)
     81      {
     82        $api_url = 'http://vimeo.com/api/v2/video/'.$video['video_id'].'.json';
     83        $json = download_remote_file($api_url, true);
     84        if ($json === false or $json == 'file_error') return false;
     85       
     86        $json = json_decode($json, true);
     87        $video = array_merge($video, array(
     88          'title' => $json[0]['title'],
     89          'description' => $json[0]['description'],
     90          'thumbnail' => $json[0]['thumbnail_large'],
     91          'author' => $json[0]['user_name'],
     92          'tags' => array_map('trim', explode(',', $json[0]['tags'])),
     93          ));
     94      }
     95      else
     96      {
     97        $video['title'] = 'Vimeo #'.$video['video_id'];
     98      }
     99     
    73100      break;
    74101    }
     
    83110      $video['video_id'] = $url['path'][2];
    84111     
    85       $api_url = 'https://api.dailymotion.com/video/'.$video['video_id'].'?fields=description,thumbnail_large_url,title,owner.username,tags'; // DM doesn't accept non secure connection
    86       $json = download_remote_file($api_url, true);
    87       if ($json === false or $json == 'file_error') return false;
    88      
    89       $json = json_decode($json, true);
    90       $json['thumbnail_large_url'] = preg_replace('#\?([0-9]+)$#', null, $json['thumbnail_large_url']);
    91      
    92       $video = array_merge($video, array(
    93         'url' => 'http://dailymotion.com/video/'.$video['video_id'],
    94         'title' => $json['title'],
    95         'description' => $json['description'],
    96         'thumbnail' => $json['thumbnail_large_url'],
    97         'author' => $json['owner.username'],
    98         'tags' => $json['tags'],
    99         ));
     112      $video['url'] = 'http://dailymotion.com/video/'.$video['video_id'];
     113     
     114      if (!$safe_mode)
     115      {
     116        $api_url = 'https://api.dailymotion.com/video/'.$video['video_id'].'?fields=description,thumbnail_large_url,title,owner.username,tags'; // DM doesn't accept non secure connection
     117        $json = download_remote_file($api_url, true);
     118        if ($json === false or $json == 'file_error') return false;
     119       
     120        $json = json_decode($json, true);
     121        $json['thumbnail_large_url'] = preg_replace('#\?([0-9]+)$#', null, $json['thumbnail_large_url']);
     122       
     123        $video = array_merge($video, array(
     124          'title' => $json['title'],
     125          'description' => $json['description'],
     126          'thumbnail' => $json['thumbnail_large_url'],
     127          'author' => $json['owner.username'],
     128          'tags' => $json['tags'],
     129          ));
     130      }
     131      else
     132      {
     133        $video['title'] = $video['video_id'];
     134      }
     135     
    100136      break;
    101137    }
     
    106142      $video['type'] = 'wat';
    107143     
     144      // no safe_mode for wat.tv
    108145      $html = download_remote_file($source_url, true);
    109146      if ($html === false or $html == 'file_error') return false;
     
    139176      $video['video_id'] = rtrim($url['path'][2], '.html');
    140177     
    141       $html = download_remote_file($source_url, true);
    142       if ($html === false or $html == 'file_error') return false;
    143      
    144178      $video['url'] = 'http://wideo.fr/video/'.$video['video_id'].'.html';
    145179     
    146       preg_match('#<meta property="og:title" content="([^">]*)" />#', $html, $matches);
    147       $video['title'] = $matches[1];
    148      
    149       preg_match('#<meta property="og:description" content="([^">]*)" />#', $html, $matches);
    150       $video['description'] = $matches[1];
    151      
    152       preg_match('#<meta property="og:image" content="([^">]+)" />#', $html, $matches);
    153       $video['thumbnail'] = $matches[1];
    154      
    155       preg_match('#<li id="li_author">Auteur :  <a href=(?:[^>]*)><span>(.*?)</span></a>#', $html, $matches);
    156       $video['author'] = $matches[1];
    157      
    158       preg_match('#<meta name="keywords" content="([^">]+)" />#', $html, $matches);
    159       $video['tags'] = array_map('trim', explode(',', $matches[1]));
     180      if (!$safe_mode)
     181      {
     182        $html = download_remote_file($source_url, true);
     183        if ($html === false or $html == 'file_error') return false;
     184       
     185        preg_match('#<meta property="og:title" content="([^">]*)" />#', $html, $matches);
     186        $video['title'] = $matches[1];
     187       
     188        preg_match('#<meta property="og:description" content="([^">]*)" />#', $html, $matches);
     189        $video['description'] = $matches[1];
     190       
     191        preg_match('#<meta property="og:image" content="([^">]+)" />#', $html, $matches);
     192        $video['thumbnail'] = $matches[1];
     193       
     194        preg_match('#<li id="li_author">Auteur :  <a href=(?:[^>]*)><span>(.*?)</span></a>#', $html, $matches);
     195        $video['author'] = $matches[1];
     196       
     197        preg_match('#<meta name="keywords" content="([^">]+)" />#', $html, $matches);
     198        $video['tags'] = array_map('trim', explode(',', $matches[1]));
     199      }
     200      else
     201      {
     202        $video['title'] = $video['video_id'];
     203      }
     204     
    160205      break;
    161206    }
Note: See TracChangeset for help on using the changeset viewer.