Archived
1
0
This repository has been archived on 2020-12-10. You can view files and clone it, but cannot push or open issues or pull requests.
old/application/models/YoutubePlayerModel.php
2018-10-16 18:28:42 +02:00

135 lines
5.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use Coduo\PHPHumanizer\DateTimeHumanizer;
class YoutubePlayerModel extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function getRecommendVideos($id)
{
$data = $this->youtubePlayerModel->newestVids('15');
$return = '';
if (!empty($data)) {
foreach ($data as $row) {
$videoId = $row['contentDetails'];
$videoId = $videoId['videoId'];
$title = $this->youtubePlayerModel->getInfos($videoId)['title'];
if ($id == $videoId) {
$return .= '<li>
<a onclick="switchVideo(\'' . $videoId . '\');" style="cursor: pointer;">
<div class="image-container active">
<img src="http://img.youtube.com/vi/' . $videoId . '/maxresdefault.jpg" alt="' . $title . '" class="img-fluid">
<div class="overlay"><span>' . $title . '</span></div>
</div>
</a>
</li>';
} else {
$return .= '<li>
<a onclick="switchVideo(\'' . $videoId . '\');" style="cursor: pointer;">
<div class="image-container">
<img src="http://img.youtube.com/vi/' . $videoId . '/maxresdefault.jpg" alt="' . $title . '" class="img-fluid">
<div class="overlay"><span>' . $title . '</span></div>
</div>
</a>
</li>';
}
// if($id == $videoId) {
// $return .= '<a onClick="switchVideo(\'' . $videoId . '\');" class="btn btn-sm btn-default active raised">' .$title. '</a>';
// } else {
// $return .= '<a onClick="switchVideo(\'' . $videoId . '\');" class="btn btn-sm btn-default raised">' .$title. '</a>';
// }
}
} else {
$return = null;
}
return $return;
}
public function newestVids($vidCount)
{
$data = $this->db->query('SELECT * FROM social_posts WHERE post_plattform = \'YouTube\' ORDER BY post_date DESC LIMIT ?', [$vidCount])->result_array();
if (!empty($data)) {
return $data;
} else {
return null;
}
}
public function getInfos($id)
{
$data = json_decode(@file_get_contents('https://www.googleapis.com/youtube/v3/videos?part=snippet%2Cstatistics&hl=de&id=' . $id . '&regionCode=de&key=AIzaSyAZi6W9FODqd-bjlmV0sGJ8vjvSgFNTXVM'), true)['items'];
$data = $data[0];
$infos = $data['snippet'];
$stats = $data['statistics'];
$return = [];
$return['title'] = $infos['title'];
$description = htmlspecialchars($infos['description']);
$descriptionList = explode(' ', $description);
$description = '';
foreach ($descriptionList as $item) {
if (isset(parse_url($item)['scheme']) && isset(parse_url($item)['host'])) {
$description .= '<a href="' . $item . '" target="_blank">' . $item . '</a> ';
} else {
if ($infos['channelTitle'] == 'KingOfDog') {
$items1 = ['✔️', '', '©'];
$items2 = ['</p><p>✔️', '<br>', '<br>©'];
$description .= str_replace($items1, $items2, $item) . ' ';
} else {
$description .= $item . ' ';
}
}
}
$return['description'] = $description;
$published = strtotime($infos['publishedAt']);
$return['published'] = DateTimeHumanizer::difference(new \DateTime(), new \DateTime("@$published"), $_SESSION['site_lang']);
$return['channelName'] = $infos['channelTitle'];
$return['channelUrl'] = 'http://youtube.com/channel/' . $infos['channelId'];
$return['views'] = number_format($stats['viewCount'], 0, ',', '.');
$return['likes'] = $stats['likeCount'];
$return['dislikes'] = $stats['dislikeCount'];
$return['comments'] = number_format($stats['commentCount'], 0, ',', '.');
$rateCount = intval($return['likes']) + intval($return['dislikes']);
if (intval($return['likes']) > 0 && intval($return['dislikes']) > 0) {
$return['likeWidth'] = intval($return['likes']) / $rateCount * 100;
$return['dislikeWidth'] = intval($return['dislikes']) / $rateCount * 100;
$return['grayWidth'] = 0;
} elseif (intval($return['likes']) == 0 && intval($return['dislikes']) == 0) {
$return['likeWidth'] = 0;
$return['dislikeWidth'] = 0;
$return['grayWidth'] = 100;
} elseif (intval($return['likes']) == 0) {
$return['likeWidth'] = 0;
$return['dislikeWidth'] = 100;
$return['grayWidth'] = 0;
} else {
$return['likeWidth'] = 100;
$return['dislikeWidth'] = 0;
$return['grayWidth'] = 0;
}
$return['thumbnails'] = $infos['thumbnails'];
return $return;
}
public function checkVideo($videoID)
{
$data = json_decode(@file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=" . $videoID . "&key=AIzaSyAZi6W9FODqd-bjlmV0sGJ8vjvSgFNTXVM"));
return $data->pageInfo->totalResults !== 0;
}
}