1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-05 02:39:46 +02:00
Oinktube/install/importSiteFromAPI.php
2022-06-11 16:01:05 -03:00

320 lines
12 KiB
PHP

<?php
//streamer config
require_once '../videos/configuration.php';
if (!isCommandLineInterface()) {
return die('Command Line only');
}
ob_end_flush();
function download($url, $filename, $path, $forceDownload=false){
$parts = explode("/{$filename}/", $url);
if(empty($parts[1])){
if(preg_match("/\.mp3$/", $url)){
$parts[1] = "{$filename}.mp3";
}
}
if(empty($parts[1])){
_error_log("importVideo::download ERROR on download {$url}");
return false;
}
$parts2 = explode('?', $parts[1]);
$file = $parts2[0];
$destination = $path.$file;
if($forceDownload || !file_exists($destination)){
_error_log("importVideo::download [$destination]");
return wget($url, $destination, true);
}else{
_error_log("importVideo::download skipped [$destination]");
}
return false;
}
set_time_limit(360000);
ini_set('max_execution_time', 360000);
$global['rowCount'] = $global['limitForUnlimitedVideos'] = 999999;
$siteURL = trim(@$argv[1]);
$APISecret = trim(@$argv[2]);
$type = trim(@$argv[3]);
if(empty($siteURL) || !isValidURL($siteURL)){
echo 'Enter a valid URL';
exit;
}
if(empty($APISecret)){
echo 'Enter a valid APISecret';
exit;
}
$rowCount = 50;
$current = 1;
$hasNewContent = true;
_error_log("importSite: start {$siteURL}");
if($type !== 'm3u8'){
// get categories
while($hasNewContent){
$APIURL = "{$siteURL}plugin/API/get.json.php?APIName=category&rowCount={$rowCount}&current={$current}&APISecret={$APISecret}";
$content = url_get_contents($APIURL, "", 30);
$hasNewContent = false;
$current++;
if(!empty($content)){
_error_log("importCategory: SUCCESS {$APIURL}");
$json = _json_decode($content);
if(!empty($json) && !empty($json->response) && !empty($json->response->totalRows) && !empty($json->response->rows)){
_error_log("importCategory: JSON SUCCESS totalRows={$json->response->totalRows}");
$hasNewContent = true;
foreach ($json->response->rows as $key => $value) {
$cat = Category::getCategoryByName($value->clean_name);
if(!empty($cat)){
_error_log("importCategory: category exists [{$cat['id']}]{$cat['clean_name']}");
continue;
}
$o = new Category(0);
$o->setName($value->name);
$o->setClean_name($value->clean_name);
$o->setDescription($value->description);
$o->setIconClass($value->iconClass);
$o->setPrivate($value->private);
$o->setAllow_download($value->allow_download);
$o->setOrder($value->order);
$o->setSuggested($value->suggested);
$o->setNextVideoOrder($value->nextVideoOrder);
$o->setUsers_id(1);
_error_log("importCategory: Saving ...");
$id = $o->save(true);
if($id){
_error_log("importCategory: saved {$id}");
}else{
_error_log("importCategory: ERROR NOT saved");
$video->setStatus(Video::$statusBrokenMissingFiles);
}
//exit;
}
}else{
_error_log("importCategory: JSON ERROR {$content} ");
}
}else{
_error_log("importCategory: ERROR {$APIURL} content is empty");
}
}
$current = 1;
$hasNewContent = true;
// get users
while($hasNewContent){
$APIURL = "{$siteURL}plugin/API/get.json.php?APIName=users_list&rowCount={$rowCount}&current={$current}&APISecret={$APISecret}";
$content = url_get_contents($APIURL, "", 30);
$hasNewContent = false;
$current++;
if(!empty($content)){
_error_log("importUsers: SUCCESS {$APIURL}");
$json = _json_decode($content);
if(!empty($json) && !empty($json->response)){
_error_log("importUsers: JSON SUCCESS");
$hasNewContent = true;
foreach ($json->response as $key => $value) {
$user = User::getUserFromEmail($value->email);
if(empty($user)){
$user = User::getUserFromChannelName($value->channelName);
}
if(!empty($user)){
_error_log("importUsers: exists [{$user['id']}]{$user['clean_name']}");
continue;
}
$o = new User(0);
$o->setUser($value->user);
$o->setPassword($value->user);
$o->setName($value->name);
$o->setEmail($value->email);
$o->setIsAdmin(0);
$o->setStatus($value->a);
$o->setCanStream($value->canStream);
$o->setCanUpload($value->canUpload);
$o->setCanCreateMeet($value->canCreateMeet);
$o->setCanViewChart($value->canViewChart);
$o->setChannelName($value->channelName);
$o->setEmailVerified($value->emailVerified);
$o->setAbout($value->about);
$o->setAnalyticsCode($value->analyticsCode);
$o->setExternalOptions($value->externalOptions);
$o->setFirst_name($value->first_name);
$o->setLast_name($value->last_name);
$o->setAddress($value->address);
$o->setZip_code($value->zip_code);
$o->setCountry($value->country);
$o->setRegion($value->region);
$o->setCity($value->city);
$o->setDonationLink($value->donationLink);
$o->setExtra_info($value->extra_info);
$o->setPhone($value->phone);
$o->setIs_company($value->is_company);
_error_log("importUsers: Saving ...");
$id = $o->save(false);
if($id){
_error_log("importUsers: saved {$id}");
wget($value->photo, "{$global['systemRootPath']}videos/userPhoto/photo{$id}.png", true);
//wget($value->background, "{$global['systemRootPath']}videos/userPhoto/photo{$id}.png", true);
}else{
_error_log("importUsers: ERROR NOT saved");
$video->setStatus(Video::$statusBrokenMissingFiles);
}
//exit;
}
}else{
_error_log("importUsers: JSON ERROR ". json_last_error_msg());
//exit;
_error_log("importUsers: JSON ERROR {$content} ");
}
}else{
_error_log("importUsers: ERROR {$APIURL} content is empty");
}
}
}
$current = 1;
$hasNewContent = true;
// import videos
while($hasNewContent){
$APIURL = "{$siteURL}plugin/API/get.json.php?APIName=video&rowCount={$rowCount}&current={$current}&APISecret={$APISecret}";
$content = url_get_contents($APIURL, "", 30);
$hasNewContent = false;
$current++;
if(!empty($content)){
_error_log("importVideos: SUCCESS {$APIURL}");
$json = _json_decode($content);
if(!empty($json) && !empty($json->response) && !empty($json->response->totalRows) && !empty($json->response->rows)){
_error_log("importVideo: JSON SUCCESS totalRows={$json->response->totalRows}");
$hasNewContent = true;
foreach ($json->response->rows as $key => $value) {
if($type=='m3u8'){
if(empty($value->videos->m3u8)){
continue;
}
}
$videos_id = 0;
$row = Video::getVideoFromFileNameLight($value->filename);
if(!empty($row)){
_error_log("importVideo: Video found");
$videos_id = $row['id'];
}else{
_error_log("importVideo: Video NOT found");
}
_error_log("importVideo: Video {$videos_id} {$value->title} {$value->fileName}");
$users_id = 1;
$user = User::getUserFromEmail($value->email);
if(empty($user)){
$user = User::getUserFromChannelName($value->channelName);
}
if(!empty($user)){
$users_id = $user['id'];
}
$cat = Category::getCategoryByName($value->clean_name);
$video = new Video($value->title, $value->filename, $videos_id);
$video->setCreated("'$value->created'");
$video->setDuration($value->duration);
$video->setType($value->type);
$video->setVideoDownloadedLink($value->videoDownloadedLink);
$video->setDuration_in_seconds($value->duration_in_seconds);
$video->setDescription($value->description);
$video->setUsers_id($users_id);
$video->setStatus(Video::$statusTranfering);
$video->setCategories_id($cat['id']);
_error_log("importVideo: Saving video");
$id = $video->save(false, true);
if($id){
_error_log("importVideo: Video saved {$id}");
$path = getVideosDir().$value->filename.DIRECTORY_SEPARATOR;
make_path($path);
// download images
download($value->images->poster, $value->filename, $path);
download($value->images->thumbsGif, $value->filename, $path);
foreach ($value->videos->mp4 as $key2=>$value2) {
_error_log("importVideo MP4: key = {$key} key2 = {$key2} APIURL = $APIURL");
download($value2, $value->filename, $path);
}
if(!empty($value->videos->mp3)){
_error_log("importVideo MP3: {$value->videos->mp3} APIURL = $APIURL");
download($value->videos->mp3, $value->filename, $path);
}
$video->setStatus(Video::$statusActive);
if(!empty($value->videos->m3u8)){
$size = getDirSize($path);
if($size < 10000000){
_error_log("importVideo m3u8: {$value->videos->m3u8->url} APIURL = $APIURL ($size) ". humanFileSize($size));
sendToEncoder($id, $value->videos->m3u8->url);
$video->setStatus(Video::$statusEncoding);
}else{
_error_log("importVideo m3u8 NOT SEND: ($size) ". humanFileSize($size));
}
}
}else{
_error_log("importVideo: ERROR Video NOT saved");
$video->setStatus(Video::$statusBrokenMissingFiles);
}
$video->save(false, true);
//exit;
}
}else{
_error_log("importVideo: JSON ERROR {$content} ");
}
}else{
_error_log("importVideo: ERROR {$APIURL} content is empty");
}
}
die();