Skip to content

Instantly share code, notes, and snippets.

@vpadhariya
Created September 17, 2019 17:25
Show Gist options
  • Save vpadhariya/63c67db880f565f08c7c9859da71d3f0 to your computer and use it in GitHub Desktop.
Save vpadhariya/63c67db880f565f08c7c9859da71d3f0 to your computer and use it in GitHub Desktop.
Youtube Data search and parsing
<?php
//require_once('modules/admin/models/ServiceProvider.php');
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class YoutubeCommand extends CConsoleCommand
{
var $startTime;
var $thisFile;
var $arr_priority = array (1 => 10, 2 => 6, 3 => 4);
public function init()
{
$this->startTime = time();
$this->thisFile = basename(__FILE__);
echo "\n" . 'SCRIPT `' . $this->thisFile . '` START AT: ' . date('l d. F Y h:i:s A ') . "\n\n";
}
function __destruct()
{
$endTime = time() - $this->startTime;
echo "\n\n" . 'TIME TAKEN TO EXECUTE SCTIPT : ', $endTime . '(' . round($endTime / 60, 3) . ') Seconds(Minutes)';
echo "\n" . date('h:i:s'), ' : FILE `' . $this->thisFile . '` EXECUTED SUCESSFULLY', "\n\n";
}
public function run($args)
{
foreach($this->arr_priority as $priority => $limit)
{
$mo_youtube = BrandWords::model()->findAll(array(
'condition' => 'youtube_status = :youtube_status AND name != :name AND priority = :priority',
'params' => array(':youtube_status' => '0', ':name' => '', ':priority' => $priority),
'limit' => $limit
));
echo '#--------------------------------------------' . "\n";
echo $priority . ' ' . $limit . "\n";
echo '#--------------------------------------------' . "\n";
foreach ($mo_youtube as $value)
{
$id = $value->getAttribute('id');
$keyword = $value->getAttribute('name');
$is_special = $value->getAttribute('is_special');
$brand_id = $value->getAttribute('brand_id');
$category_id = CommonHelper::getBrandCats($brand_id);
$comments_found = $youtube_count = 0;
//youtube Comment Add Codeing
$youtubeComment = self::getComments($keyword);
if (count($youtubeComment) > 0)
{
$comments_found = count($youtubeComment);
// Save comment into DB
$youtube_count = YoutubeHelpers::youtubeCommentSave($youtubeComment, $category_id, $brand_id, $keyword, $is_special);
}
echo "\n";
if($is_special)
echo 'Special Keyword : ', $keyword, "\n";
else
echo 'Keyword : ', $keyword, "\n";
if(count($youtubeComment) > 0)
{
echo 'Saved comments : ', $youtube_count['total_saved'], "\n";
if($is_special)
echo 'Saved pending : ', $youtube_count['pending'], "\n";
echo 'Comments Found : ', $comments_found, "\n";
}
else
echo 'No comments were found for this Keyword!' . "\n";
$keyword = BrandWords::model()->findByPk($id);
$keyword->youtube_status = '1';
$keyword->save();
}
// Reset counters if all status flag are 1
CommonHelper::resetCounters('youtube', $priority);
}
echo "\n" . 'Comment fetching ended' . "\n";
}
function getComments($txtq)
{
set_time_limit(0);
$txtq = str_replace(" ", "%20", $txtq);
//$url='http://gdata.youtube.com/feeds/api/videos?alt=json&v=2&q='.$txtq.'&safeSearch=none&time=all_time&uploader=partner&location='.$txtlocation['lat'].','.$txtlocation['lng'].'&location-radius='.$txtarea.'km';
$url='http://gdata.youtube.com/feeds/api/videos?alt=json&v=2&q='.$txtq.'&safeSearch=none&time=all_time&uploader=partner';
$result = CommonHelper::simpleCurl($url);
$test = json_decode($result);
$entry = isset($test->feed->entry)?$test->feed->entry:'';
$i=0;
$user_detail = array();
if(!empty($entry))
{
foreach ($entry as $e)
{
if(!empty($e->{'gd$comments'}->{'gd$feedLink'}->{'countHint'}) && $e->{'gd$comments'}->{'gd$feedLink'}->{'countHint'}!=0)
{
$url = $e->{'gd$comments'}->{'gd$feedLink'}->{'href'}."&alt=json";
$result = CommonHelper::simpleCurl($url);
$test = json_decode($result);
$entry_feed = isset($test->feed->entry)?$test->feed->entry:'';
if(!empty($entry_feed))
{
foreach ($entry_feed as $ef)
{
if(!empty($ef->content->{'$t'}) && CommonHelper::match_phrase($txtq, $ef->content->{'$t'}))
{
$name = isset($ef->author[0]->name->{'$t'})?$ef->author[0]->name->{'$t'} : '';
$userId = isset($ef->author[0]->{'yt$userId'}->{'$t'})?$ef->author[0]->{'yt$userId'}->{'$t'} : '';
$videoid = isset($ef->{'yt$videoid'}->{'$t'})?$ef->{'yt$videoid'}->{'$t'} : '';
$content = isset($ef->content->{'$t'})?$ef->content->{'$t'} : '';
$published = isset($ef->published->{'$t'})?$ef->published->{'$t'} : '';
$updated = isset($ef->updated->{'$t'})?$ef->updated->{'$t'} : '';
$user_detail[$i] = array();
$user_detail[$i]['name'] = $name;
$user_detail[$i]['userId'] = $userId;
$user_detail[$i]['videoid'] = $videoid;
$user_detail[$i]['content'] = $content;
$user_detail[$i]['published'] = $published;
$user_detail[$i]['updated'] = $updated;
$i++;
}
}
}
}
}
}
return $user_detail;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment