2026-08-29 12:42:36 +03:00
const { execFile } = require ( 'child_process' );
const path = require ( 'path' );
const http = require ( 'http' );
2026-10-03 10:45:58 +03:00
const https = require ( 'https' );
2026-09-19 05:14:11 +03:00
const fs = require ( 'fs' );
2026-11-05 18:02:25 +03:00
const os = require ( 'os' );
2026-10-03 10:45:58 +03:00
const crypto = require ( 'crypto' );
2026-08-29 12:42:36 +03:00
const searchCache = new Map ();
const CACHE_TTL = 30 * 60 * 1000 ; // 30 minutes
2026-09-19 05:14:11 +03:00
let cachedYtDlpPath = null ;
2026-11-05 18:02:25 +03:00
let cachedYtDlpCustomSetting = null ;
let autoDownloadPromise = null ;
2026-10-03 10:45:58 +03:00
2026-11-05 18:02:25 +03:00
function isExecutableFile ( filePath ) {
if ( ! filePath || typeof filePath !== 'string' ) return false ;
try {
const stat = fs . statSync ( filePath );
if ( ! stat . isFile () || stat . size < 1024 ) return false ;
if ( process . platform !== 'win32' ) {
try {
fs . accessSync ( filePath , fs . constants . X_OK );
} catch ( _ ) {
try {
fs . chmodSync ( filePath , 0o755 );
} catch ( __ ) {}
2026-09-19 05:14:11 +03:00
}
2026-11-05 18:02:25 +03:00
}
return true ;
} catch ( e ) {
return false ;
}
}
function discoverDynamicCandidatePaths ( peertubeHelpers ) {
const isWin = process . platform === 'win32' ;
const binNames = isWin ? [ 'yt-dlp.exe' , 'yt-dlp' ] : [ 'yt-dlp' , 'yt-dlp_linux' , 'yt-dlp_macos' ];
const searchDirs = new Set ();
function addWithParents ( startDir , maxUp = 4 ) {
if ( ! startDir || typeof startDir !== 'string' ) return ;
let cur = path . resolve ( startDir );
for ( let i = 0 ; i <= maxUp ; i ++ ) {
searchDirs . add ( cur );
searchDirs . add ( path . join ( cur , 'bin' ));
searchDirs . add ( path . join ( cur , 'storage' , 'bin' ));
const parent = path . dirname ( cur );
if ( ! parent || parent === cur ) break ;
cur = parent ;
}
}
// 1. PeerTube plugin data directory & its parent storage directory (e.g. /var/www/peertube/storage/bin/yt-dlp or /data/bin/yt-dlp)
try {
if (
peertubeHelpers &&
peertubeHelpers . plugin &&
typeof peertubeHelpers . plugin . getDataDirectoryPath === 'function'
) {
addWithParents ( peertubeHelpers . plugin . getDataDirectoryPath (), 4 );
}
} catch ( e ) {}
// 2. Plugin installation directory (__dirname) & working directory (process.cwd())
addWithParents ( __dirname , 4 );
addWithParents ( process . cwd (), 3 );
// 3. System PATH environment variable directories (pure Node.js `which` / `where` equivalent)
const pathEnv = process . env . PATH || process . env . Path || '' ;
for ( const dir of pathEnv . split ( path . delimiter )) {
const cleanDir = dir . trim ();
if ( cleanDir ) searchDirs . add ( cleanDir );
}
// 4. Common Linux, Docker, macOS, and Windows binary locations
if ( isWin ) {
searchDirs . add ( 'D:\\yaktube_storage\\bin' );
searchDirs . add ( 'C:\\yaktube_storage\\bin' );
searchDirs . add ( 'C:\\laragon\\bin' );
searchDirs . add ( 'C:\\ProgramData\\chocolatey\\bin' );
if ( process . env . LOCALAPPDATA ) {
searchDirs . add ( path . join ( process . env . LOCALAPPDATA , 'Microsoft' , 'WinGet' , 'Links' ));
}
if ( process . env . USERPROFILE ) {
searchDirs . add ( path . join ( process . env . USERPROFILE , 'scoop' , 'shims' ));
searchDirs . add ( path . join ( process . env . USERPROFILE , 'AppData' , 'Local' , 'Microsoft' , 'WinGet' , 'Links' ));
}
} else {
searchDirs . add ( '/var/www/peertube/storage/bin' );
searchDirs . add ( '/var/lib/peertube/storage/bin' );
searchDirs . add ( '/data/bin' );
searchDirs . add ( '/usr/local/bin' );
searchDirs . add ( '/usr/bin' );
searchDirs . add ( '/bin' );
searchDirs . add ( '/snap/bin' );
searchDirs . add ( '/opt/homebrew/bin' );
searchDirs . add ( '/home/peertube/.local/bin' );
try {
const home = os . homedir ();
if ( home ) searchDirs . add ( path . join ( home , '.local' , 'bin' ));
2026-09-19 05:14:11 +03:00
} catch ( e ) {}
}
2026-11-05 18:02:25 +03:00
const candidates = [];
for ( const dir of searchDirs ) {
for ( const bName of binNames ) {
candidates . push ( path . join ( dir , bName ));
}
}
return candidates ;
}
function resolveYtDlpPath ( logger , customPath , peertubeHelpers ) {
const cleanCustom = customPath ? String ( customPath ). trim () : '' ;
if ( cleanCustom && isExecutableFile ( cleanCustom )) {
if ( cachedYtDlpPath !== cleanCustom ) {
cachedYtDlpPath = cleanCustom ;
cachedYtDlpCustomSetting = cleanCustom ;
if ( logger ) logger . info ( `[YakTube OnDemand] Using custom configured yt-dlp binary at: ${ cachedYtDlpPath } ` );
}
return cachedYtDlpPath ;
}
if ( cachedYtDlpPath && cachedYtDlpCustomSetting === cleanCustom && isExecutableFile ( cachedYtDlpPath )) {
return cachedYtDlpPath ;
}
if ( process . env . YTDLP_PATH && isExecutableFile ( process . env . YTDLP_PATH )) {
cachedYtDlpPath = process . env . YTDLP_PATH ;
cachedYtDlpCustomSetting = cleanCustom ;
if ( logger ) logger . info ( `[YakTube OnDemand] Using YTDLP_PATH binary at: ${ cachedYtDlpPath } ` );
return cachedYtDlpPath ;
}
const candidates = discoverDynamicCandidatePaths ( peertubeHelpers );
for ( const candidate of candidates ) {
if ( isExecutableFile ( candidate )) {
cachedYtDlpPath = candidate ;
cachedYtDlpCustomSetting = cleanCustom ;
if ( logger )
logger . info ( `[YakTube OnDemand] Auto-detected yt-dlp binary ( ${ process . platform } ) at: ${ cachedYtDlpPath } ` );
return cachedYtDlpPath ;
}
}
// Trigger background auto-download if not found on disk yet
ensureYtDlpBinaryDownloaded ( logger , peertubeHelpers ). catch (() => {});
return process . platform === 'win32' ? 'yt-dlp.exe' : 'yt-dlp' ;
}
function downloadFileFollowRedirects ( url , destPath , maxRedirects = 5 ) {
return new Promise (( resolve , reject ) => {
if ( maxRedirects < 0 ) return reject ( new Error ( 'Too many redirects' ));
const client = url . startsWith ( 'https' ) ? https : http ;
const req = client . get (
url ,
{
headers : {
'User-Agent' : 'YakTube-OnDemand-Plugin/1.7 (PeerTube)'
},
timeout : 60000
},
res => {
if ( res . statusCode >= 300 && res . statusCode < 400 && res . headers . location ) {
res . resume ();
const nextUrl = new URL ( res . headers . location , url ). toString ();
return resolve ( downloadFileFollowRedirects ( nextUrl , destPath , maxRedirects - 1 ));
}
if ( res . statusCode !== 200 ) {
res . resume ();
return reject ( new Error ( `HTTP ${ res . statusCode } when downloading yt-dlp` ));
}
const tmpPath = ` ${ destPath } .tmp- ${ process . pid } ` ;
const fileStream = fs . createWriteStream ( tmpPath , { mode : 0o755 });
res . pipe ( fileStream );
fileStream . on ( 'finish' , () => {
fileStream . close (() => {
try {
if ( process . platform !== 'win32' ) {
fs . chmodSync ( tmpPath , 0o755 );
}
fs . renameSync ( tmpPath , destPath );
resolve ( destPath );
} catch ( err ) {
reject ( err );
}
});
});
fileStream . on ( 'error' , err => {
try {
fs . unlinkSync ( tmpPath );
} catch ( _ ) {}
reject ( err );
});
}
);
req . on ( 'error' , reject );
});
}
async function ensureYtDlpBinaryDownloaded ( logger , peertubeHelpers ) {
if ( autoDownloadPromise ) return autoDownloadPromise ;
autoDownloadPromise = ( async () => {
try {
const isWin = process . platform === 'win32' ;
const isMac = process . platform === 'darwin' ;
const assetName = isWin ? 'yt-dlp.exe' : isMac ? 'yt-dlp_macos' : 'yt-dlp' ;
const targetFileName = isWin ? 'yt-dlp.exe' : 'yt-dlp' ;
let targetDir = path . join ( __dirname , 'bin' );
try {
if (
peertubeHelpers &&
peertubeHelpers . plugin &&
typeof peertubeHelpers . plugin . getDataDirectoryPath === 'function'
) {
targetDir = path . join ( peertubeHelpers . plugin . getDataDirectoryPath (), 'bin' );
}
} catch ( e ) {}
fs . mkdirSync ( targetDir , { recursive : true });
const targetPath = path . join ( targetDir , targetFileName );
if ( isExecutableFile ( targetPath )) {
cachedYtDlpPath = targetPath ;
return targetPath ;
}
const downloadUrl = `https://github.com/yt-dlp/yt-dlp/releases/latest/download/ ${ assetName } ` ;
if ( logger ) {
logger . info ( `[YakTube OnDemand] yt-dlp not found on system; auto-downloading ${ assetName } to ${ targetPath } ...` );
}
await downloadFileFollowRedirects ( downloadUrl , targetPath );
if ( isExecutableFile ( targetPath )) {
cachedYtDlpPath = targetPath ;
if ( logger ) logger . info ( `[YakTube OnDemand] Successfully auto-installed yt-dlp at: ${ targetPath } ` );
return targetPath ;
}
} catch ( e ) {
if ( logger ) logger . warn ( `[YakTube OnDemand] Auto-download of yt-dlp warning: ${ e . message } ` );
} finally {
autoDownloadPromise = null ;
}
return null ;
})();
return autoDownloadPromise ;
2026-09-19 05:14:11 +03:00
}
2026-08-29 12:42:36 +03:00
function getCached ( q ) {
const item = searchCache . get ( q );
if ( item && Date . now () - item . time < CACHE_TTL ) {
return item . data ;
}
return null ;
}
function setCache ( q , data ) {
searchCache . set ( q , { time : Date . now (), data });
if ( searchCache . size > 200 ) {
const firstKey = searchCache . keys (). next (). value ;
searchCache . delete ( firstKey );
}
}
2026-10-03 10:45:58 +03:00
function postInnerTubeJSON ( url , data ) {
2026-08-29 12:42:36 +03:00
return new Promise (( resolve , reject ) => {
2026-10-03 10:45:58 +03:00
const postData = JSON . stringify ( data );
const req = https . request (
url ,
2026-08-29 12:42:36 +03:00
{
2026-10-03 10:45:58 +03:00
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
'Content-Length' : Buffer . byteLength ( postData ),
'User-Agent' :
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36'
}
2026-08-29 12:42:36 +03:00
},
res => {
let body = '' ;
res . on ( 'data' , chunk => ( body += chunk ));
res . on ( 'end' , () => {
try {
2026-10-03 10:45:58 +03:00
resolve ( JSON . parse ( body ));
2026-08-29 12:42:36 +03:00
} catch ( e ) {
reject ( e );
}
});
}
);
req . on ( 'error' , reject );
2026-10-03 10:45:58 +03:00
req . write ( postData );
2026-08-29 12:42:36 +03:00
req . end ();
});
}
2026-10-03 11:14:03 +03:00
let pruneIntervalTimer = null ;
2026-10-23 08:59:50 +03:00
const DEFAULT_BLOCKED_KEYWORDS = [
'porn' ,
'porno' ,
'sex' ,
'seksi' ,
'sexy' ,
'strip' ,
'striptease' ,
'bikini' ,
'lingerie' ,
'see-through' ,
'sheer' ,
'babydoll' ,
'gravure' ,
2026-11-03 18:51:10 +03:00
'bra' ,
'plus-size bra' ,
'try on' ,
'try-on' ,
'underwear' ,
'panties' ,
'thong' ,
'cleavage' ,
'twerk' ,
'cameltoe' ,
2026-10-23 08:59:50 +03:00
'nude' ,
'nudes' ,
'nudist' ,
'erotik' ,
'erotic' ,
'erotizm' ,
'çıplak' ,
'ciplak' ,
'soyunma' ,
'soyun' ,
'sikiş' ,
'sikis' ,
'amcı k' ,
'amcik' ,
'yarak' ,
'yarrak' ,
'penis' ,
'vagina' ,
'vajina' ,
'mastürbasyon' ,
'masturbasyon' ,
'adult' ,
'+18' ,
'nsfw' ,
'xxx' ,
2026-11-03 18:51:10 +03:00
'xvideos' ,
2026-10-23 08:59:50 +03:00
'onlyfans' ,
'sissy' ,
'seduce' ,
'seduces' ,
'seducing' ,
'seductress' ,
'milf' ,
'deepthroat' ,
'blowjob' ,
'anal' ,
'hentai' ,
'boob' ,
'boobs' ,
2026-11-06 05:41:30 +03:00
'prnstars' ,
'pornstar' ,
'pornstars' ,
'brazzers' ,
'brattysis' ,
'nubiles' ,
'naughty america' ,
'sexploitation' ,
'twerking' ,
'gyat' ,
'nalgona' ,
'pawg' ,
'ssbbw' ,
'bimbofication' ,
'femdom' ,
'fetish' ,
'ear licking' ,
'mic licking' ,
'lens licking' ,
'foot tickle' ,
'tickle feet' ,
'foot goddess' ,
'stepsister' ,
'stepmom' ,
'sara jay' ,
'julia ann' ,
'lisa ann' ,
'alexis texas' ,
'lexi belle' ,
'alyx star' ,
'lady sonia' ,
'krazee klipz' ,
2026-10-23 08:59:50 +03:00
'uyuşturucu' ,
'uyusturucu' ,
'esrar' ,
'kokain' ,
'eroin' ,
'bonzai' ,
'bahis' ,
'kumar' ,
'bet' ,
'casino'
];
function isContentBlocked ( text , list = DEFAULT_BLOCKED_KEYWORDS ) {
if ( ! text || typeof text !== 'string' ) return false ;
const clean = text . toLowerCase (). replace ( /['\"\-_\/\\.,;:\(\)\[\]#!\?]/g , ' ' );
const words = clean . split ( /\s+/ ). filter ( Boolean );
for ( const item of list ) {
const target = item . trim (). toLowerCase ();
if ( ! target ) continue ;
if ( target . includes ( ' ' )) {
if ( clean . includes ( target )) return true ;
} else {
if ( words . includes ( target )) return true ;
}
}
return false ;
}
2026-11-01 10:39:57 +03:00
async function register ({ registerHook , getRouter , registerSetting , settingsManager , peertubeHelpers , logger }) {
2026-08-29 12:42:36 +03:00
const router = getRouter ();
2026-11-03 18:51:10 +03:00
let guestImportLimit = 5 ;
let maxDiskUsagePercent = 85 ;
let yaknetSsoEnabled = true ;
let smartPrefetchMode = 'auto-when-no-local' ;
2026-11-03 19:01:59 +03:00
let federatedRecommendationsMode = 'smart-relevant' ;
2026-11-03 18:51:10 +03:00
let customChannelId = '' ;
let customYtDlpPath = '' ;
let safeSearchEnabled = true ;
let blockedKeywordsList = [... DEFAULT_BLOCKED_KEYWORDS ];
// YakTube Bildirim Filtresi & SafeSearch Video Listesi Filtresi
2026-11-01 10:39:57 +03:00
if ( typeof registerHook === 'function' ) {
registerHook ({
target : 'filter:notifier.notification.enabled.result' ,
handler : async ( result , { notification }) => {
if ( notification && ( notification . type === 7 || notification . type === 'my-video-import-success' )) {
return {
webNotificationEnabled : false ,
emailNotificationEnabled : false
};
}
return result ;
}
});
2026-11-03 18:51:10 +03:00
const filterVideoListResult = async result => {
if ( ! safeSearchEnabled || ! result || ! Array . isArray ( result . data )) return result ;
const beforeLen = result . data . length ;
result . data = result . data . filter ( v => ! v || ! isContentBlocked ( v . name || '' , blockedKeywordsList ));
const diff = beforeLen - result . data . length ;
if ( diff > 0 && typeof result . total === 'number' ) {
result . total = Math . max ( 0 , result . total - diff );
}
return result ;
};
registerHook ({
target : 'filter:api.videos.list.result' ,
handler : filterVideoListResult
});
registerHook ({
target : 'filter:api.overviews.videos.list.result' ,
handler : filterVideoListResult
});
if ( logger ) logger . info ( '[YakTube OnDemand] Registered notification filter & SafeSearch video list hooks.' );
}
2026-10-03 10:26:46 +03:00
if ( typeof registerSetting === 'function' ) {
2026-11-03 18:06:54 +03:00
registerSetting ({
name : 'enable-yaknet-sso' ,
label : 'YakNet SSO ve Merkezi Hesap Entegrasyonu' ,
type : 'select' ,
options : [
{ label : 'Açık (Resmi YakNet Giriş & Hesap Entegrasyonu)' , value : 'true' },
{ label : 'Kapalı (Standart Bağımsı z PeerTube Girişi)' , value : 'false' }
],
description :
'YakNet SSO ile tek tı kla oturum açma, YakNet hesap kartı ve merkezi arama geçmişi eşitlemesini etkinleştirir. Bu özelliği kullanmak için peertube-plugin-auth-yaknet eklentisinin de kurulu olması önerilir. Kapatı ldığında platform standart bağımsı z PeerTube hesap sistemiyle çalışır.' ,
private : false ,
default : 'true'
});
2026-11-03 18:51:10 +03:00
registerSetting ({
name : 'smart-prefetch-mode' ,
label : 'Akı llı Arka Plan Öneri Hazı rlama (Smart Pre-Fetch)' ,
type : 'select' ,
options : [
{
label : 'Otomatik (Önerilen — Yerel benzer video yoksa en alakalı 1. öneriyi arka planda hazı rla)' ,
value : 'auto-when-no-local'
},
{
label : 'Her Zaman (İzlenen her videoda en alakalı 1. YouTube önerisini arka planda hazı rla)' ,
value : 'always'
},
{
label : 'Kapalı (Sadece kullanı cı İndir ve İzle butonuna tı kladığında aktar)' ,
value : 'disabled'
}
],
description :
'İzleyici bir videoyu en az 25 saniye izlediğinde, sunucuda o konuyla ilgili yerel video yoksa en alakalı 1. YouTube önerisini arka planda sessizce hazı rlar. Böylece video bittiğinde alakası z videolar yerine doğrudan ilgili içerik kesintisiz oynatı lı r. Sunucu kaynakları nı korumak isteyen yöneticiler bu ayarı kapatabilir.' ,
private : false ,
default : 'auto-when-no-local'
});
2026-11-03 19:01:59 +03:00
registerSetting ({
name : 'federated-recommendations-mode' ,
label : 'Fediverse (Diğer PeerTube Sunucuları ) Öneri Görünümü' ,
type : 'select' ,
options : [
{
label :
'Akı llı Alaka Süzgeci (Önerilen — Konuyla ve geçmişle alakalı Fediverse videoları nı 🌐 rozetiyle önerilere dahil et, diğerlerini açılı r buton altı nda tut)' ,
value : 'smart-relevant'
},
{
label :
'Tam Açık (Akı llı önerilerin altı nda PeerTube standart Fediverse öneri listesini de her zaman açık bı rak)' ,
value : 'show-all'
},
{
label : 'Kapalı (Önerilerde yalnı zca bu sunucudaki yerel videoları ve YouTube sonuçları nı göster)' ,
value : 'disabled'
}
],
description :
'İzleme sayfası ndaki önerilerde Fediverse (diğer PeerTube sunucuları ndan gelen federe videolar) içeriklerinin nası l sunulacağını belirler. Varsayı lan "Akı llı Alaka Süzgeci" modu Fediverse videoları nı engellemez; izlenen videoyla, dille veya kullanı cı geçmişiyle alakalı olan Fediverse videoları nı "🌐 Fediverse\'de İzle" rozetiyle doğrudan önerilere dahil eder, alakası z/yabancı yayı nları ise isteğe bağlı "🌐 Fediverse Ağındaki Diğer Videoları Göster" çekmecesi altı nda toplar.' ,
private : false ,
default : 'smart-relevant'
});
2026-10-23 08:59:50 +03:00
registerSetting ({
name : 'safe-search-enabled' ,
label : 'Güvenli Okul & Aile Araması (SafeSearch)' ,
type : 'select' ,
options : [
{ label : 'Açık (Önerilen - Katı Çocuk ve Aile Filtresi)' , value : 'true' },
{ label : 'Kapalı (Filtresiz)' , value : 'false' }
],
description : 'Açık olduğunda arama sonuçları nda katı çocuk ve aile güvenliği filtresi uygulanı r.' ,
private : false ,
default : 'true'
});
registerSetting ({
name : 'blocked-keywords' ,
label : 'Yasaklı / Filtrelenecek Kelimeler (Çocuk & Aile Güvenliği)' ,
type : 'input-textarea' ,
description :
'Arama ve video aktarma işlemlerinde filtrelenecek anahtar kelimeler listesi. Virgülle veya alt alta yazabilirsiniz. Bu kelimeleri içeren aramalar engellenir ve uygunsuz videolar sisteme aktarı lamaz.' ,
private : false ,
default : DEFAULT_BLOCKED_KEYWORDS . join ( ', ' )
});
2026-10-03 10:26:46 +03:00
registerSetting ({
name : 'guest-import-limit' ,
label : 'Misafir Video İndirme Sı nı rı (Kota)' ,
type : 'input' ,
description :
'Giriş yapmamış (misafir) kullanı cı ları n YouTube üzerinden sunucuya aktarabileceği maksimum video sayı sı (Örn: 5). Sı nı r istemiyorsanı z (sı nı rsı z) 0 yazı n.' ,
private : false ,
default : '5'
});
2026-10-03 10:45:58 +03:00
2026-10-03 11:14:03 +03:00
registerSetting ({
name : 'max-disk-usage-percent' ,
label : 'Otomatik Disk Temizleme Sı nı rı (%)' ,
type : 'input' ,
description :
'Sunucu diski bu doluluk yüzdesine (Örn: 85) ulaştığında, en eski ve en az izlenen videolar otomatik olarak silinerek yer açılı r. Otomatik silmeyi kapatmak için 0 yazı n.' ,
private : false ,
default : '85'
});
2026-10-03 10:45:58 +03:00
registerSetting ({
name : 'default-channel-id' ,
label : 'Varsayı lan Video Kanalı ID (Opsiyonel)' ,
type : 'input' ,
description :
'İndirilen videoları n yükleneceği kanal ID numarası . Boş bı rakı lı rsa sunucu yöneticisinin (Admin) ilk video kanalı otomatik seçilir.' ,
private : false ,
default : ''
});
registerSetting ({
name : 'ytdlp-binary-path' ,
label : 'yt-dlp Çalıştı rı labilir Dosya Yolu (Opsiyonel)' ,
type : 'input' ,
description :
'Özel yt-dlp dosya yolu (Örn: /usr/local/bin/yt-dlp). Boş bı rakı lı rsa sistemde otomatik tespit edilir.' ,
private : true ,
default : ''
});
2026-10-18 11:26:23 +03:00
registerSetting ({
name : 'home-categories' ,
label : 'Ana Sayfa Kategori Çipleri (YouTube Tarzı )' ,
type : 'input-textarea' ,
description :
2026-10-18 12:03:32 +03:00
'Ana sayfada video ı zgarası nı n üstünde gösterilecek kategori butonları nı düzenleyin, silin veya yenilerini ekleyin. Virgülle veya alt alta yazabilirsiniz (Örn: Tümü, Müzik, Oyun, Canlı , Rap yapma, Evcil hayvanlar, Komedi skeç gösterileri, Doğa, Futbol, Son yüklenenler, İzlenenler, Yeni öneriler).' ,
2026-10-18 11:26:23 +03:00
private : false ,
2026-10-18 12:03:32 +03:00
default :
'Tümü, Müzik, Oyun, Canlı , Rap yapma, Evcil hayvanlar, Komedi skeç gösterileri, Doğa, Futbol, Son yüklenenler, İzlenenler, Yeni öneriler'
2026-10-18 11:26:23 +03:00
});
2026-10-03 10:26:46 +03:00
}
2026-10-18 12:03:32 +03:00
let rawHomeCategories =
'Tümü, Müzik, Oyun, Canlı , Rap yapma, Evcil hayvanlar, Komedi skeç gösterileri, Doğa, Futbol, Son yüklenenler, İzlenenler, Yeni öneriler' ;
2026-10-18 11:26:23 +03:00
let parsedHomeCategories = [];
function parseHomeCategories ( str ) {
if ( ! str || typeof str !== 'string' || str . trim () === '' ) {
2026-10-18 12:03:32 +03:00
str =
'Tümü, Müzik, Oyun, Canlı , Rap yapma, Evcil hayvanlar, Komedi skeç gösterileri, Doğa, Futbol, Son yüklenenler, İzlenenler, Yeni öneriler' ;
2026-10-18 11:26:23 +03:00
}
2026-10-18 12:03:32 +03:00
const lines = str
. split ( /[\n,]+/ )
. map ( s => s . trim ())
. filter ( Boolean );
2026-10-18 11:26:23 +03:00
const result = [];
const categoryMap = {
2026-10-18 12:03:32 +03:00
müzik : 1 ,
music : 1 ,
film : 2 ,
filmler : 2 ,
films : 2 ,
sinema : 2 ,
cinema : 2 ,
araçlar : 3 ,
otomotiv : 3 ,
arabalar : 3 ,
vehicles : 3 ,
cars : 3 ,
sanat : 4 ,
art : 4 ,
spor : 5 ,
sports : 5 ,
futbol : 5 ,
football : 5 ,
seyahat : 6 ,
gezi : 6 ,
travel : 6 ,
travels : 6 ,
doğa : 6 ,
doga : 6 ,
oyun : 7 ,
oyunlar : 7 ,
gaming : 7 ,
games : 7 ,
insanlar : 8 ,
blog : 8 ,
vlog : 8 ,
people : 8 ,
komedi : 9 ,
mizah : 9 ,
comedy : 9 ,
funny : 9 ,
'komedi skeç gösterileri' : 9 ,
'komedi skeç' : 9 ,
'komedi skeçleri' : 9 ,
skeç : 9 ,
eğlence : 10 ,
eglence : 10 ,
entertainment : 10 ,
haberler : 11 ,
haber : 11 ,
gündem : 11 ,
siyaset : 11 ,
politika : 11 ,
news : 11 ,
'nası l yapı lı r' : 12 ,
'pratik bilgiler' : 12 ,
'how to' : 12 ,
howto : 12 ,
eğitim : 13 ,
egitim : 13 ,
ders : 13 ,
education : 13 ,
aktivizm : 14 ,
activism : 14 ,
bilim : 15 ,
teknoloji : 15 ,
'bilim ve teknoloji' : 15 ,
'bilim & teknoloji' : 15 ,
science : 15 ,
technology : 15 ,
yazı lı m : 15 ,
hayvanlar : 16 ,
'evcil hayvanlar' : 16 ,
evcil : 16 ,
animals : 16 ,
pets : 16 ,
çocuklar : 17 ,
çocuk : 17 ,
kids : 17 ,
yemek : 18 ,
mutfak : 18 ,
food : 18 ,
cooking : 18
2026-10-18 11:26:23 +03:00
};
for ( const item of lines ) {
2026-11-03 18:51:10 +03:00
const lower = item . toLocaleLowerCase ( 'tr' ). replace ( /i\u0307/g , 'i' );
2026-10-18 11:26:23 +03:00
if ( lower === 'tümü' || lower === 'all' || lower === 'hepsi' ) {
result . push ({
label : item ,
key : 'all' ,
url : '/videos/browse'
});
continue ;
}
if ( item . includes ( ':' )) {
const parts = item . split ( ':' );
const label = parts [ 0 ]. trim ();
const val = parts . slice ( 1 ). join ( ':' ). trim ();
if ( /^\d+$/ . test ( val )) {
result . push ({
label ,
key : 'cat_' + val ,
url : `/videos/browse?categoryOneOf= ${ val } `
});
} else if ( val === 'live' || val === 'isLive' ) {
result . push ({
label ,
key : 'live' ,
url : '/videos/browse?isLive=true'
});
} else if ( val . startsWith ( 'sort=' )) {
2026-10-18 12:03:32 +03:00
const sVal = val . split ( '=' )[ 1 ];
const cleanKey = sVal . replace ( /^-/ , '' );
2026-10-18 11:26:23 +03:00
result . push ({
label ,
2026-10-18 12:03:32 +03:00
key : 'sort_' + cleanKey ,
2026-10-18 11:26:23 +03:00
url : `/videos/browse? ${ val } `
});
} else if ( val . startsWith ( 'search=' )) {
result . push ({
label ,
key : 'search_' + val . split ( '=' )[ 1 ],
url : `/search?search= ${ encodeURIComponent ( val . split ( '=' )[ 1 ]) } `
});
} else {
result . push ({
label ,
key : 'search_' + val ,
url : `/search?search= ${ encodeURIComponent ( val ) } `
});
}
continue ;
}
if ( lower === 'canlı ' || lower === 'canlı yayı n' || lower === 'canlı yayı nlar' || lower === 'live' ) {
result . push ({
label : item ,
key : 'live' ,
url : '/videos/browse?isLive=true'
});
2026-10-18 12:03:32 +03:00
} else if ( lower === 'son yüklenenler' || lower === 'son yuklenenler' || lower === 'en yeniler' ) {
result . push ({
label : item ,
key : 'sort_publishedAt' ,
url : '/videos/browse?sort=-publishedAt'
});
} else if (
lower === 'izlenenler' ||
lower === 'en çok izlenenler' ||
lower === 'en cok izlenenler' ||
lower === 'popüler' ||
lower === 'populer'
) {
result . push ({
label : item ,
key : 'sort_views' ,
url : '/videos/browse?sort=-views'
});
} else if (
lower === 'yeni öneriler' ||
lower === 'yeni oneriler' ||
lower === 'öneriler' ||
lower === 'oneriler' ||
lower === 'trendler' ||
lower === 'popülerler'
) {
result . push ({
label : item ,
key : 'sort_hot' ,
url : '/videos/browse?sort=-hot'
});
} else if ( lower === 'rap yapma' || lower === 'rap' ) {
result . push ({
label : item ,
key : 'search_rap' ,
url : '/search?search=rap'
});
} else if ( lower === 'mixler' || lower === "mix'ler" || lower === 'mix' ) {
result . push ({
label : item ,
key : 'search_mix' ,
url : '/search?search=mix'
});
} else if ( lower === 'podcastler' || lower === "podcast'ler" || lower === 'podcast' ) {
result . push ({
label : item ,
key : 'search_podcast' ,
url : '/search?search=podcast'
});
2026-10-18 11:26:23 +03:00
} else if ( categoryMap [ lower ] !== undefined ) {
const catId = categoryMap [ lower ];
result . push ({
label : item ,
key : 'cat_' + catId ,
url : `/videos/browse?categoryOneOf= ${ catId } `
});
} else {
result . push ({
label : item ,
2026-10-18 12:03:32 +03:00
key : 'search_' + lower . replace ( /[^a-z0-9_-]/gi , '_' ),
2026-10-18 11:26:23 +03:00
url : `/search?search= ${ encodeURIComponent ( item ) } `
});
}
}
return result ;
}
parsedHomeCategories = parseHomeCategories ( rawHomeCategories );
2026-11-17 23:15:56 +03:00
let lastSettingsLoadTime = 0 ;
const SETTINGS_CACHE_TTL_MS = 60 * 1000 ;
async function loadPluginSettings ( force = false ) {
2026-10-03 10:26:46 +03:00
if ( ! settingsManager || typeof settingsManager . getSetting !== 'function' ) return ;
2026-11-17 23:15:56 +03:00
if ( ! force && lastSettingsLoadTime > 0 && Date . now () - lastSettingsLoadTime < SETTINGS_CACHE_TTL_MS ) {
return ;
}
lastSettingsLoadTime = Date . now ();
2026-10-03 10:26:46 +03:00
try {
const val = await settingsManager . getSetting ( 'guest-import-limit' );
if ( val !== undefined && val !== null && String ( val ). trim () !== '' ) {
const parsed = parseInt ( String ( val ). trim (), 10 );
guestImportLimit = isNaN ( parsed ) || parsed < 0 ? 5 : parsed ;
} else {
guestImportLimit = 5 ;
}
} catch ( e ) {}
2026-10-03 10:45:58 +03:00
2026-10-03 11:14:03 +03:00
try {
const diskVal = await settingsManager . getSetting ( 'max-disk-usage-percent' );
if ( diskVal !== undefined && diskVal !== null && String ( diskVal ). trim () !== '' ) {
const parsedDisk = parseInt ( String ( diskVal ). trim (), 10 );
maxDiskUsagePercent = isNaN ( parsedDisk ) || parsedDisk < 0 ? 85 : Math . min ( 99 , parsedDisk );
} else {
maxDiskUsagePercent = 85 ;
}
} catch ( e ) {}
2026-10-03 10:45:58 +03:00
try {
const chVal = await settingsManager . getSetting ( 'default-channel-id' );
customChannelId = chVal ? String ( chVal ). trim () : '' ;
} catch ( e ) {}
try {
const binVal = await settingsManager . getSetting ( 'ytdlp-binary-path' );
customYtDlpPath = binVal ? String ( binVal ). trim () : '' ;
} catch ( e ) {}
2026-10-18 11:26:23 +03:00
try {
const catsVal = await settingsManager . getSetting ( 'home-categories' );
if ( catsVal !== undefined && catsVal !== null && String ( catsVal ). trim () !== '' ) {
rawHomeCategories = String ( catsVal ). trim ();
} else {
2026-10-18 12:03:32 +03:00
rawHomeCategories =
'Tümü, Müzik, Oyun, Canlı , Rap yapma, Evcil hayvanlar, Komedi skeç gösterileri, Doğa, Futbol, Son yüklenenler, İzlenenler, Yeni öneriler' ;
2026-10-18 11:26:23 +03:00
}
parsedHomeCategories = parseHomeCategories ( rawHomeCategories );
} catch ( e ) {}
2026-10-23 08:59:50 +03:00
2026-11-03 18:06:54 +03:00
try {
const ssoVal = await settingsManager . getSetting ( 'enable-yaknet-sso' );
if ( ssoVal !== undefined && ssoVal !== null ) {
yaknetSsoEnabled = String ( ssoVal ). trim () !== 'false' ;
} else {
yaknetSsoEnabled = true ;
}
} catch ( e ) {}
2026-11-03 18:51:10 +03:00
try {
const pfVal = await settingsManager . getSetting ( 'smart-prefetch-mode' );
if ( pfVal && [ 'auto-when-no-local' , 'always' , 'disabled' ]. includes ( String ( pfVal ). trim ())) {
smartPrefetchMode = String ( pfVal ). trim ();
} else {
smartPrefetchMode = 'auto-when-no-local' ;
}
} catch ( e ) {}
2026-11-03 19:01:59 +03:00
try {
const fedVal = await settingsManager . getSetting ( 'federated-recommendations-mode' );
if ( fedVal && [ 'smart-relevant' , 'show-all' , 'disabled' ]. includes ( String ( fedVal ). trim ())) {
federatedRecommendationsMode = String ( fedVal ). trim ();
} else {
federatedRecommendationsMode = 'smart-relevant' ;
}
} catch ( e ) {}
2026-10-23 08:59:50 +03:00
try {
const safeVal = await settingsManager . getSetting ( 'safe-search-enabled' );
if ( safeVal !== undefined && safeVal !== null ) {
safeSearchEnabled = String ( safeVal ). trim () !== 'false' ;
}
} catch ( e ) {}
try {
const wordsVal = await settingsManager . getSetting ( 'blocked-keywords' );
if ( wordsVal && typeof wordsVal === 'string' && wordsVal . trim () !== '' ) {
const list = wordsVal
. split ( /[\n,]+/ )
. map ( w => w . trim (). toLowerCase ())
. filter ( Boolean );
2026-11-03 18:51:10 +03:00
if ( list . length > 0 ) {
blockedKeywordsList = Array . from ( new Set ([... DEFAULT_BLOCKED_KEYWORDS , ... list ]));
}
} else {
blockedKeywordsList = [... DEFAULT_BLOCKED_KEYWORDS ];
2026-10-23 08:59:50 +03:00
}
} catch ( e ) {}
2026-10-03 10:26:46 +03:00
}
2026-11-17 23:15:56 +03:00
await loadPluginSettings ( true );
2026-10-03 10:26:46 +03:00
if ( settingsManager && typeof settingsManager . onSettingsChange === 'function' ) {
2026-11-17 23:15:56 +03:00
settingsManager . onSettingsChange (() => loadPluginSettings ( true ));
2026-10-03 10:26:46 +03:00
}
2026-11-05 18:02:25 +03:00
resolveYtDlpPath ( logger , customYtDlpPath , peertubeHelpers );
2026-10-03 10:26:46 +03:00
2026-10-03 10:45:58 +03:00
// Helper to run SQL queries via PeerTube's built-in Sequelize instance (works on ANY PeerTube server without bridge.js)
async function dbQuery ( sql , bindParams = []) {
if ( ! peertubeHelpers || ! peertubeHelpers . database || typeof peertubeHelpers . database . query !== 'function' ) {
return [];
}
const res = await peertubeHelpers . database . query ( sql , {
bind : bindParams
});
if ( Array . isArray ( res ) && Array . isArray ( res [ 0 ])) {
return res [ 0 ];
}
return Array . isArray ( res ) ? res : [];
}
// Helper to get local PeerTube HTTP connection details dynamically on any PeerTube instance
function getLocalServerTarget () {
let port = 9000 ;
let hostHeader = 'localhost' ;
try {
if ( peertubeHelpers && peertubeHelpers . config ) {
const listenCfg = peertubeHelpers . config . getServerListeningConfig ();
if ( listenCfg && listenCfg . port ) port = listenCfg . port ;
const webUrl = peertubeHelpers . config . getWebserverUrl ();
if ( webUrl ) {
const parsed = new URL ( webUrl );
hostHeader = parsed . host ;
}
}
} catch ( e ) {}
return { hostname : '127.0.0.1' , port , hostHeader };
}
// Universal Admin OAuth Token Resolver (Zero hardcoded passwords - works on ANY PeerTube server via peertubeHelpers.database)
let cachedAdminToken = null ;
let tokenExpiresAt = 0 ;
async function getAdminToken () {
if ( cachedAdminToken && Date . now () < tokenExpiresAt ) {
return cachedAdminToken ;
}
// 1. Check existing valid admin token in oAuthToken table
try {
const rows = await dbQuery (
`SELECT t."accessToken"
FROM "oAuthToken" t
INNER JOIN "user" u ON u.id = t."userId"
WHERE u.role = 0 AND t."accessTokenExpiresAt" > NOW() + INTERVAL '5 minutes'
ORDER BY t."accessTokenExpiresAt" DESC
LIMIT 1`
);
if ( rows . length > 0 && rows [ 0 ]. accessToken ) {
cachedAdminToken = rows [ 0 ]. accessToken ;
tokenExpiresAt = Date . now () + 4 * 60 * 1000 ;
return cachedAdminToken ;
}
// 2. If no active admin token exists, mint one directly in oAuthToken for the administrator (role = 0)
const admins = await dbQuery ( `SELECT id FROM "user" WHERE role = 0 ORDER BY id ASC LIMIT 1` );
const clients = await dbQuery ( `SELECT id FROM "oAuthClient" ORDER BY id ASC LIMIT 1` );
if ( admins . length > 0 && clients . length > 0 ) {
const accessToken = crypto . randomBytes ( 20 ). toString ( 'hex' );
const refreshToken = crypto . randomBytes ( 20 ). toString ( 'hex' );
await dbQuery (
`INSERT INTO "oAuthToken" ("accessToken", "accessTokenExpiresAt", "refreshToken", "refreshTokenExpiresAt", "oAuthClientId", "userId", "createdAt", "updatedAt")
VALUES ($1, NOW() + INTERVAL '7 days', $2, NOW() + INTERVAL '30 days', $3, $4, NOW(), NOW())` ,
[ accessToken , refreshToken , clients [ 0 ]. id , admins [ 0 ]. id ]
);
cachedAdminToken = accessToken ;
tokenExpiresAt = Date . now () + 30 * 60 * 1000 ;
return cachedAdminToken ;
}
} catch ( err ) {
if ( logger ) logger . warn ( '[YakTube OnDemand] DB token lookup warning: ' + err . message );
}
throw new Error ( 'Could not resolve or generate admin OAuth token' );
}
2026-11-06 05:33:47 +03:00
function classifyVideoCategoryAndChannel ( title , currentCategory , tags , description ) {
const rawT = String ( title || '' );
const t = rawT . toLocaleLowerCase ( 'tr' );
const tagStr = Array . isArray ( tags ) ? tags . filter ( Boolean ). join ( ' ' ). toLocaleLowerCase ( 'tr' ) : '' ;
const descStr = String ( description || '' )
. slice ( 0 , 400 )
. toLocaleLowerCase ( 'tr' );
const combined = ` ${ t } ${ tagStr } ` ;
const fullText = ` ${ combined } ${ descStr } ` ;
const cat = currentCategory ? Number ( currentCategory ) : null ;
// 1. Music (Category 1 -> YakTube Müzik)
const musicPattern =
/\b(müzik|muzik|music|musik|muziek|musiqi|müzikleri|muzikleri|şarkı |sarki|şarkı lar|sarkilar|klip|video\s*klip|official\s*video|official\s*music|official\s*audio|audio|karaoke|kareoke|akustik|acoustic|enstrümantal|enstrumantal|instrumental|sözleri|sozleri|lyrics|lyric|remix|mix|mashup|cover|ai\s*cover|freestyle|konser|canlı \s*performans|performans|netd|sony\s*music|dmc|poll\s*production|mahzen|tfb\s*records|dokuz\s*sekiz|avrupa\s*müzik|pasaj\s*müzik|türkçe\s*pop|turkce\s*pop|arabesk|rap|hip\s*hop|boombesk|chillebesk|halay|türkü|turku|türküleri|ilahi|kaside|fon\s*müziği|melodi|beat|feat\.?|ft\.?|mv|kpop|k-pop|aespa|sechskies|starakor|star\s*akor|akor|akorları |altyapı |alt\s*yapı )\b/i ;
const knownMusicArtists =
/\b(tarkan|sezen\s*aksu|gülşen|gulsen|ebru\s*yaşar|ebru\s*yasar|derya\s*uluğ|derya\s*ulug|asil\s*gök|ferhat\s*göçer|ferhat\s*gocer|sertab\s*erener|candan\s*erçetin|bahadı r\s*tatlıöz|reynmen|semicenk|burak\s*bulut|kurtuluş\s*kuş|tekir|mehmet\s*elmas|yı ldı rı m\s*elmas|ahmet\s*kaya|ibrahim\s*tatlı ses|müslüm\s*gürses|orhan\s*gencebay|barış\s*manço|cem\s*karaca|mabel\s*matiz|hadise|aleyna\s*tilki|simge|ziynet\s*sali|hande\s*yener|demet\s*akalı |mustafa\s*sandal|kenan\s*doğulu|yalı n|teoman|duman|mor\s*ve\s*ötesi|manga|sagopa|ceza|ezhel|uzi|cakal|çakal|lvbel|blok3|poizi|wegh|dedublüman|melek\s*mosso|zeynep\s*bastı k|melike\s*şahin|karsu|maya\s*perest|siyam|zeyd|gökhan\s*türkmen|emre\s*aydı n|oğuzhan\s*koç|murat\s*boz|edis|buray|ilyas\s*yalçıntaş|tuğçe\s*kandemir|bilal\s*sonses|sancak|taladro|canfeza|no\.?\s*1|şehinşah|hidra|allame|contra|norm\s*ender|ben\s*fero|motive|ati242|girl\s*in\s*red|tate\s*mcrae|rose|giselle|sunghoon|serkan\s*nişancı |yiğit\s*mahzuni|afşin\s*akyol)\b/i ;
if ( cat === 1 || /[🎶🎵🎤🎧🎼]/ . test ( rawT ) || musicPattern . test ( combined ) || knownMusicArtists . test ( combined )) {
return { category : 1 , bucket : 'music' , keywords : [ 'muzik' , 'müzik' , 'music' ] };
}
// 2. Podcast & Sohbet (-> YakTube Podcast & Sohbet)
const podcastPattern =
/\b(podcast|röportaj|roportaj|sohbet|söyleşi|soylesi|açık\s*mikrofon|konuk|bölüm\s*sohbet|talk\s*show|interview|in\s*your\s*dreams)\b/i ;
if ( podcastPattern . test ( combined )) {
return { category : cat || 8 , bucket : 'podcast' , keywords : [ 'podcast' , 'sohbet' ] };
}
// 3. Teknoloji, Bilim & Eğitim (Categories 12, 13, 15 -> YakTube Teknoloji & Eğitim)
const techEduPattern =
/\b(teknoloji|technology|bilim|science|yazı lı m|software|kodlama|coding|programlama|linux|windows|android|ios|iphone|samsung|yapay\s*zeka|artificial\s*intelligence|chatgpt|gemini|bitcoin|kripto|crypto|blockchain|donanı m|inceleme|review|rehber|tutorial|how\s*to|nası l\s*yapı lı r|eğitim|egitim|education|educational|ders|lecture|kurs|matematik|fizik|kimya|ingilizce|english\s*listening|learn\s*english|qcomputing|quantum|developer|open\s*source|kde|ubuntu|debian|seo|search\s*console|marketing)\b/i ;
if ( cat === 12 || cat === 13 || cat === 15 || techEduPattern . test ( combined )) {
return { category : cat || 13 , bucket : 'tech' , keywords : [ 'teknoloji' , 'tekno' , 'egitim' , 'eğitim' , 'tech' ] };
}
// 4. Oyun, Spor & Araçlar (Categories 3, 5, 7 -> YakTube Oyun & Spor)
const gameSportPattern =
/\b(oyun|gaming|game|gameplay|walkthrough|playthrough|steam|playstation|xbox|nintendo|minecraft|valorant|csgo|cs2|pubg|gta|roblox|fortnite|league\s*of\s*legends|osu|twitch|spor|sport|sports|futbol|football|soccer|basketbol|voleybol|şampiyonlar\s*ligi|champions\s*league|galatasaray|fenerbahçe|fenerbahce|beşiktaş|besiktas|trabzonspor|man\s*utd|manchester|real\s*madrid|barcelona|maç\s*özeti|highlights|fitness|workout|gym|yoga|stretching|wwe|undertaker|red\s*bull|extreme\s*sports|araba|araç|aracı |otomobil|sürüş|multijet|gokart|go\s*kart|karting|motor|f1|formula\s*1)\b/i ;
if ( cat === 3 || cat === 5 || cat === 7 || gameSportPattern . test ( combined )) {
return { category : cat || 5 , bucket : 'game_sport' , keywords : [ 'oyun' , 'spor' , 'game' , 'sport' ] };
}
// 5. Eğlence, Komedi, Film & Dizi (Categories 2, 9, 10 -> YakTube Eğlence & Dizi)
const entertainmentPattern =
/\b(komedi|comedy|mizah|komik|funny|skeç|skec|parodi|parodie|şaka|prank|eğlence|eglence|entertainment|film|movie|sinema|cinema|fragman|trailer|dizi|series|drama|episode|ep\.\s*\d+|bölüm|bolum|dramatizeme|kı sa\s*film|short\s*film|animasyon|animation|anime|çizgi\s*film|tiyatro|stand\s*up|dans|dance|koreografi|choreography|tiktok\s*compilation|challenge|show)\b/i ;
if ( cat === 2 || cat === 9 || cat === 10 || entertainmentPattern . test ( combined )) {
return { category : cat || 10 , bucket : 'entertainment' , keywords : [ 'eglence' , 'eğlence' , 'dizi' , 'komedi' ] };
}
// 6. Haber, Gündem, Gezi, Doğa & Yaşam (Categories 4, 6, 8, 11, 14, 16, 17, 18 -> YakTube Haber & Yaşam)
const newsLifePattern =
/\b(haber|haberler|gazete|gazetesi|news|gündem|siyaset|politika|ekonomi|belgesel|documentary|gezi|seyahat|travel|vlog|doğa|doga|nature|kamp|hayvan|animal|kedi|cat|köpek|dog|yemek|mutfak|tarif|food|cooking|moda|fashion|style|outfit|wear|trend|alışveriş|haul|yaşam|lifestyle|sağlı k|health|çocuk|kids|sanat|art)\b/i ;
if ([ 4 , 6 , 11 , 14 , 16 , 17 , 18 ]. includes ( cat ) || newsLifePattern . test ( combined ) || newsLifePattern . test ( fullText )) {
return { category : cat || 8 , bucket : 'news_life' , keywords : [ 'yasam' , 'yaşam' , 'haber' , 'gezi' ] };
}
// 7. Artist - Song fallback for "Artist - Song Title" format
if ( /^[^\-|]{2,35}\s*[\-\u2010-\u2015]\s*[^\-|]{2,60}$/ . test ( rawT . trim ())) {
return { category : 1 , bucket : 'music' , keywords : [ 'muzik' , 'müzik' , 'music' ] };
}
return { category : cat || 8 , bucket : 'general' , keywords : [ 'genel' , 'general' , 'main' ] };
}
async function loadOfficialChannels () {
const allAdminChannels = await dbQuery (
`SELECT vc.id, vc.name, act."preferredUsername" AS handle, a.name AS "accountName", u.id AS "userId"
FROM "videoChannel" vc
INNER JOIN "actor" act ON act."videoChannelId" = vc.id
INNER JOIN "account" a ON vc."accountId" = a.id
INNER JOIN "user" u ON a."userId" = u.id
WHERE u.role = 0 AND act."serverId" IS NULL
ORDER BY vc.id ASC`
);
const yaktubeChannels = allAdminChannels . filter (
c =>
( c . accountName && c . accountName . toLowerCase () === 'yaktube' ) ||
( c . handle && c . handle . toLowerCase (). startsWith ( 'yaktube_' )) ||
Number ( c . userId ) === 1
);
return yaktubeChannels . length > 0 ? yaktubeChannels : allAdminChannels ;
}
function pickChannelIdFromList ( channels , decision ) {
if ( ! channels || channels . length === 0 ) return '1' ;
for ( const kw of decision . keywords || []) {
const found = channels . find (
c =>
( c . handle && c . handle . toLocaleLowerCase ( 'tr' ). includes ( kw )) ||
( c . name && c . name . toLocaleLowerCase ( 'tr' ). includes ( kw ))
);
if ( found ) return String ( found . id );
}
const genel =
channels . find (
c =>
( c . handle && c . handle . toLowerCase (). includes ( 'genel' )) ||
( c . name && c . name . toLocaleLowerCase ( 'tr' ). includes ( 'genel' ))
) || channels [ 0 ];
return String ( genel . id );
}
2026-10-03 10:45:58 +03:00
// Universal Channel Resolver (Works on any PeerTube instance + smart category mapping)
async function resolveTargetChannelId ( title , explicitChannelId ) {
if ( explicitChannelId ) return String ( explicitChannelId );
if ( customChannelId ) return String ( customChannelId );
try {
2026-11-06 05:33:47 +03:00
const channels = await loadOfficialChannels ();
2026-10-03 10:45:58 +03:00
if ( channels . length > 0 ) {
2026-11-06 05:33:47 +03:00
const decision = classifyVideoCategoryAndChannel ( title , null , [], '' );
return pickChannelIdFromList ( channels , decision );
2026-10-03 10:45:58 +03:00
}
2026-11-06 05:33:47 +03:00
} catch ( e ) {
if ( logger ) logger . warn ( '[YakTube OnDemand] resolveTargetChannelId warning: ' + e . message );
}
2026-10-03 10:45:58 +03:00
return '1' ;
}
2026-11-06 05:33:47 +03:00
// Post-import synchronizer: once yt-dlp populates a video's YouTube category & tags, ensure it is in the right channel
async function syncVideoChannelAndCategory ( videoId ) {
if ( ! videoId || customChannelId ) return ;
try {
const rows = await dbQuery (
`SELECT v.id, v.name, v.category, v."channelId", v.description,
(SELECT array_agg(t.name) FROM "videoTag" vt INNER JOIN "tag" t ON t.id = vt."tagId" WHERE vt."videoId" = v.id) AS tags
FROM "video" v
WHERE v.id = $1 AND v.remote = false
LIMIT 1` ,
[ Number ( videoId )]
);
if ( rows . length === 0 ) return ;
const v = rows [ 0 ];
const channels = await loadOfficialChannels ();
if ( channels . length === 0 ) return ;
const decision = classifyVideoCategoryAndChannel ( v . name , v . category , v . tags , v . description );
const targetChannelId = Number ( pickChannelIdFromList ( channels , decision ));
const targetCategory = decision . category || v . category || 8 ;
if (
( targetChannelId && targetChannelId !== Number ( v . channelId ) && Number ( v . channelId ) === 1 ) ||
( ! v . category && targetCategory )
) {
await dbQuery (
`UPDATE "video"
SET "channelId" = $1,
category = COALESCE(category, $2)
WHERE id = $3` ,
[ targetChannelId || v . channelId , targetCategory , v . id ]
);
}
} catch ( e ) {}
}
2026-10-03 10:45:58 +03:00
// Helper to check if request is from an authenticated PeerTube user
async function isRequestAuthenticated ( req , res , bodyAuthFlag ) {
if ( bodyAuthFlag === true ) return true ;
try {
if ( peertubeHelpers && peertubeHelpers . user && typeof peertubeHelpers . user . getAuthUser === 'function' ) {
const u = await peertubeHelpers . user . getAuthUser ( res );
if ( u ) return true ;
}
} catch ( e ) {}
const authHeader = req . headers && req . headers . authorization ;
if ( authHeader && authHeader . startsWith ( 'Bearer ' )) {
const token = authHeader . substring ( 7 ). trim ();
if ( token ) {
try {
const rows = await dbQuery (
`SELECT id FROM "oAuthToken" WHERE "accessToken" = $1 AND "accessTokenExpiresAt" > NOW() LIMIT 1` ,
[ token ]
);
if ( rows . length > 0 ) return true ;
} catch ( e ) {}
}
}
return false ;
}
2026-10-19 17:39:24 +03:00
function normalizeTitleForMatch ( str ) {
if ( ! str ) return '' ;
return String ( str )
. replace ( /&/g , '&' )
. replace ( /'/g , "'" )
. replace ( /"/g , '"' )
. replace ( /[\u2010-\u2015]/g , '-' )
. replace ( /\s*[\(\[](official|video|music|audio|clip|4k|hd|lyrics|sözleri|visualizer)[^\)\]]*[\)\]]/gi , '' )
. replace ( /[^a-zA-Z0-9\u00C0-\u024F\u1E00-\u1EFF]/g , ' ' )
. replace ( /\s+/g , ' ' )
. trim ()
. toLocaleLowerCase ( 'tr' );
}
2026-10-03 10:45:58 +03:00
// Helper to enrich YouTube results with existing local PeerTube videos via DB
async function enrichResultsWithLocalStatus ( results ) {
try {
const imports = await dbQuery (
`SELECT vi."targetUrl", v.id, v.uuid, v.name
FROM "videoImport" vi
INNER JOIN "video" v ON v.id = vi."videoId"
WHERE vi."targetUrl" IS NOT NULL
2026-11-04 04:48:03 +03:00
AND v.state = 1
AND (vi.state IS NULL OR vi.state != 3)
2026-10-18 23:36:34 +03:00
ORDER BY vi.id DESC`
2026-10-03 10:45:58 +03:00
);
const localVideos = await dbQuery (
`SELECT id, uuid, name
FROM "video"
WHERE remote = false AND state = 1
2026-10-18 23:36:34 +03:00
ORDER BY id DESC`
2026-10-03 10:45:58 +03:00
);
return results . map ( item => {
if ( item . isLive ) {
return { ... item , isDownloaded : false };
}
const matchedImport = imports . find ( imp => imp . targetUrl && item . id && imp . targetUrl . includes ( item . id ));
if ( matchedImport ) {
return {
... item ,
isDownloaded : true ,
localVideo : {
id : matchedImport . id ,
uuid : matchedImport . uuid ,
name : matchedImport . name ,
url : '/videos/watch/' + matchedImport . uuid
}
};
}
2026-10-19 17:39:24 +03:00
const rawItemTitle = ( item . title || '' ). toLowerCase (). trim ();
const normItemTitle = normalizeTitleForMatch ( item . title );
2026-10-03 10:45:58 +03:00
const matchedLocal = localVideos . find ( lv => {
2026-10-19 17:39:24 +03:00
const rawLv = ( lv . name || '' ). toLowerCase (). trim ();
if ( rawLv === rawItemTitle ) return true ;
const normLv = normalizeTitleForMatch ( lv . name );
return (
normLv &&
( normLv === normItemTitle ||
( normLv . length > 5 && ( normItemTitle . includes ( normLv ) || normLv . includes ( normItemTitle ))))
);
2026-10-03 10:45:58 +03:00
});
if ( matchedLocal ) {
return {
... item ,
isDownloaded : true ,
localVideo : {
id : matchedLocal . id ,
uuid : matchedLocal . uuid ,
name : matchedLocal . name ,
url : '/videos/watch/' + matchedLocal . uuid
}
};
}
return { ... item , isDownloaded : false };
});
} catch ( e ) {
return results ;
}
}
2026-10-03 11:14:03 +03:00
// Universal Disk Storage & Auto-Pruner (Works on ANY PeerTube server without bridge.js)
const MIN_FREE_GB = 25 ;
const MIN_VIDEO_AGE_MS = 7 * 24 * 60 * 60 * 1000 ;
function getStorageCheckPath () {
try {
if (
peertubeHelpers &&
peertubeHelpers . plugin &&
typeof peertubeHelpers . plugin . getDataDirectoryPath === 'function'
) {
let dir = peertubeHelpers . plugin . getDataDirectoryPath ();
while ( dir && dir !== path . dirname ( dir )) {
if ( fs . existsSync ( dir )) return dir ;
dir = path . dirname ( dir );
}
}
} catch ( e ) {}
if ( process . platform === 'win32' && fs . existsSync ( 'D:\\yaktube_storage' )) {
return 'D:\\' ;
}
return process . cwd ();
}
async function getStorageStats () {
await loadPluginSettings ();
const checkPath = getStorageCheckPath ();
return new Promise (( resolve , reject ) => {
fs . statfs ( checkPath , ( err , stats ) => {
if ( err ) return reject ( err );
const totalBytes = stats . blocks * stats . bsize ;
const freeBytes = stats . bfree * stats . bsize ;
const usedBytes = totalBytes - freeBytes ;
const totalGB = totalBytes / ( 1024 * 1024 * 1024 );
const freeGB = freeBytes / ( 1024 * 1024 * 1024 );
const usedGB = usedBytes / ( 1024 * 1024 * 1024 );
const usedPercent = totalBytes > 0 ? ( usedBytes / totalBytes ) * 100 : 0 ;
const isLowSpace = maxDiskUsagePercent > 0 && ( usedPercent >= maxDiskUsagePercent || freeGB < MIN_FREE_GB );
resolve ({
totalBytes ,
freeBytes ,
usedBytes ,
totalGB : Number ( totalGB . toFixed ( 2 )),
freeGB : Number ( freeGB . toFixed ( 2 )),
usedGB : Number ( usedGB . toFixed ( 2 )),
usedPercent : Number ( usedPercent . toFixed ( 1 )),
maxDiskUsagePercent ,
isLowSpace
});
});
});
}
async function checkAndPruneStorage () {
try {
let stats = await getStorageStats ();
if ( maxDiskUsagePercent <= 0 ) {
return { pruned : 0 , freedGB : 0 , status : 'Auto-pruning disabled (max-disk-usage-percent = 0)' };
}
if ( ! stats . isLowSpace ) {
return {
pruned : 0 ,
freedGB : 0 ,
status : `Disk space healthy ( ${ stats . freeGB } GB free, ${ stats . usedPercent } % used, limit: ${ maxDiskUsagePercent } %)`
};
}
const targetPercent = Math . max ( 10 , maxDiskUsagePercent - 10 );
if ( logger ) {
logger . info (
`[YakTube Auto-Pruner] Low disk space detected! Used: ${ stats . usedPercent } % (Limit: ${ maxDiskUsagePercent } %), Free: ${ stats . freeGB } GB. Starting cleanup...`
);
}
const token = await getAdminToken ();
const { hostname , port , hostHeader } = getLocalServerTarget ();
const vids = await dbQuery (
`SELECT id, uuid, name, views, "createdAt"
FROM "video"
WHERE remote = false
ORDER BY views ASC, "createdAt" ASC
LIMIT 100`
);
const now = Date . now ();
let prunedCount = 0 ;
for ( const vid of vids ) {
const createdAt = new Date ( vid . createdAt ). getTime ();
if ( now - createdAt < MIN_VIDEO_AGE_MS ) {
continue ;
}
if ( logger ) {
logger . info ( `[YakTube Auto-Pruner] Pruning unused video: " ${ vid . name } " (ID: ${ vid . id } , Views: ${ vid . views } )` );
}
await new Promise ( resolve => {
const delReq = http . request (
{
hostname ,
port ,
path : '/api/v1/videos/' + vid . id ,
method : 'DELETE' ,
headers : {
Host : hostHeader ,
Authorization : 'Bearer ' + token
}
},
delRes => {
delRes . on ( 'data' , () => {});
delRes . on ( 'end' , resolve );
}
);
delReq . on ( 'error' , resolve );
delReq . end ();
});
prunedCount ++ ;
stats = await getStorageStats ();
if ( stats . usedPercent <= targetPercent && stats . freeGB >= MIN_FREE_GB ) {
if ( logger ) {
logger . info (
`[YakTube Auto-Pruner] Target disk space reached ( ${ stats . usedPercent } % used). Pruning completed.`
);
}
break ;
}
}
return { pruned : prunedCount , status : 'Pruning completed' , currentStats : stats };
} catch ( err ) {
if ( logger ) logger . error ( '[YakTube Auto-Pruner] Error during prune check: ' + err . message );
return { error : err . message };
}
}
if ( pruneIntervalTimer ) clearInterval ( pruneIntervalTimer );
pruneIntervalTimer = setInterval (
() => {
checkAndPruneStorage (). catch (() => {});
},
4 * 60 * 60 * 1000
);
2026-10-18 11:26:23 +03:00
// 1. Quota, Categories & Storage Config Routes
2026-10-03 10:45:58 +03:00
router . get ( '/quota-config' , async ( _req , res ) => {
await loadPluginSettings ();
2026-10-18 11:26:23 +03:00
return res . json ({
ok : true ,
2026-11-03 18:06:54 +03:00
yaknetSsoEnabled ,
2026-11-03 18:51:10 +03:00
smartPrefetchMode ,
2026-11-03 19:01:59 +03:00
federatedRecommendationsMode ,
2026-10-18 11:26:23 +03:00
guestImportLimit ,
maxDiskUsagePercent ,
2026-10-23 08:59:50 +03:00
safeSearchEnabled ,
blockedKeywords : blockedKeywordsList ,
2026-10-18 11:26:23 +03:00
categories : parsedHomeCategories
});
});
router . get ( '/categories-config' , async ( _req , res ) => {
await loadPluginSettings ();
return res . json ({
ok : true ,
raw : rawHomeCategories ,
categories : parsedHomeCategories
});
2026-10-03 11:14:03 +03:00
});
router . get ( '/storage-status' , async ( _req , res ) => {
try {
const stats = await getStorageStats ();
return res . json ({
ok : true ,
stats ,
policy : {
maxUsagePercent : maxDiskUsagePercent ,
targetUsagePercent : maxDiskUsagePercent > 0 ? Math . max ( 10 , maxDiskUsagePercent - 10 ) : 0 ,
minFreeGB : MIN_FREE_GB ,
gracePeriodDays : 7
}
});
} catch ( e ) {
return res . status ( 500 ). json ({ error : e . message });
}
});
router . post ( '/prune-check' , async ( _req , res ) => {
const result = await checkAndPruneStorage ();
return res . json ( result );
2026-10-03 10:26:46 +03:00
});
2026-11-19 20:30:22 +03:00
const inFlightYouTubeSearches = new Map ();
// Reusable YouTube Raw Search Helper (with Cache, In-Flight Dedupe & SafeSearch)
2026-11-03 18:51:10 +03:00
function searchYouTubeRaw ( query , maxCount = 24 ) {
2026-11-19 20:30:22 +03:00
const cleanQ = ( query || '' ). trim ();
if ( ! cleanQ || cleanQ . length < 2 ) return Promise . resolve ({ results : [], cached : false });
if ( safeSearchEnabled && isContentBlocked ( cleanQ , blockedKeywordsList )) {
return Promise . resolve ({ results : [], cached : false , blocked : true });
}
2026-11-03 18:51:10 +03:00
2026-11-19 20:30:22 +03:00
const cacheKey = ` ${ cleanQ . toLocaleLowerCase ( 'tr' ) } __ ${ maxCount } ` ;
const cached = getCached ( cacheKey ) || ( maxCount <= 24 ? getCached ( cleanQ ) : null );
if ( cached ) {
const filteredCached = safeSearchEnabled
? cached . filter ( e => ! isContentBlocked ( e . title , blockedKeywordsList ))
: cached ;
return Promise . resolve ({ results : filteredCached . slice ( 0 , maxCount ), cached : true });
}
2026-11-03 18:51:10 +03:00
2026-11-19 20:30:22 +03:00
if ( inFlightYouTubeSearches . has ( cacheKey )) {
return inFlightYouTubeSearches . get ( cacheKey );
}
const p = new Promise (( resolve , reject ) => {
2026-11-05 18:02:25 +03:00
const ytdlpPath = resolveYtDlpPath ( logger , customYtDlpPath , peertubeHelpers );
2026-11-03 18:51:10 +03:00
const args = [
`ytsearch ${ maxCount } : ${ cleanQ } ` ,
'--dump-single-json' ,
'--flat-playlist' ,
'--skip-download' ,
'--no-warnings'
];
2026-11-19 20:30:22 +03:00
execFile ( ytdlpPath , args , { maxBuffer : 10 * 1024 * 1024 , timeout : 30000 }, ( error , stdout , stderr ) => {
2026-11-03 18:51:10 +03:00
if ( error ) {
2026-11-19 20:30:22 +03:00
if ( logger ) {
logger . warn (
`[YakTube Search] yt-dlp search error for " ${ cleanQ } ": ${ error . message } ${ stderr || '' } ` . trim ()
);
}
2026-11-03 18:51:10 +03:00
return reject ( error );
}
try {
const rawStr = stdout . trim ();
const startIdx = rawStr . indexOf ( '{' );
const endIdx = rawStr . lastIndexOf ( '}' );
if ( startIdx === - 1 || endIdx === - 1 ) {
throw new Error ( 'No JSON object found in output' );
}
const json = JSON . parse ( rawStr . substring ( startIdx , endIdx + 1 ));
const entries = json . entries || [];
let rawResults = entries
. filter ( e => e && e . id )
. map ( e => {
const duration = e . duration || 0 ;
const mins = Math . floor ( duration / 60 );
const secs = Math . floor ( duration % 60 );
const isLive = e . is_live === true || e . live_status === 'is_live' || ( ! e . duration && e . was_live !== true );
const durationFormatted = isLive
? 'CANLI'
: ( mins < 10 ? '0' + mins : mins ) + ':' + ( secs < 10 ? '0' + secs : secs );
let thumbnail =
e . thumbnail || ( e . thumbnails && e . thumbnails . length ? e . thumbnails [ e . thumbnails . length - 1 ]. url : '' );
if ( ! thumbnail && e . id ) {
thumbnail = `https://i.ytimg.com/vi/ ${ e . id } /hqdefault.jpg` ;
}
return {
id : e . id ,
title : e . title ,
channel : e . uploader || e . channel || 'YouTube' ,
duration ,
durationFormatted ,
isLive ,
url : e . url || `https://www.youtube.com/watch?v= ${ e . id } ` ,
thumbnail
};
});
if ( safeSearchEnabled ) {
rawResults = rawResults . filter ( e => ! isContentBlocked ( e . title , blockedKeywordsList ));
}
setCache ( cacheKey , rawResults );
if ( maxCount >= 18 ) setCache ( cleanQ , rawResults );
return resolve ({ results : rawResults , cached : false });
} catch ( parseErr ) {
return reject ( parseErr );
}
});
2026-11-19 20:30:22 +03:00
}). finally (() => {
inFlightYouTubeSearches . delete ( cacheKey );
2026-11-03 18:51:10 +03:00
});
2026-11-19 20:30:22 +03:00
inFlightYouTubeSearches . set ( cacheKey , p );
return p ;
2026-11-03 18:51:10 +03:00
}
2026-11-04 04:48:03 +03:00
function isCrawlerBotRequest ( req ) {
const ua = String (( req && req . headers && req . headers [ 'user-agent' ]) || '' ). toLowerCase ();
if ( ! ua ) return false ;
return /applebot|reflectionbot|googlebot|bingbot|yandexbot|baiduspider|duckduckbot|slurp|ahrefsbot|semrushbot|mj12bot|dotbot|petalbot|bytespider|facebookexternalhit|twitterbot|discordbot|telegrambot|whatsapp|linkedinbot|headlesschrome/i . test (
ua
);
}
function extractYouTubeIdFromUrl ( urlStr ) {
if ( ! urlStr || typeof urlStr !== 'string' ) return null ;
const m =
urlStr . match ( /[?&]v=([a-zA-Z0-9_-]{11})/ ) ||
urlStr . match ( /youtu\.be\/([a-zA-Z0-9_-]{11})/ ) ||
urlStr . match ( /shorts\/([a-zA-Z0-9_-]{11})/ ) ||
urlStr . match ( /embed\/([a-zA-Z0-9_-]{11})/ );
return m ? m [ 1 ] : null ;
}
function translateImportErrorMessage ( rawErrStr ) {
const s = String ( rawErrStr || '' ). toLowerCase ();
if (
s . includes ( 'confirm your age' ) ||
s . includes ( 'age-restricted' ) ||
s . includes ( 'age_restriction' ) ||
s . includes ( 'inappropriate for some users' )
) {
return 'Bu video YouTube tarafı ndan +18 yaş kı sı tlamalı (oturum açma zorunlu) olarak işaretlendiği için sunucuya aktarı lamamaktadı r.' ;
}
if ( s . includes ( 'live streaming' ) || s . includes ( 'is_live' ) || s . includes ( 'live event' )) {
return 'Canlı yayı nlar video dosyası olarak indirilemez. Lütfen Canlı Yayı nı İzle modunu kullanı n.' ;
}
if (
s . includes ( 'private video' ) ||
s . includes ( 'video unavailable' ) ||
s . includes ( 'has been removed' ) ||
s . includes ( 'not available in your country' )
) {
return "Bu video YouTube'da gizli, silinmiş veya erişime kapalı durumda olduğu için aktarı lamadı ." ;
}
if ( s . includes ( 'confirm you’ re not a bot' ) || s . includes ( 'confirm you are not a bot' ) || s . includes ( ' 429 ' )) {
return 'YouTube geçici yoğunluk sı nı rı nedeniyle bağlantı yı kı sı tladı . Lütfen birkaç saniye sonra tekrar deneyin.' ;
}
return "Bu video şu anda YouTube'dan aktarı lamadı . Lütfen tekrar deneyin veya başka bir video seçin." ;
}
const inFlightImportPromises = new Map ();
2026-10-03 10:45:58 +03:00
// 2. YouTube Search Route (aliases: /youtube-search and /search)
async function handleYouTubeSearch ( req , res ) {
await loadPluginSettings ();
2026-11-04 04:48:03 +03:00
if ( isCrawlerBotRequest ( req )) {
return res . json ({ results : [], cached : true , botIgnored : true , guestImportLimit });
}
2026-08-29 12:42:36 +03:00
const query = ( req . query . q || '' ). trim ();
if ( ! query || query . length < 2 ) {
2026-10-03 10:26:46 +03:00
return res . json ({ results : [], guestImportLimit });
2026-08-29 12:42:36 +03:00
}
2026-10-23 08:59:50 +03:00
if ( safeSearchEnabled && isContentBlocked ( query , blockedKeywordsList )) {
return res . json ({
results : [],
blocked : true ,
guestImportLimit ,
message :
'Bu arama terimi veya içerik, Çocuk ve Aile Güvenliği (Child & Family Safety) politikaları gereğince filtrelenmiştir.'
});
}
2026-11-03 18:51:10 +03:00
try {
const { results : rawResults , cached } = await searchYouTubeRaw ( query , 24 );
const enrichedResults = await enrichResultsWithLocalStatus ( rawResults );
return res . json ({ results : enrichedResults , cached , guestImportLimit });
} catch ( err ) {
if ( logger ) logger . error ( 'YouTube search error: ' + err . message );
2026-11-19 20:30:22 +03:00
return res . json ({ results : [], searchError : true , errorDetails : err . message , guestImportLimit });
2026-08-29 12:42:36 +03:00
}
2026-10-03 10:45:58 +03:00
}
2026-08-29 12:42:36 +03:00
2026-10-03 10:45:58 +03:00
router . get ( '/youtube-search' , handleYouTubeSearch );
router . get ( '/search' , handleYouTubeSearch );
2026-11-03 18:51:10 +03:00
// 2.5. YouTube-Style Smart Recommendation & Personalized Up-Next Engine
const RECOMMENDATION_STOP_WORDS = new Set ([
'official' ,
'video' ,
'klip' ,
'music' ,
'audio' ,
'lyrics' ,
'lyric' ,
'sözleri' ,
'sozleri' ,
'şarkı ' ,
'sarki' ,
'hd' ,
'4k' ,
'1080p' ,
'full' ,
'izle' ,
'yeni' ,
'bölüm' ,
'bolum' ,
've' ,
'ile' ,
'için' ,
'icin' ,
'bir' ,
'bu' ,
'da' ,
'de' ,
'mi' ,
'mu' ,
'the' ,
'and' ,
'for' ,
'with' ,
'from' ,
'feat' ,
'ft' ,
'prod' ,
'remix' ,
'version' ,
'canlı ' ,
'canli' ,
'performans' ,
'konser' ,
'yaktube' ,
'youtube'
]);
function extractRecommendationKeywords ( text ) {
const norm = normalizeTitleForMatch ( text );
if ( ! norm ) return [];
return norm
. split ( /\s+/ )
. map ( w => w . replace ( /i\u0307/g , 'i' ))
. filter ( w => w . length >= 3 && ! RECOMMENDATION_STOP_WORDS . has ( w ) && ! /^\d+$/ . test ( w ));
}
function formatDurationLabel ( seconds ) {
const dur = Math . max ( 0 , Math . floor ( Number ( seconds ) || 0 ));
const mins = Math . floor ( dur / 60 );
const secs = dur % 60 ;
return ( mins < 10 ? '0' + mins : String ( mins )) + ':' + ( secs < 10 ? '0' + secs : String ( secs ));
}
2026-11-04 05:28:50 +03:00
const GENERIC_CHANNEL_AND_TAG_NAMES = new Set ([
'yaktube' ,
'root' ,
'admin' ,
'youtube' ,
'varsayilan' ,
'varsayı lan' ,
'muzik kanali' ,
'müzik kanalı ' ,
'muzik_kanali' ,
'genel medya' ,
'yaktube genel medya' ,
'yaktube muzik' ,
'yaktube müzik' ,
'video' ,
'shorts' ,
'viral' ,
'yeni' ,
'turkce' ,
'türkçe' ,
'muzik' ,
'müzik' ,
'musik' ,
'muziek' ,
'music' ,
'film' ,
'dizi' ,
'haber' ,
'komik' ,
'eglence' ,
'eğlence' ,
'bildirimler' ,
'notifications'
]);
function normalizeCoreVideoTitle ( text ) {
if ( ! text ) return '' ;
return normalizeTitleForMatch (
String ( text )
. replace ( /[\(\[].*?[\)\]]/g , ' ' )
. replace (
/\b(official\s*music\s*video|official\s*video|official\s*audio|video\s*klip|klip|hd|4k|1080p|lyric\s*video|lyrics|audio|şarkı \s*sözleri|sarki\s*sozleri|full\s*izle|yaktube)\b/gi ,
' '
)
);
}
function isGenericChannelOrTag ( name ) {
const norm = normalizeTitleForMatch ( name );
if ( ! norm || norm . length < 3 ) return true ;
if ( GENERIC_CHANNEL_AND_TAG_NAMES . has ( norm )) return true ;
if ( /^yaktube\b/i . test ( norm ) || /\bkanali$/i . test ( norm ) || /\bkanalı $/i . test ( norm )) return true ;
return false ;
}
2026-11-17 23:15:56 +03:00
let localLibrarySnapshotCache = { time : 0 , rows : [] };
const LOCAL_LIBRARY_CACHE_TTL_MS = 30 * 1000 ;
async function getCachedLocalLibraryRows () {
if (
localLibrarySnapshotCache . rows . length > 0 &&
Date . now () - localLibrarySnapshotCache . time < LOCAL_LIBRARY_CACHE_TTL_MS
) {
return localLibrarySnapshotCache . rows ;
}
const rows = await dbQuery (
`SELECT v.id, v.uuid, v.name, v.category, v.duration, v.views, v."createdAt",
(SELECT t.filename FROM "thumbnail" t WHERE t."videoId" = v.id ORDER BY t.width ASC NULLS LAST LIMIT 1) AS "thumbFilename",
vi."targetUrl" AS "importUrl",
(
SELECT tg.name
FROM "videoTag" vt2
INNER JOIN "tag" tg ON tg.id = vt2."tagId"
WHERE vt2."videoId" = v.id
ORDER BY vt2."createdAt" ASC
LIMIT 1
) AS "primaryTag"
FROM "video" v
LEFT JOIN "videoImport" vi ON vi."videoId" = v.id
WHERE v.remote = false AND v.state = 1
ORDER BY v.id DESC
LIMIT 250`
);
localLibrarySnapshotCache = { time : Date . now (), rows };
return rows ;
}
2026-11-03 18:51:10 +03:00
router . get ( '/smart-recommendations' , async ( req , res ) => {
await loadPluginSettings ();
2026-11-04 04:48:03 +03:00
if ( isCrawlerBotRequest ( req )) {
return res . json ({
ok : true ,
results : [],
hasLocalMatch : false ,
botIgnored : true ,
smartPrefetchMode ,
guestImportLimit
});
}
2026-11-03 18:51:10 +03:00
const rawUuid = ( req . query . uuid || '' ). trim ();
const rawTitle = ( req . query . title || '' ). trim ();
const rawChannel = ( req . query . channel || '' ). trim ();
2026-11-04 05:28:50 +03:00
const rawYtChannel = ( req . query . ytChannel || '' ). trim ();
2026-11-03 18:51:10 +03:00
const rawHistory = ( req . query . history || '' ). trim ();
2026-11-04 05:28:50 +03:00
const rawWatched = ( req . query . watched || '' ). trim ();
2026-11-03 18:51:10 +03:00
2026-11-04 05:28:50 +03:00
const cleanRawTitle = /^(bildirimler|notifications|yaktube|peertube|yükleniyor|loading)$/i . test ( rawTitle )
? ''
: rawTitle ;
if ( ! cleanRawTitle && ! rawUuid ) {
2026-11-03 18:51:10 +03:00
return res . json ({
ok : true ,
results : [],
hasLocalMatch : false ,
smartPrefetchMode ,
guestImportLimit
});
}
const resolvedUuid = rawUuid ? shortToUuid ( rawUuid ) : '' ;
let currentVideoRow = null ;
2026-11-04 05:28:50 +03:00
let currentVideoTags = [];
2026-11-03 18:51:10 +03:00
if ( resolvedUuid ) {
try {
2026-11-17 23:15:56 +03:00
let currentRows = [];
if ( /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i . test ( resolvedUuid )) {
currentRows = await dbQuery (
`SELECT v.id, v.uuid, v.name, v.category, v."channelId", vi."targetUrl"
FROM "video" v
LEFT JOIN "videoImport" vi ON vi."videoId" = v.id
WHERE v.uuid = $1::uuid
LIMIT 1` ,
[ resolvedUuid ]
);
} else if ( /^\d+$/ . test ( rawUuid )) {
currentRows = await dbQuery (
`SELECT v.id, v.uuid, v.name, v.category, v."channelId", vi."targetUrl"
FROM "video" v
LEFT JOIN "videoImport" vi ON vi."videoId" = v.id
WHERE v.id = $1::int
LIMIT 1` ,
[ parseInt ( rawUuid , 10 )]
);
}
2026-11-03 18:51:10 +03:00
if ( currentRows . length > 0 ) {
currentVideoRow = currentRows [ 0 ];
2026-11-04 05:28:50 +03:00
const tagRows = await dbQuery (
`SELECT t.name
FROM "videoTag" vt
INNER JOIN "tag" t ON t.id = vt."tagId"
WHERE vt."videoId" = $1
2026-11-17 23:15:56 +03:00
ORDER BY vt."createdAt" ASC
2026-11-04 05:28:50 +03:00
LIMIT 8` ,
[ currentVideoRow . id ]
);
currentVideoTags = tagRows . map ( r => String ( r . name || '' ). trim ()). filter ( Boolean );
2026-11-03 18:51:10 +03:00
}
} catch ( e ) {}
}
2026-11-04 05:28:50 +03:00
const effectiveTitle = cleanRawTitle || ( currentVideoRow && currentVideoRow . name ) || '' ;
if ( ! effectiveTitle ) {
return res . json ({
ok : true ,
results : [],
hasLocalMatch : false ,
smartPrefetchMode ,
guestImportLimit
});
}
2026-11-03 18:51:10 +03:00
const normCurrentTitle = normalizeTitleForMatch ( effectiveTitle );
2026-11-04 05:28:50 +03:00
const coreCurrentTitle = normalizeCoreVideoTitle ( effectiveTitle );
2026-11-03 18:51:10 +03:00
const currentCategory = currentVideoRow && currentVideoRow . category ? Number ( currentVideoRow . category ) : null ;
let currentYtId = null ;
if ( currentVideoRow && currentVideoRow . targetUrl ) {
const m =
currentVideoRow . targetUrl . match ( /[?&]v=([^&]+)/ ) ||
currentVideoRow . targetUrl . match ( /youtu\.be\/([^?]+)/ ) ||
currentVideoRow . targetUrl . match ( /shorts\/([^?]+)/ );
if ( m ) currentYtId = m [ 1 ];
}
2026-11-04 05:28:50 +03:00
// Build strict exclusion sets for the current video AND recently watched videos (prevents A <-> B autoplay loops)
const excludedUuids = new Set ();
const excludedYtIds = new Set ();
const excludedNormTitles = new Set ();
const excludedCoreTitles = new Set ();
if ( resolvedUuid ) excludedUuids . add ( resolvedUuid . toLowerCase ());
if ( rawUuid ) excludedUuids . add ( rawUuid . toLowerCase ());
if ( currentYtId ) excludedYtIds . add ( currentYtId );
if ( normCurrentTitle ) excludedNormTitles . add ( normCurrentTitle );
if ( coreCurrentTitle && coreCurrentTitle . length >= 4 ) excludedCoreTitles . add ( coreCurrentTitle );
if ( rawWatched ) {
const watchedParts = rawWatched
. split ( '|' )
. map ( s => String ( s || '' ). trim ())
. filter ( Boolean );
for ( const wp of watchedParts ) {
if ( /^[0-9a-f]{8}-[0-9a-f]{4}-/i . test ( wp )) {
excludedUuids . add ( wp . toLowerCase ());
} else if ( /^[a-zA-Z0-9_-]{22}$/ . test ( wp )) {
const fullU = shortToUuid ( wp );
excludedUuids . add ( wp . toLowerCase ());
if ( fullU ) excludedUuids . add ( fullU . toLowerCase ());
} else if ( /^[a-zA-Z0-9_-]{11}$/ . test ( wp ) && ! /\s/ . test ( wp )) {
excludedYtIds . add ( wp );
} else {
const nTitle = normalizeTitleForMatch ( wp );
const cTitle = normalizeCoreVideoTitle ( wp );
if ( nTitle && nTitle . length >= 4 ) excludedNormTitles . add ( nTitle );
if ( cTitle && cTitle . length >= 4 ) excludedCoreTitles . add ( cTitle );
}
}
}
function isCurrentVideoMatch ( uuid , ytId , title ) {
if ( uuid && ( uuid . toLowerCase () === resolvedUuid . toLowerCase () || uuid . toLowerCase () === rawUuid . toLowerCase ())) {
return true ;
}
if ( currentYtId && ytId && ytId === currentYtId ) return true ;
const nT = normalizeTitleForMatch ( title );
const cT = normalizeCoreVideoTitle ( title );
if ( nT && nT === normCurrentTitle ) return true ;
if ( cT && coreCurrentTitle && cT . length >= 5 && cT === coreCurrentTitle ) return true ;
return false ;
}
function isAlreadyWatchedVideo ( uuid , ytId , title ) {
if ( uuid && excludedUuids . has ( String ( uuid ). toLowerCase ())) return true ;
if ( ytId && excludedYtIds . has ( String ( ytId ))) return true ;
const nT = normalizeTitleForMatch ( title );
const cT = normalizeCoreVideoTitle ( title );
if ( nT && excludedNormTitles . has ( nT )) return true ;
if ( cT && cT . length >= 5 && excludedCoreTitles . has ( cT )) return true ;
return false ;
}
// Identify Creator / YouTube Channel / Artist from multiple signals
2026-11-03 18:51:10 +03:00
let artistPart = '' ;
const dashParts = effectiveTitle . split ( /\s+[\-\u2010-\u2015]\s+/ );
2026-11-04 05:28:50 +03:00
const pipeParts = effectiveTitle . split ( /\s+\|\s+/ );
const handleMatch = effectiveTitle . match ( /@([a-zA-Z0-9_.\u00C0-\u017F]{3,30})/ );
if ( dashParts . length >= 2 && dashParts [ 0 ]. trim (). length >= 3 && dashParts [ 0 ]. trim (). length <= 42 ) {
2026-11-03 18:51:10 +03:00
artistPart = dashParts [ 0 ]
. replace ( /[\(\[].*?[\)\]]/g , '' )
. replace ( /YakTube/gi , '' )
. trim ();
} else if (
2026-11-04 05:28:50 +03:00
pipeParts . length >= 2 &&
pipeParts [ pipeParts . length - 1 ]. trim (). length >= 3 &&
pipeParts [ pipeParts . length - 1 ]. trim (). length <= 40
2026-11-03 18:51:10 +03:00
) {
2026-11-04 05:28:50 +03:00
artistPart = pipeParts [ pipeParts . length - 1 ]
. replace ( /[\(\[].*?[\)\]]/g , '' )
. replace ( /^@/ , '' )
. trim ();
} else if ( handleMatch && handleMatch [ 1 ]) {
artistPart = handleMatch [ 1 ]. trim ();
2026-11-03 18:51:10 +03:00
}
2026-11-04 05:28:50 +03:00
let channelCreatorName = '' ;
if ( rawYtChannel && ! isGenericChannelOrTag ( rawYtChannel )) {
channelCreatorName = rawYtChannel . trim ();
} else {
const firstMeaningfulTag = currentVideoTags . find ( t => ! isGenericChannelOrTag ( t ));
if ( firstMeaningfulTag ) {
channelCreatorName = firstMeaningfulTag ;
} else if ( rawChannel && ! isGenericChannelOrTag ( rawChannel )) {
channelCreatorName = rawChannel . trim ();
}
}
if ( ! artistPart && channelCreatorName ) {
artistPart = channelCreatorName ;
}
2026-11-03 18:51:10 +03:00
const artistNorm = normalizeTitleForMatch ( artistPart );
2026-11-04 05:28:50 +03:00
const channelCreatorNorm = normalizeTitleForMatch ( channelCreatorName );
const currentTagNorms = currentVideoTags
. filter ( t => ! isGenericChannelOrTag ( t ))
. map ( t => normalizeTitleForMatch ( t ))
. filter ( t => t . length >= 3 );
const currentKeywords = Array . from (
new Set ([
... extractRecommendationKeywords ( effectiveTitle ),
... currentVideoTags . flatMap ( t => extractRecommendationKeywords ( t ))
])
);
2026-11-03 18:51:10 +03:00
// Build clean primary search query for YouTube
const cleanTopic = effectiveTitle
. replace ( /YakTube/gi , '' )
. replace ( /[\(\[].*?[\)\]]/g , '' )
. replace ( /official\s*video|video\s*klip|klip|hd|4k|lyric\s*video|audio|şarkı \s*sözleri|sarki\s*sozleri/gi , '' )
. replace ( /\s+/g , ' ' )
. trim ();
const topicWords = cleanTopic . split ( /\s+/ ). filter ( w => w . length >= 2 );
2026-11-04 05:28:50 +03:00
let primaryQuery = topicWords . slice ( 0 , 5 ). join ( ' ' ) || cleanTopic || effectiveTitle ;
if (
channelCreatorName &&
topicWords . length <= 3 &&
! normalizeTitleForMatch ( primaryQuery ). includes ( channelCreatorNorm )
) {
primaryQuery = ` ${ primaryQuery } ${ channelCreatorName } ` . trim ();
}
2026-11-03 18:51:10 +03:00
2026-11-04 05:28:50 +03:00
// Build dedicated Creator / Same-Channel query so other videos from the same YouTube channel are fetched
let creatorQuery = '' ;
const candidateCreators = [ channelCreatorName , artistPart ]. filter ( c => c && ! isGenericChannelOrTag ( c ));
for ( const cand of candidateCreators ) {
const normCand = normalizeTitleForMatch ( cand );
if ( normCand && normCand . length >= 3 && normCand !== normalizeTitleForMatch ( primaryQuery )) {
creatorQuery = cand ;
break ;
}
}
if ( ! creatorQuery && candidateCreators . length > 0 ) {
creatorQuery = candidateCreators [ 0 ];
}
// Parse user watch & search history terms (filtering out UI labels and placeholders)
2026-11-03 18:51:10 +03:00
let historyTerms = [];
if ( rawHistory ) {
try {
const parsed = rawHistory . startsWith ( '[' ) ? JSON . parse ( rawHistory ) : rawHistory . split ( '|' );
if ( Array . isArray ( parsed )) {
2026-11-04 05:28:50 +03:00
const defaultIgnore = new Set ([
'tarkan' ,
'barış manço' ,
'baris manco' ,
'yapay zeka' ,
'canlı haber' ,
'canli haber' ,
'bildirimler' ,
'notifications' ,
'yaktube' ,
'peertube'
]);
2026-11-03 18:51:10 +03:00
const cleaned = parsed
. map ( t => String ( t || '' ). trim ())
2026-11-04 05:28:50 +03:00
. filter (
t =>
t . length >= 3 &&
! /^(bildirimler|notifications|yaktube|peertube|anasayfa)$/i . test ( t ) &&
( ! safeSearchEnabled || ! isContentBlocked ( t , blockedKeywordsList ))
);
2026-11-03 18:51:10 +03:00
const nonDefaults = cleaned . filter ( t => ! defaultIgnore . has ( t . toLocaleLowerCase ( 'tr' )));
2026-11-04 05:28:50 +03:00
historyTerms = ( nonDefaults . length > 0 ? nonDefaults : cleaned ). slice ( 0 , 10 );
2026-11-03 18:51:10 +03:00
}
} catch ( e ) {}
}
const historyKeywords = Array . from (
new Set ( historyTerms . flatMap ( t => extractRecommendationKeywords ( t )). filter ( w => w . length >= 3 ))
);
2026-11-04 05:28:50 +03:00
// Pick up to 2 distinct secondary queries from user watch/search history
const historyQueries = [];
2026-11-03 18:51:10 +03:00
const normPrimary = normalizeTitleForMatch ( primaryQuery );
2026-11-04 05:28:50 +03:00
const normCreator = normalizeTitleForMatch ( creatorQuery );
2026-11-03 18:51:10 +03:00
for ( const hTerm of historyTerms ) {
const normH = normalizeTitleForMatch ( hTerm );
if (
normH &&
normH . length >= 3 &&
normH !== normPrimary &&
2026-11-04 05:28:50 +03:00
normH !== normCreator &&
2026-11-03 18:51:10 +03:00
! normPrimary . includes ( normH ) &&
2026-11-04 05:28:50 +03:00
! normH . includes ( normPrimary ) &&
! historyQueries . some ( existing => normalizeTitleForMatch ( existing ) === normH )
2026-11-03 18:51:10 +03:00
) {
2026-11-04 05:28:50 +03:00
const cleanedHTerm = hTerm
2026-11-03 18:51:10 +03:00
. replace ( /[\(\[].*?[\)\]]/g , '' )
. split ( /\s+/ )
. slice ( 0 , 4 )
. join ( ' ' )
. trim ();
2026-11-04 05:28:50 +03:00
if ( cleanedHTerm . length >= 3 ) {
historyQueries . push ( cleanedHTerm );
if ( historyQueries . length >= 2 ) break ;
}
2026-11-03 18:51:10 +03:00
}
}
2026-11-04 05:28:50 +03:00
// 1. Score Local YakTube Videos from PostgreSQL (including their stored YouTube channel/tags)
2026-11-03 18:51:10 +03:00
const scoredLocalItems = [];
2026-11-04 05:28:50 +03:00
const watchedFallbackItems = [];
2026-11-03 18:51:10 +03:00
let hasDirectLocalMatch = false ;
try {
2026-11-17 23:15:56 +03:00
const localRows = await getCachedLocalLibraryRows ();
2026-11-03 18:51:10 +03:00
for ( const lv of localRows ) {
if ( ! lv || ! lv . name ) continue ;
if ( currentVideoRow && Number ( lv . id ) === Number ( currentVideoRow . id )) continue ;
if ( safeSearchEnabled && isContentBlocked ( lv . name , blockedKeywordsList )) continue ;
let lvYtId = null ;
if ( lv . importUrl ) {
const m =
lv . importUrl . match ( /[?&]v=([^&]+)/ ) ||
lv . importUrl . match ( /youtu\.be\/([^?]+)/ ) ||
lv . importUrl . match ( /shorts\/([^?]+)/ );
if ( m ) lvYtId = m [ 1 ];
}
2026-11-04 05:28:50 +03:00
// Never recommend the exact video currently playing
if ( isCurrentVideoMatch ( lv . uuid , lvYtId , lv . name )) continue ;
const normLv = normalizeTitleForMatch ( lv . name );
if ( ! normLv ) continue ;
const lvTagNorm = normalizeTitleForMatch ( lv . primaryTag || '' );
const lvChannelDisplay =
lv . primaryTag && ! isGenericChannelOrTag ( lv . primaryTag ) ? lv . primaryTag : 'YakTube Kütüphanesi' ;
2026-11-03 18:51:10 +03:00
let rawScore = 0 ;
let reasonBadge = '' ;
2026-11-04 05:28:50 +03:00
let sourceBucket = 'topic' ;
2026-11-03 18:51:10 +03:00
2026-11-04 05:28:50 +03:00
// Same YouTube channel / creator / artist match
const isSameArtist =
artistNorm && artistNorm . length >= 3 && ( normLv . includes ( artistNorm ) || lvTagNorm === artistNorm );
const isSameChannel =
( channelCreatorNorm &&
channelCreatorNorm . length >= 3 &&
( normLv . includes ( channelCreatorNorm ) || lvTagNorm === channelCreatorNorm )) ||
( lvTagNorm && currentTagNorms . includes ( lvTagNorm ));
if ( isSameChannel || isSameArtist ) {
rawScore += 75 ;
reasonBadge =
lv . primaryTag && ! isGenericChannelOrTag ( lv . primaryTag )
? `📺 Aynı Kanaldan • ${ lv . primaryTag } `
: '📺 Aynı Kanaldan / Sanatçı' ;
sourceBucket = 'channel' ;
2026-11-03 18:51:10 +03:00
}
// Keyword overlap with current video
const lvKeywords = extractRecommendationKeywords ( lv . name );
let kwOverlap = 0 ;
for ( const kw of currentKeywords ) {
if ( lvKeywords . includes ( kw ) || ( kw . length >= 4 && normLv . includes ( kw ))) {
kwOverlap ++ ;
}
}
if ( kwOverlap > 0 ) {
rawScore += kwOverlap * 28 ;
if ( ! reasonBadge ) reasonBadge = '🔥 Benzer İçerik' ;
}
// User watch/search history match
let histOverlap = 0 ;
for ( const hkw of historyKeywords ) {
2026-11-04 05:28:50 +03:00
if (
lvKeywords . includes ( hkw ) ||
( hkw . length >= 4 && normLv . includes ( hkw )) ||
( lvTagNorm && lvTagNorm . includes ( hkw ))
) {
2026-11-03 18:51:10 +03:00
histOverlap ++ ;
}
}
if ( histOverlap > 0 ) {
2026-11-04 05:28:50 +03:00
rawScore += Math . min ( 48 , histOverlap * 24 );
if ( ! reasonBadge ) {
reasonBadge = '🎯 İzleme Geçmişinize Göre' ;
sourceBucket = 'history' ;
}
2026-11-03 18:51:10 +03:00
}
// Same category match (only if category is known)
if ( currentCategory && lv . category && Number ( lv . category ) === currentCategory ) {
rawScore += 20 ;
if ( ! reasonBadge ) reasonBadge = '📂 Aynı Kategoriden' ;
}
// Strictly exclude completely unrelated videos (score < 20)
if ( rawScore < 20 ) continue ;
2026-11-04 05:28:50 +03:00
const alreadyWatched = isAlreadyWatchedVideo ( lv . uuid , lvYtId , lv . name );
if ( ! alreadyWatched && rawScore >= 40 ) {
2026-11-03 18:51:10 +03:00
hasDirectLocalMatch = true ;
}
const finalScore = rawScore >= 40 ? rawScore + 35 : 54 + Math . min ( 10 , Math . floor (( lv . views || 0 ) / 10 ));
const thumbUrl = lv . thumbFilename
? `/lazy-static/thumbnails/ ${ lv . thumbFilename } `
: lvYtId
? `https://i.ytimg.com/vi/ ${ lvYtId } /hqdefault.jpg`
: '/client/assets/images/default-thumbnail.jpg' ;
2026-11-04 05:28:50 +03:00
const candidateObj = {
2026-11-03 18:51:10 +03:00
id : lvYtId || `local- ${ lv . uuid } ` ,
title : lv . name ,
2026-11-04 05:28:50 +03:00
channel : lvChannelDisplay ,
2026-11-03 18:51:10 +03:00
duration : lv . duration || 0 ,
durationFormatted : formatDurationLabel ( lv . duration || 0 ),
isLive : false ,
isDownloaded : true ,
2026-11-03 19:01:59 +03:00
isFederated : false ,
2026-11-04 05:28:50 +03:00
sourceType : sourceBucket ,
2026-11-03 18:51:10 +03:00
reasonBadge : reasonBadge ? `✅ ${ reasonBadge . replace ( /^[🔥🎯📂]\s*/ , '' ) } ` : "✅ YakTube'da İzle" ,
relevanceScore : finalScore ,
url : lv . importUrl || `/videos/watch/ ${ lv . uuid } ` ,
thumbnail : thumbUrl ,
localVideo : {
id : lv . id ,
uuid : lv . uuid ,
name : lv . name ,
url : `/videos/watch/ ${ lv . uuid } `
}
2026-11-04 05:28:50 +03:00
};
if ( alreadyWatched ) {
watchedFallbackItems . push ({
... candidateObj ,
relevanceScore : Math . max ( 10 , finalScore - 80 ),
reasonBadge : '🔄 Tekrar İzle'
});
} else {
scoredLocalItems . push ( candidateObj );
}
2026-11-03 18:51:10 +03:00
}
} catch ( e ) {
if ( logger ) logger . warn ( '[YakTube SmartRec] Local DB query warning: ' + e . message );
}
2026-11-03 19:01:59 +03:00
// 1.5. Score Relevant Fediverse (Remote PeerTube) Videos when federatedRecommendationsMode !== 'disabled'
if ( federatedRecommendationsMode !== 'disabled' ) {
try {
2026-11-17 23:15:56 +03:00
const trgmPatterns = Array . from (
2026-11-03 19:01:59 +03:00
new Set (
2026-11-04 05:28:50 +03:00
[
...( artistNorm && artistNorm . length >= 3 ? [ artistNorm ] : []),
...( channelCreatorNorm && channelCreatorNorm . length >= 3 ? [ channelCreatorNorm ] : []),
... currentKeywords ,
... historyKeywords
]
2026-11-17 23:15:56 +03:00
. map ( k =>
String ( k || '' )
. toLowerCase ()
. replace ( /[ç]/g , 'c' )
. replace ( /[ğ]/g , 'g' )
. replace ( /[ıİ]/g , 'i' )
. replace ( /[ö]/g , 'o' )
. replace ( /[ş]/g , 's' )
. replace ( /[ü]/g , 'u' )
. trim ()
)
2026-11-03 19:01:59 +03:00
. filter ( k => k && k . length >= 3 )
2026-11-17 23:15:56 +03:00
. slice ( 0 , 6 )
2026-11-03 19:01:59 +03:00
. map ( k => `% ${ k } %` )
)
);
2026-11-17 23:15:56 +03:00
if ( trgmPatterns . length > 0 ) {
2026-11-03 19:01:59 +03:00
const fedRows = await dbQuery (
2026-11-17 23:15:56 +03:00
`SELECT v.id, v.uuid, v.name, v.category, v.language, v.duration, v.views, v."isLive", v.nsfw,
(SELECT t.filename FROM "thumbnail" t WHERE t."videoId" = v.id ORDER BY t.width ASC NULLS LAST LIMIT 1) AS "thumbFilename"
2026-11-03 19:01:59 +03:00
FROM "video" v
WHERE v.remote = true
AND v.state = 1
AND v.privacy = 1
AND v.nsfw = false
2026-11-17 23:15:56 +03:00
AND lower(immutable_unaccent(v.name::text)) LIKE ANY($1)
LIMIT 40` ,
[ trgmPatterns ]
2026-11-03 19:01:59 +03:00
);
for ( const fv of fedRows ) {
if ( ! fv || ! fv . name ) continue ;
2026-11-04 05:28:50 +03:00
if ( isCurrentVideoMatch ( fv . uuid , null , fv . name )) continue ;
if ( isAlreadyWatchedVideo ( fv . uuid , null , fv . name )) continue ;
2026-11-03 19:01:59 +03:00
if ( safeSearchEnabled && ( fv . nsfw || isContentBlocked ( fv . name , blockedKeywordsList ))) continue ;
const normFv = normalizeTitleForMatch ( fv . name );
2026-11-04 05:28:50 +03:00
if ( ! normFv ) continue ;
2026-11-03 19:01:59 +03:00
let fedScore = 0 ;
let fedBadge = '' ;
if ( artistNorm && artistNorm . length >= 3 && normFv . includes ( artistNorm )) {
fedScore += 65 ;
fedBadge = '🌐 Fediverse • Aynı Sanatçı' ;
}
const fvKeywords = extractRecommendationKeywords ( fv . name );
let kwOverlap = 0 ;
for ( const kw of currentKeywords ) {
if ( fvKeywords . includes ( kw ) || ( kw . length >= 4 && normFv . includes ( kw ))) {
kwOverlap ++ ;
}
}
if ( kwOverlap > 0 ) {
fedScore += kwOverlap * 30 ;
if ( ! fedBadge ) fedBadge = '🌐 Fediverse • Benzer İçerik' ;
}
let histOverlap = 0 ;
for ( const hkw of historyKeywords ) {
if ( fvKeywords . includes ( hkw ) || ( hkw . length >= 4 && normFv . includes ( hkw ))) {
histOverlap ++ ;
}
}
if ( histOverlap > 0 ) {
fedScore += Math . min ( 48 , histOverlap * 24 );
2026-11-04 05:28:50 +03:00
if ( ! fedBadge ) fedBadge = '🌐 Fediverse • İzleme Geçmişinize Göre' ;
2026-11-03 19:01:59 +03:00
}
if (
currentCategory &&
fv . category &&
Number ( fv . category ) === currentCategory &&
String ( fv . language || '' ). toLowerCase () === 'tr'
) {
fedScore += 22 ;
if ( ! fedBadge ) fedBadge = '🌐 Fediverse • Aynı Kategori' ;
}
// Require strong relevance (score >= 28) so random foreign webcams never slip in
if ( fedScore < 28 ) continue ;
const thumbUrl = fv . thumbFilename
? `/lazy-static/thumbnails/ ${ fv . thumbFilename } `
: '/client/assets/images/default-thumbnail.jpg' ;
scoredLocalItems . push ({
id : `fed- ${ fv . uuid } ` ,
title : fv . name ,
channel : 'Fediverse Ağı' ,
duration : fv . duration || 0 ,
durationFormatted : fv . isLive ? 'CANLI' : formatDurationLabel ( fv . duration || 0 ),
isLive : Boolean ( fv . isLive ),
isDownloaded : true ,
isFederated : true ,
2026-11-04 05:28:50 +03:00
sourceType : 'federated' ,
2026-11-03 19:01:59 +03:00
reasonBadge : fedBadge || "🌐 Fediverse'de İzle" ,
relevanceScore : fedScore + 20 ,
url : `/videos/watch/ ${ fv . uuid } ` ,
thumbnail : thumbUrl ,
localVideo : {
id : fv . id ,
uuid : fv . uuid ,
name : fv . name ,
url : `/videos/watch/ ${ fv . uuid } `
}
});
}
}
} catch ( e ) {
if ( logger ) logger . warn ( '[YakTube SmartRec] Federated DB query warning: ' + e . message );
}
}
2026-11-04 05:28:50 +03:00
// 2. Query YouTube On-Demand for:
// (a) Current Video Topic
// (b) Same YouTube Channel / Creator (so other videos from the same channel appear!)
// (c) User Watch & Search History (up to 2 distinct interest queries)
2026-11-03 18:51:10 +03:00
let primaryYtResults = [];
2026-11-04 05:28:50 +03:00
let creatorYtResults = [];
2026-11-03 18:51:10 +03:00
let historyYtResults = [];
try {
2026-11-04 05:28:50 +03:00
const pTask = searchYouTubeRaw ( primaryQuery , 16 ). catch (() => ({ results : [] }));
const cTask =
creatorQuery &&
normalizeTitleForMatch ( creatorQuery ) !== normalizeTitleForMatch ( primaryQuery ) &&
( ! safeSearchEnabled || ! isContentBlocked ( creatorQuery , blockedKeywordsList ))
? searchYouTubeRaw ( creatorQuery , 10 ). catch (() => ({ results : [] }))
: Promise . resolve ({ results : [] });
const hTasks = historyQueries
. filter ( hq => ! safeSearchEnabled || ! isContentBlocked ( hq , blockedKeywordsList ))
. map ( hq => searchYouTubeRaw ( hq , 8 ). catch (() => ({ results : [] })));
const [ primaryRes , creatorRes , ... histResList ] = await Promise . all ([ pTask , cTask , ... hTasks ]);
2026-11-03 18:51:10 +03:00
primaryYtResults = await enrichResultsWithLocalStatus (( primaryRes && primaryRes . results ) || []);
2026-11-04 05:28:50 +03:00
if ( creatorRes && creatorRes . results && creatorRes . results . length > 0 ) {
creatorYtResults = await enrichResultsWithLocalStatus ( creatorRes . results );
}
const combinedHistRaw = histResList . flatMap ( r => ( r && r . results ) || []);
if ( combinedHistRaw . length > 0 ) {
historyYtResults = await enrichResultsWithLocalStatus ( combinedHistRaw );
2026-11-03 18:51:10 +03:00
}
} catch ( e ) {}
const scoredYtItems = [];
2026-11-04 05:28:50 +03:00
function matchesSameChannelOrCreator ( item ) {
const normItemTitle = normalizeTitleForMatch ( item . title || '' );
const normItemChan = normalizeTitleForMatch ( item . channel || '' );
if (
channelCreatorNorm &&
channelCreatorNorm . length >= 3 &&
( normItemChan . includes ( channelCreatorNorm ) ||
channelCreatorNorm . includes ( normItemChan ) ||
normItemTitle . includes ( channelCreatorNorm ))
) {
if ( normItemChan . length >= 3 || normItemTitle . includes ( channelCreatorNorm )) return true ;
}
2026-11-03 18:51:10 +03:00
if (
artistNorm &&
artistNorm . length >= 3 &&
2026-11-04 05:28:50 +03:00
( normItemChan . includes ( artistNorm ) || normItemTitle . includes ( artistNorm ))
2026-11-03 18:51:10 +03:00
) {
2026-11-04 05:28:50 +03:00
return true ;
}
return false ;
}
// Score same-channel / creator YouTube results
creatorYtResults . forEach (( item , idx ) => {
if ( ! item || ! item . id || ! item . title ) return ;
const localUuid = item . localVideo && item . localVideo . uuid ? item . localVideo . uuid : null ;
if ( isCurrentVideoMatch ( localUuid , item . id , item . title )) return ;
if ( isAlreadyWatchedVideo ( localUuid , item . id , item . title )) return ;
let score = Math . max ( 66 , 88 - idx * 2 );
const chanLabel = item . channel || creatorQuery ;
let badge = chanLabel ? `📺 Aynı Kanaldan • ${ chanLabel } ` : '📺 Aynı Kanaldan' ;
if ( item . isDownloaded && item . localVideo ) {
score += 25 ;
hasDirectLocalMatch = true ;
badge = `✅ Aynı Kanaldan • ${ chanLabel } ` ;
}
scoredYtItems . push ({
... item ,
reasonBadge : badge ,
relevanceScore : score ,
sourceType : 'channel'
});
});
// Score primary topic YouTube results
primaryYtResults . forEach (( item , idx ) => {
if ( ! item || ! item . id || ! item . title ) return ;
const localUuid = item . localVideo && item . localVideo . uuid ? item . localVideo . uuid : null ;
if ( isCurrentVideoMatch ( localUuid , item . id , item . title )) return ;
if ( isAlreadyWatchedVideo ( localUuid , item . id , item . title )) return ;
let score = Math . max ( 55 , 78 - idx * 1.5 );
let badge = '🔥 Benzer İçerik' ;
let sType = 'topic' ;
if ( matchesSameChannelOrCreator ( item )) {
score += 16 ;
badge = item . channel ? `📺 Aynı Kanaldan • ${ item . channel } ` : '📺 Aynı Kanaldan / Sanatçı' ;
sType = 'channel' ;
2026-11-03 18:51:10 +03:00
}
const itemKws = extractRecommendationKeywords ( item . title );
let histHits = 0 ;
for ( const hkw of historyKeywords ) {
if ( itemKws . includes ( hkw )) histHits ++ ;
}
if ( histHits > 0 ) {
score += 8 ;
}
if ( item . isDownloaded && item . localVideo ) {
score += 25 ;
hasDirectLocalMatch = true ;
2026-11-04 05:28:50 +03:00
badge = sType === 'channel' ? `✅ Aynı Kanaldan • ${ item . channel || '' } ` . trim () : "✅ YakTube'da İzle" ;
2026-11-03 18:51:10 +03:00
}
scoredYtItems . push ({
... item ,
reasonBadge : badge ,
relevanceScore : score ,
2026-11-04 05:28:50 +03:00
sourceType : sType
2026-11-03 18:51:10 +03:00
});
});
2026-11-04 05:28:50 +03:00
// Score personalized watch/search history YouTube results
2026-11-03 18:51:10 +03:00
historyYtResults . forEach (( item , idx ) => {
if ( ! item || ! item . id || ! item . title ) return ;
2026-11-04 05:28:50 +03:00
const localUuid = item . localVideo && item . localVideo . uuid ? item . localVideo . uuid : null ;
if ( isCurrentVideoMatch ( localUuid , item . id , item . title )) return ;
if ( isAlreadyWatchedVideo ( localUuid , item . id , item . title )) return ;
2026-11-03 18:51:10 +03:00
2026-11-04 05:28:50 +03:00
let score = Math . max ( 54 , 74 - idx * 1.8 );
let badge = '🎯 İzleme Geçmişinize Göre' ;
2026-11-03 18:51:10 +03:00
if ( item . isDownloaded && item . localVideo ) {
score += 22 ;
2026-11-04 05:28:50 +03:00
badge = '✅ İzleme Geçmişinize Göre' ;
2026-11-03 18:51:10 +03:00
}
scoredYtItems . push ({
... item ,
reasonBadge : badge ,
relevanceScore : score ,
sourceType : 'history'
});
});
2026-11-04 05:28:50 +03:00
// 3. Merge, Deduplicate & Interleave (Channel + Topic + Watch History, just like YouTube)
2026-11-03 18:51:10 +03:00
const combined = [... scoredLocalItems , ... scoredYtItems ];
combined . sort (( a , b ) => ( b . relevanceScore || 0 ) - ( a . relevanceScore || 0 ));
2026-11-04 05:28:50 +03:00
const dedupedPool = [];
2026-11-03 18:51:10 +03:00
const seenIds = new Set ();
const seenUuids = new Set ();
const seenNormTitles = new Set ();
2026-11-04 05:28:50 +03:00
const seenCoreTitles = new Set ();
2026-11-03 18:51:10 +03:00
for ( const item of combined ) {
const normT = normalizeTitleForMatch ( item . title );
2026-11-04 05:28:50 +03:00
const coreT = normalizeCoreVideoTitle ( item . title );
2026-11-03 18:51:10 +03:00
if ( ! normT || normT === normCurrentTitle ) continue ;
2026-11-04 05:28:50 +03:00
if ( coreT && coreCurrentTitle && coreT . length >= 5 && coreT === coreCurrentTitle ) continue ;
2026-11-03 18:51:10 +03:00
if ( item . id && seenIds . has ( item . id )) continue ;
if ( item . localVideo && item . localVideo . uuid && seenUuids . has ( item . localVideo . uuid )) continue ;
if ( seenNormTitles . has ( normT )) continue ;
2026-11-04 05:28:50 +03:00
if ( coreT && coreT . length >= 5 && seenCoreTitles . has ( coreT )) continue ;
2026-11-03 18:51:10 +03:00
if ( item . id ) seenIds . add ( item . id );
if ( item . localVideo && item . localVideo . uuid ) seenUuids . add ( item . localVideo . uuid );
seenNormTitles . add ( normT );
2026-11-04 05:28:50 +03:00
if ( coreT && coreT . length >= 5 ) seenCoreTitles . add ( coreT );
2026-11-03 18:51:10 +03:00
2026-11-04 05:28:50 +03:00
dedupedPool . push ( item );
2026-11-03 18:51:10 +03:00
}
2026-11-04 05:28:50 +03:00
// Ensure a rich YouTube-like mix in the top results:
// - #1 is the highest-scoring unwatched Same-Channel or Topic match (for seamless Autoplay)
// - Interleave Same-Channel ('channel'), Topic ('topic'), and Watch History ('history') items
const channelBucket = dedupedPool . filter ( i => i . sourceType === 'channel' );
const historyBucket = dedupedPool . filter ( i => i . sourceType === 'history' );
const topicBucket = dedupedPool . filter ( i => i . sourceType !== 'channel' && i . sourceType !== 'history' );
const finalResults = [];
const pickedSet = new Set ();
function pushUnique ( candidate ) {
if ( ! candidate || pickedSet . has ( candidate )) return false ;
pickedSet . add ( candidate );
finalResults . push ( candidate );
return true ;
}
// Slot #1 (Up Next Autoplay): Best Same-Channel or Topic video
if (
channelBucket . length > 0 &&
( ! topicBucket [ 0 ] || channelBucket [ 0 ]. relevanceScore >= topicBucket [ 0 ]. relevanceScore - 10 )
) {
pushUnique ( channelBucket [ 0 ]);
} else if ( topicBucket . length > 0 ) {
pushUnique ( topicBucket [ 0 ]);
}
// Interleave remaining top candidates so the user sees Same-Channel, Topic, and History videos together
let cIdx = 0 ;
let tIdx = 0 ;
let hIdx = 0 ;
while (
finalResults . length < 24 &&
( cIdx < channelBucket . length || tIdx < topicBucket . length || hIdx < historyBucket . length )
) {
if ( cIdx < channelBucket . length ) pushUnique ( channelBucket [ cIdx ++ ]);
if ( tIdx < topicBucket . length ) pushUnique ( topicBucket [ tIdx ++ ]);
if ( tIdx < topicBucket . length ) pushUnique ( topicBucket [ tIdx ++ ]);
if ( hIdx < historyBucket . length ) pushUnique ( historyBucket [ hIdx ++ ]);
}
// Only if there are almost no unwatched results (< 4), append watched fallbacks at the very bottom (never #1)
if ( finalResults . length < 4 && watchedFallbackItems . length > 0 ) {
for ( const wf of watchedFallbackItems ) {
if ( finalResults . length >= 8 ) break ;
const normT = normalizeTitleForMatch ( wf . title );
if ( ! seenNormTitles . has ( normT )) {
seenNormTitles . add ( normT );
finalResults . push ( wf );
}
}
}
// Identify #1 undownloaded match for optional background smart pre-fetch
2026-11-03 18:51:10 +03:00
const topPrefetchCandidate =
finalResults . find (
it =>
! it . isDownloaded &&
! it . isLive &&
it . url &&
( it . url . includes ( 'youtube.com' ) || it . url . includes ( 'youtu.be' )) &&
( ! it . duration || it . duration <= 900 ) // Only pre-fetch videos <= 15 mins to respect server storage/bandwidth
) || null ;
return res . json ({
ok : true ,
query : primaryQuery ,
2026-11-04 05:28:50 +03:00
creatorQuery : creatorQuery || null ,
historyQuery : historyQueries [ 0 ] || null ,
historyQueries ,
2026-11-03 18:51:10 +03:00
hasLocalMatch : hasDirectLocalMatch ,
smartPrefetchMode ,
2026-11-03 19:01:59 +03:00
federatedRecommendationsMode ,
2026-11-03 18:51:10 +03:00
topPrefetchCandidate : topPrefetchCandidate
? {
id : topPrefetchCandidate . id ,
title : topPrefetchCandidate . title ,
url : topPrefetchCandidate . url ,
channel : topPrefetchCandidate . channel
}
: null ,
guestImportLimit ,
results : finalResults
});
});
2026-10-03 10:45:58 +03:00
// 3. On-Demand Video Import Route (aliases: /ondemand-import and /import)
async function handleOnDemandImport ( req , res ) {
await loadPluginSettings ();
2026-11-04 04:48:03 +03:00
if ( isCrawlerBotRequest ( req )) {
return res . status ( 403 ). json ({ ok : false , error : 'Automated bot requests are not allowed to import videos.' });
}
2026-10-03 10:45:58 +03:00
const { targetUrl , title , channelId , isAuthenticated , guestImportCount } = req . body || {};
2026-08-29 12:42:36 +03:00
if ( ! targetUrl || ( ! targetUrl . includes ( 'youtube.com' ) && ! targetUrl . includes ( 'youtu.be' ))) {
return res . status ( 400 ). json ({ error : 'Invalid YouTube URL' });
}
2026-11-04 04:48:03 +03:00
const ytId = extractYouTubeIdFromUrl ( targetUrl );
const normalizedTargetUrl = ytId ? `https://www.youtube.com/watch?v= ${ ytId } ` : targetUrl . trim ();
const dedupeKey = ytId || normalizedTargetUrl ;
2026-10-23 08:59:50 +03:00
if (
safeSearchEnabled &&
2026-11-04 04:48:03 +03:00
( isContentBlocked ( title , blockedKeywordsList ) || isContentBlocked ( normalizedTargetUrl , blockedKeywordsList ))
2026-10-23 08:59:50 +03:00
) {
return res . status ( 403 ). json ({
ok : false ,
blocked : true ,
error :
"Bu video içeriği, Çocuk ve Aile Güvenliği (Child & Family Safety) politikaları gereğince YakTube'a aktarı lamaz."
});
}
2026-11-04 04:48:03 +03:00
// 1. Check DB for already published, currently importing, or previously failed records for this YouTube video
try {
const searchPattern = ytId ? `% ${ ytId } %` : normalizedTargetUrl ;
const existingRows = await dbQuery (
`SELECT vi.id AS "importId", vi.state AS "importState",
v.id, v.uuid, v.name, v.state AS "videoState", v."updatedAt"
FROM "videoImport" vi
INNER JOIN "video" v ON v.id = vi."videoId"
WHERE vi."targetUrl" LIKE $1
ORDER BY vi.id DESC` ,
[ searchPattern ]
);
const publishedRow = existingRows . find ( r => Number ( r . videoState ) === 1 );
if ( publishedRow ) {
return res . json ({
ok : true ,
alreadyImported : true ,
importId : publishedRow . importId ,
video : {
id : publishedRow . id ,
uuid : publishedRow . uuid ,
name : publishedRow . name ,
url : '/videos/watch/' + publishedRow . uuid
}
});
}
const activeRow = existingRows . find ( r => {
const vState = Number ( r . videoState );
const iState = Number ( r . importState );
const ageMs = r . updatedAt ? Date . now () - new Date ( r . updatedAt ). getTime () : 0 ;
return ( vState === 2 || vState === 3 ) && iState !== 3 && iState !== 4 && ageMs < 10 * 60 * 1000 ;
});
if ( activeRow ) {
return res . json ({
ok : true ,
inProgress : true ,
importId : activeRow . importId ,
video : {
id : activeRow . id ,
uuid : activeRow . uuid ,
name : activeRow . name ,
url : '/videos/watch/' + activeRow . uuid
}
});
}
// Clean up any failed (v.state = 12 / vi.state = 3) or stale (>15 min in state 3) rows before re-importing
const staleOrFailedRows = existingRows . filter ( r => {
const vState = Number ( r . videoState );
const iState = Number ( r . importState );
const ageMs = r . updatedAt ? Date . now () - new Date ( r . updatedAt ). getTime () : 0 ;
return vState === 12 || iState === 3 || ( vState === 3 && ageMs >= 15 * 60 * 1000 );
});
if ( staleOrFailedRows . length > 0 ) {
const badImportIds = staleOrFailedRows . map ( r => r . importId ). filter ( Boolean );
const badVideoIds = staleOrFailedRows . map ( r => r . id ). filter ( Boolean );
if ( badImportIds . length > 0 ) {
await dbQuery ( `DELETE FROM "videoImport" WHERE id = ANY($1::int[])` , [ badImportIds ]);
}
if ( badVideoIds . length > 0 ) {
await dbQuery (
`DELETE FROM "video" v
WHERE v.id = ANY($1::int[])
AND v.state IN (3, 12)
AND NOT EXISTS (SELECT 1 FROM "videoFile" vf WHERE vf."videoId" = v.id)
AND NOT EXISTS (SELECT 1 FROM "videoStreamingPlaylist" vsp WHERE vsp."videoId" = v.id)` ,
[ badVideoIds ]
);
}
}
} catch ( dbCheckErr ) {}
// 2. If another request is already initiating an import for this exact video right now, await it
if ( inFlightImportPromises . has ( dedupeKey )) {
try {
const sharedResult = await inFlightImportPromises . get ( dedupeKey );
return res . status ( sharedResult . status ). json ( sharedResult . payload );
} catch ( sharedErr ) {
return res . status ( 500 ). json ({ error : sharedErr . message });
}
}
2026-10-03 10:45:58 +03:00
const isLoggedUser = await isRequestAuthenticated ( req , res , isAuthenticated );
2026-10-03 10:26:46 +03:00
if ( ! isLoggedUser && guestImportLimit > 0 ) {
const clientCount = parseInt ( String ( guestImportCount || '0' ), 10 ) || 0 ;
if ( clientCount >= guestImportLimit ) {
return res . status ( 429 ). json ({
ok : false ,
quotaExceeded : true ,
limit : guestImportLimit ,
2026-10-18 12:03:32 +03:00
error :
'Kota Doldu: Misafir video indirme sı nı rı na ulaştı nı z. Sı nı rsı z video aktarı mı , yorum yapabilme ve diğer tüm ayrı calı klar için lütfen giriş yapı n.'
2026-10-03 10:26:46 +03:00
});
}
}
2026-10-03 11:14:03 +03:00
// Non-blocking background check to prune old videos if disk usage exceeds maxDiskUsagePercent
checkAndPruneStorage (). catch (() => {});
2026-11-04 04:48:03 +03:00
const importPromise = ( async () => {
2026-08-29 12:42:36 +03:00
const token = await getAdminToken ();
2026-10-03 10:45:58 +03:00
const resolvedChannelId = await resolveTargetChannelId ( title , channelId );
const { hostname , port , hostHeader } = getLocalServerTarget ();
2026-08-29 12:42:36 +03:00
const postData = new URLSearchParams ({
2026-11-04 04:48:03 +03:00
targetUrl : normalizedTargetUrl ,
2026-09-19 05:14:11 +03:00
channelId : resolvedChannelId ,
2026-08-29 12:42:36 +03:00
privacy : '1'
}). toString ();
2026-11-04 04:48:03 +03:00
return await new Promise (( resolve , reject ) => {
const importReq = http . request (
{
hostname ,
port ,
path : '/api/v1/videos/imports' ,
method : 'POST' ,
headers : {
Host : hostHeader ,
Authorization : 'Bearer ' + token ,
'Content-Type' : 'application/x-www-form-urlencoded' ,
'Content-Length' : Buffer . byteLength ( postData )
}
},
importRes => {
let body = '' ;
importRes . on ( 'data' , c => ( body += c ));
importRes . on ( 'end' , () => {
try {
const data = JSON . parse ( body );
if ( data && data . video ) {
2026-11-06 05:33:47 +03:00
syncVideoChannelAndCategory ( data . video . id ). catch (() => {});
2026-11-04 04:48:03 +03:00
return resolve ({
status : 200 ,
payload : {
ok : true ,
importId : data . id ,
video : {
id : data . video . id ,
uuid : data . video . uuid ,
shortUUID : data . video . shortUUID ,
name : data . video . name ,
url : data . video . url
}
}
});
} else {
const friendlyMsg = translateImportErrorMessage ( body );
return resolve ({
status : 400 ,
payload : {
ok : false ,
error : friendlyMsg ,
userMessage : friendlyMsg ,
details : data
}
});
}
} catch ( e ) {
const friendlyMsg = translateImportErrorMessage ( body );
return resolve ({
status : 500 ,
payload : {
ok : false ,
error : friendlyMsg ,
userMessage : friendlyMsg ,
raw : body
2026-08-29 12:42:36 +03:00
}
});
}
2026-11-04 04:48:03 +03:00
});
}
);
2026-08-29 12:42:36 +03:00
2026-11-04 04:48:03 +03:00
importReq . on ( 'error' , err => {
reject ( err );
});
importReq . write ( postData );
importReq . end ();
2026-08-29 12:42:36 +03:00
});
2026-11-04 04:48:03 +03:00
})();
2026-08-29 12:42:36 +03:00
2026-11-04 04:48:03 +03:00
inFlightImportPromises . set ( dedupeKey , importPromise );
try {
const outcome = await importPromise ;
return res . status ( outcome . status ). json ( outcome . payload );
2026-08-29 12:42:36 +03:00
} catch ( err ) {
2026-11-04 04:48:03 +03:00
return res . status ( 500 ). json ({ error : 'Admin authentication or HTTP connection failed' , details : err . message });
} finally {
inFlightImportPromises . delete ( dedupeKey );
2026-08-29 12:42:36 +03:00
}
2026-10-03 10:45:58 +03:00
}
2026-08-29 12:42:36 +03:00
2026-10-03 10:45:58 +03:00
router . post ( '/ondemand-import' , handleOnDemandImport );
router . post ( '/import' , handleOnDemandImport );
// 4. Video Import Status Polling Route
2026-08-29 12:42:36 +03:00
router . get ( '/status/:id' , async ( req , res ) => {
2026-11-17 23:15:56 +03:00
if ( isCrawlerBotRequest ( req )) {
return res . status ( 403 ). json ({ error : 'Bot requests are not allowed on import status endpoint' });
}
2026-08-29 12:42:36 +03:00
const videoId = req . params . id ;
2026-10-03 10:45:58 +03:00
const { hostname , port , hostHeader } = getLocalServerTarget ();
2026-08-29 12:42:36 +03:00
const reqVideo = http . request (
{
2026-10-03 10:45:58 +03:00
hostname ,
port ,
2026-08-29 12:42:36 +03:00
path : '/api/v1/videos/' + encodeURIComponent ( videoId ),
method : 'GET' ,
2026-10-03 10:45:58 +03:00
headers : { Host : hostHeader }
2026-08-29 12:42:36 +03:00
},
resVideo => {
let body = '' ;
resVideo . on ( 'data' , c => ( body += c ));
2026-11-04 04:48:03 +03:00
resVideo . on ( 'end' , async () => {
2026-08-29 12:42:36 +03:00
try {
const video = JSON . parse ( body );
2026-10-03 10:45:58 +03:00
const isPublished = video . state && video . state . id === 1 ;
const isTranscoding = video . state && video . state . id === 2 ;
const isImporting = video . state && video . state . id === 3 ;
const isFailed = video . state && video . state . id === 12 ;
const hasFiles =
( video . files && video . files . length > 0 ) ||
( video . streamingPlaylists && video . streamingPlaylists . length > 0 );
2026-11-06 05:33:47 +03:00
if (( isPublished || isTranscoding ) && video . id ) {
await syncVideoChannelAndCategory ( video . id );
}
2026-11-04 04:48:03 +03:00
let errorMessage = null ;
if ( isFailed && video . id ) {
try {
const errRows = await dbQuery (
`SELECT id, error FROM "videoImport" WHERE "videoId" = $1 ORDER BY id DESC LIMIT 1` ,
[ video . id ]
);
const rawErr = errRows . length > 0 ? errRows [ 0 ]. error : '' ;
errorMessage = translateImportErrorMessage ( rawErr );
// Auto-cleanup failed import and orphan state=12 video so it never pollutes search or library
await dbQuery ( `DELETE FROM "videoImport" WHERE "videoId" = $1 AND state = 3` , [ video . id ]);
if ( ! hasFiles ) {
await dbQuery (
`DELETE FROM "video" v
WHERE v.id = $1
AND v.state = 12
AND NOT EXISTS (SELECT 1 FROM "videoFile" vf WHERE vf."videoId" = v.id)
AND NOT EXISTS (SELECT 1 FROM "videoStreamingPlaylist" vsp WHERE vsp."videoId" = v.id)` ,
[ video . id ]
);
}
} catch ( cleanErr ) {}
}
2026-08-29 12:42:36 +03:00
return res . json ({
id : video . id ,
uuid : video . uuid ,
shortUUID : video . shortUUID ,
name : video . name ,
state : video . state ,
2026-10-03 10:45:58 +03:00
isPublished ,
isTranscoding ,
isImporting ,
isFailed ,
hasFiles ,
2026-11-04 04:48:03 +03:00
errorMessage ,
2026-08-29 12:42:36 +03:00
url : video . url ,
isLocal : video . isLocal
});
} catch ( e ) {
return res . status ( 500 ). json ({ error : 'Failed to parse video info' });
}
});
}
);
reqVideo . on ( 'error' , e => res . status ( 500 ). json ({ error : e . message }));
reqVideo . end ();
});
2026-10-03 10:45:58 +03:00
// 5. Live Stream HLS URL Extraction Route
router . get ( '/live-stream-url' , async ( req , res ) => {
const target = ( req . query . url || req . query . id || '' ). trim ();
if ( ! target ) {
return res . status ( 400 ). json ({ error : 'Target URL is required' });
}
const ytdlpPath = resolveYtDlpPath ( logger , customYtDlpPath );
const fullUrl = target . startsWith ( 'http' ) ? target : `https://www.youtube.com/watch?v= ${ target } ` ;
execFile ( ytdlpPath , [ '-g' , fullUrl ], { timeout : 12000 }, ( err , stdout , stderr ) => {
if ( err || ! stdout . trim ()) {
return res
. status ( 500 )
. json ({ error : 'Could not extract live stream URL' , details : err ? err . message : stderr });
}
const lines = stdout . trim (). split ( /\r?\n/ );
return res . json ({
ok : true ,
streamUrl : lines [ 0 ],
isLive : true
});
});
});
// 6. Video YouTube Origin Detection Route
2026-10-16 18:23:11 +03:00
const BASE58_ALPHABET = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' ;
function shortToUuid ( shortStr ) {
if ( ! shortStr || typeof shortStr !== 'string' || shortStr . length < 20 || shortStr . length > 24 ) return shortStr ;
if ( /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i . test ( shortStr )) return shortStr ;
try {
let num = 0 n ;
for ( let i = 0 ; i < shortStr . length ; i ++ ) {
const idx = BASE58_ALPHABET . indexOf ( shortStr [ i ]);
if ( idx === - 1 ) return shortStr ;
num = num * 58 n + BigInt ( idx );
}
const hex = num . toString ( 16 ). padStart ( 32 , '0' );
if ( hex . length !== 32 ) return shortStr ;
return ` ${ hex . slice ( 0 , 8 ) } - ${ hex . slice ( 8 , 12 ) } - ${ hex . slice ( 12 , 16 ) } - ${ hex . slice ( 16 , 20 ) } - ${ hex . slice ( 20 , 32 ) } ` ;
} catch ( e ) {
return shortStr ;
}
}
2026-11-17 23:15:56 +03:00
const videoOriginCache = new Map ();
const VIDEO_ORIGIN_CACHE_TTL_MS = 10 * 60 * 1000 ;
2026-12-01 18:42:05 +03:00
const transcriptMemoryCache = new Map ();
const inFlightTranscriptRequests = new Map ();
const TRANSCRIPT_OK_TTL_MS = 12 * 60 * 60 * 1000 ;
const TRANSCRIPT_EMPTY_TTL_MS = 10 * 60 * 1000 ;
function resolveTranscriptStorageRoot () {
try {
if (
peertubeHelpers &&
peertubeHelpers . plugin &&
typeof peertubeHelpers . plugin . getDataDirectoryPath === 'function'
) {
const dataDir = peertubeHelpers . plugin . getDataDirectoryPath ();
const candidate = path . resolve ( dataDir , '..' , '..' , '..' );
if ( fs . existsSync ( candidate )) return candidate ;
}
} catch ( e ) {}
if ( fs . existsSync ( 'D:\\yaktube_storage' )) return 'D:\\yaktube_storage' ;
return process . cwd ();
}
function extractYouTubeIdFromText ( str ) {
if ( ! str || typeof str !== 'string' ) return null ;
const match =
str . match ( /[?&]v=([a-zA-Z0-9_-]{11})/ ) ||
str . match ( /youtu\.be\/([a-zA-Z0-9_-]{11})/ ) ||
str . match ( /shorts\/([a-zA-Z0-9_-]{11})/ ) ||
str . match ( /embed\/([a-zA-Z0-9_-]{11})/ );
return match ? match [ 1 ] : null ;
}
function extractYouTubeIdFromRow ( row ) {
if ( ! row ) return null ;
return extractYouTubeIdFromText ( row . targetUrl ) || extractYouTubeIdFromText ( row . description );
}
function fetchRemoteTextWithLimit ( targetUrl , timeoutMs = 9000 , maxRedirects = 4 , maxBytes = 4 * 1024 * 1024 ) {
return new Promise (( resolve , reject ) => {
if ( maxRedirects <= 0 ) return reject ( new Error ( 'Too many redirects' ));
let parsed ;
try {
parsed = new URL ( targetUrl );
} catch ( e ) {
return reject ( e );
}
if ( parsed . protocol !== 'http:' && parsed . protocol !== 'https:' ) {
return reject ( new Error ( 'Unsupported protocol' ));
}
const lib = parsed . protocol === 'https:' ? https : http ;
const req = lib . get (
targetUrl ,
{
timeout : timeoutMs ,
headers : {
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/131.0.0.0 Safari/537.36' ,
Accept : 'application/json, text/vtt, text/plain, */*'
}
},
res => {
if ([ 301 , 302 , 307 , 308 ]. includes ( res . statusCode ) && res . headers . location ) {
res . resume ();
const nextUrl = new URL ( res . headers . location , targetUrl ). toString ();
return resolve ( fetchRemoteTextWithLimit ( nextUrl , timeoutMs , maxRedirects - 1 , maxBytes ));
}
if ( res . statusCode !== 200 ) {
res . resume ();
return reject ( new Error ( 'HTTP ' + res . statusCode ));
}
const chunks = [];
let received = 0 ;
res . on ( 'data' , chunk => {
received += chunk . length ;
if ( received > maxBytes ) {
req . destroy ( new Error ( 'Response too large' ));
return ;
}
chunks . push ( chunk );
});
res . on ( 'end' , () => resolve ( Buffer . concat ( chunks ). toString ( 'utf8' )));
res . on ( 'error' , reject );
}
);
req . on ( 'timeout' , () => req . destroy ( new Error ( 'Timeout' )));
req . on ( 'error' , reject );
});
}
function parseTimeTokenToSeconds ( token ) {
if ( ! token || typeof token !== 'string' ) return NaN ;
const parts = token . trim (). replace ( ',' , '.' ). split ( ':' );
if ( parts . length === 2 ) {
const m = parseInt ( parts [ 0 ], 10 );
const s = parseFloat ( parts [ 1 ]);
if ( isNaN ( m ) || isNaN ( s )) return NaN ;
return m * 60 + s ;
}
if ( parts . length === 3 ) {
const h = parseInt ( parts [ 0 ], 10 );
const m = parseInt ( parts [ 1 ], 10 );
const s = parseFloat ( parts [ 2 ]);
if ( isNaN ( h ) || isNaN ( m ) || isNaN ( s )) return NaN ;
return h * 3600 + m * 60 + s ;
}
return NaN ;
}
function decodeSubtitleEntities ( str ) {
return String ( str || '' )
. replace ( /<\d{2}:\d{2}(?::\d{2})?\.\d+>/g , '' )
. replace ( /<\/?[^>]+(>|$)/g , ' ' )
. replace ( /&/g , '&' )
. replace ( /</g , '<' )
. replace ( />/g , '>' )
. replace ( /"/g , '"' )
. replace ( /'|'/g , "'" )
. replace ( / /g , ' ' )
. replace ( /\s+/g , ' ' )
. trim ();
}
function parseChaptersFromDescription ( description , maxDurationSec ) {
if ( ! description || typeof description !== 'string' ) return [];
const lines = description . split ( /\r?\n/ );
const seenStarts = new Set ();
const chapters = [];
for ( const rawLine of lines ) {
const line = rawLine . trim ();
if ( ! line || line . length > 220 ) continue ;
const mPrefix = line . match ( /^[\[\(\*•\-–—\s]*((?:\d{1,2}:)?\d{1,2}:\d{2})[\]\)\*:\-\s–—]+(.+)$/ );
const mSuffix = ! mPrefix ? line . match ( /^(.+?)[\s\-\–—:\(\[]+((?:\d{1,2}:)?\d{1,2}:\d{2})[\)\]\s]*$/ ) : null ;
const timeStr = mPrefix ? mPrefix [ 1 ] : mSuffix ? mSuffix [ 2 ] : null ;
let title = mPrefix ? mPrefix [ 2 ] : mSuffix ? mSuffix [ 1 ] : '' ;
if ( ! timeStr ) continue ;
const startSec = Math . floor ( parseTimeTokenToSeconds ( timeStr ));
if ( isNaN ( startSec ) || startSec < 0 ) continue ;
if ( maxDurationSec > 0 && startSec > maxDurationSec + 5 ) continue ;
title = title . replace ( /^[\-\–—:\s•]+|[\-\–—:\s•]+$/g , '' ). trim ();
if ( ! title || title . length < 2 ) continue ;
if ( seenStarts . has ( startSec )) continue ;
seenStarts . add ( startSec );
chapters . push ({ title , start : startSec });
}
chapters . sort (( a , b ) => a . start - b . start );
return chapters . length >= 2 ? chapters : [];
}
function parseJson3Subtitles ( rawText ) {
const parsed = JSON . parse ( rawText );
const events = Array . isArray ( parsed && parsed . events ) ? parsed . events : [];
const cues = [];
for ( const ev of events ) {
if ( ! ev || ! Array . isArray ( ev . segs )) continue ;
const text = ev . segs
. map ( s => ( s && s . utf8 ? s . utf8 : '' ))
. join ( '' )
. replace ( /\n+/g , ' ' );
const clean = decodeSubtitleEntities ( text );
if ( ! clean || clean === '[Müzik]' || clean === '[Music]' ) {
if ( ! clean ) continue ;
}
const start = Number ((( ev . tStartMs || 0 ) / 1000 ). toFixed ( 2 ));
const dur = ev . dDurationMs ? ev . dDurationMs / 1000 : 3 ;
const end = Number (( start + Math . max ( 0.5 , dur )). toFixed ( 2 ));
if (
cues . length > 0 &&
cues [ cues . length - 1 ]. text === clean &&
Math . abs ( start - cues [ cues . length - 1 ]. start ) < 2
) {
cues [ cues . length - 1 ]. end = Math . max ( cues [ cues . length - 1 ]. end , end );
continue ;
}
cues . push ({ start , end , text : clean });
}
return cues ;
}
function parseVttOrSrtSubtitles ( rawText ) {
const normalized = String ( rawText || '' )
. replace ( /^\uFEFF/ , '' )
. replace ( /\r\n/g , '\n' )
. replace ( /\r/g , '\n' );
const blocks = normalized . split ( /\n{2,}/ );
const cues = [];
for ( const block of blocks ) {
const lines = block
. split ( '\n' )
. map ( l => l . trim ())
. filter ( Boolean );
if ( ! lines . length ) continue ;
if ( /^(WEBVTT|Kind:|Language:|NOTE\b|STYLE\b)/i . test ( lines [ 0 ])) continue ;
const timeIdx = lines . findIndex ( l => l . includes ( '-->' ));
if ( timeIdx === - 1 ) continue ;
const parts = lines [ timeIdx ]. split ( '-->' );
if ( parts . length < 2 ) continue ;
const start = parseTimeTokenToSeconds ( parts [ 0 ]. trim (). split ( /\s+/ )[ 0 ]);
const end = parseTimeTokenToSeconds ( parts [ 1 ]. trim (). split ( /\s+/ )[ 0 ]);
if ( isNaN ( start )) continue ;
const rawBody = lines . slice ( timeIdx + 1 ). join ( ' ' );
const clean = decodeSubtitleEntities ( rawBody );
if ( ! clean ) continue ;
const safeStart = Number ( start . toFixed ( 2 ));
const safeEnd = Number (( ! isNaN ( end ) && end > start ? end : start + 3 ). toFixed ( 2 ));
if ( cues . length > 0 && cues [ cues . length - 1 ]. text === clean ) {
cues [ cues . length - 1 ]. end = Math . max ( cues [ cues . length - 1 ]. end , safeEnd );
continue ;
}
cues . push ({ start : safeStart , end : safeEnd , text : clean });
}
return cues ;
}
function parseSubtitlePayload ( rawText ) {
if ( ! rawText || typeof rawText !== 'string' ) return [];
const trimmed = rawText . trim ();
if ( ! trimmed ) return [];
if ( trimmed . startsWith ( '{' )) {
try {
return parseJson3Subtitles ( trimmed );
} catch ( e ) {}
}
return parseVttOrSrtSubtitles ( trimmed );
}
function pickPreferredCaptionTrack ( ytInfo , preferredLang , videoLang ) {
const manual = ytInfo && ytInfo . subtitles && typeof ytInfo . subtitles === 'object' ? ytInfo . subtitles : {};
const auto =
ytInfo && ytInfo . automatic_captions && typeof ytInfo . automatic_captions === 'object'
? ytInfo . automatic_captions
: {};
const availableLanguages = [];
const collectLangs = ( mapObj , isAuto ) => {
for ( const code of Object . keys ( mapObj )) {
if ( ! code || code === 'live_chat' ) continue ;
const tracks = mapObj [ code ];
if ( ! Array . isArray ( tracks ) || tracks . length === 0 ) continue ;
availableLanguages . push ({
code ,
name : tracks [ 0 ]. name || code ,
auto : isAuto
});
}
};
collectLangs ( manual , false );
collectLangs ( auto , true );
const priorityLangs = [];
for ( const candidate of [ preferredLang , 'tr' , videoLang , 'en' , 'en-US' , 'en-GB' ]) {
const c = candidate ? String ( candidate ). trim (). toLowerCase () : '' ;
if ( c && ! priorityLangs . includes ( c )) priorityLangs . push ( c );
}
const pickFormat = tracks => {
for ( const ext of [ 'json3' , 'vtt' , 'srt' ]) {
const hit = tracks . find ( t => t && t . ext === ext && t . url );
if ( hit ) return hit ;
}
return tracks . find ( t => t && t . url ) || null ;
};
const pickFromMap = ( mapObj , isAuto ) => {
const keys = Object . keys ( mapObj ). filter (
k => k && k !== 'live_chat' && Array . isArray ( mapObj [ k ]) && mapObj [ k ]. length
);
if ( keys . length === 0 ) return null ;
for ( const p of priorityLangs ) {
const exact = keys . find ( k => k . toLowerCase () === p ) || keys . find ( k => k . toLowerCase (). startsWith ( p + '-' ));
if ( exact ) {
const track = pickFormat ( mapObj [ exact ]);
if ( track ) return { code : exact , isAuto , track };
}
}
// Also match *-orig auto-captions (video's native language) or first available
const origKey = keys . find ( k => k . endsWith ( '-orig' )) || keys [ 0 ];
const fallbackTrack = pickFormat ( mapObj [ origKey ]);
return fallbackTrack ? { code : origKey , isAuto , track : fallbackTrack } : null ;
};
const chosen = pickFromMap ( manual , false ) || pickFromMap ( auto , true );
return { chosen , availableLanguages : availableLanguages . slice ( 0 , 12 ) };
}
2026-11-17 23:15:56 +03:00
2026-10-03 10:45:58 +03:00
router . get ( '/video-origin/:uuid' , async ( req , res ) => {
2026-11-17 23:15:56 +03:00
if ( isCrawlerBotRequest ( req )) {
return res . json ({ ok : false , hasYouTubeOrigin : false , botIgnored : true });
}
2026-10-16 18:23:11 +03:00
const rawParam = ( req . params . uuid || '' ). trim ();
if ( ! rawParam ) return res . status ( 400 ). json ({ error : 'UUID required' });
const resolvedUuid = shortToUuid ( rawParam );
2026-10-03 10:45:58 +03:00
2026-11-17 23:15:56 +03:00
const cacheHit = videoOriginCache . get ( resolvedUuid );
if ( cacheHit && Date . now () - cacheHit . time < VIDEO_ORIGIN_CACHE_TTL_MS ) {
if ( cacheHit . notFound ) return res . status ( 404 ). json ({ error : 'Video not found' });
return res . json ( cacheHit . payload );
}
2026-10-03 10:45:58 +03:00
try {
2026-11-17 23:15:56 +03:00
let rows = [];
if ( /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i . test ( resolvedUuid )) {
rows = await dbQuery (
`SELECT v.id, v.uuid, v.name, v.description, vi."targetUrl"
FROM "video" v
LEFT JOIN "videoImport" vi ON vi."videoId" = v.id
WHERE v.uuid = $1::uuid
LIMIT 1` ,
[ resolvedUuid ]
);
} else if ( /^\d+$/ . test ( rawParam )) {
rows = await dbQuery (
`SELECT v.id, v.uuid, v.name, v.description, vi."targetUrl"
FROM "video" v
LEFT JOIN "videoImport" vi ON vi."videoId" = v.id
WHERE v.id = $1::int
LIMIT 1` ,
[ parseInt ( rawParam , 10 )]
);
}
2026-10-03 10:45:58 +03:00
if ( rows . length === 0 ) {
2026-11-17 23:15:56 +03:00
if ( videoOriginCache . size > 2000 ) videoOriginCache . clear ();
videoOriginCache . set ( resolvedUuid , { time : Date . now (), notFound : true });
2026-10-03 10:45:58 +03:00
return res . status ( 404 ). json ({ error : 'Video not found' });
}
const row = rows [ 0 ];
2026-12-01 18:42:05 +03:00
const youtubeId = extractYouTubeIdFromRow ( row );
2026-10-03 10:45:58 +03:00
2026-11-17 23:15:56 +03:00
const payload = {
2026-10-03 10:45:58 +03:00
ok : true ,
hasYouTubeOrigin : !! youtubeId ,
youtubeId : youtubeId || null ,
targetUrl : row . targetUrl || null ,
title : row . name
2026-11-17 23:15:56 +03:00
};
if ( videoOriginCache . size > 2000 ) videoOriginCache . clear ();
videoOriginCache . set ( resolvedUuid , { time : Date . now (), notFound : false , payload });
return res . json ( payload );
2026-10-03 10:45:58 +03:00
} catch ( err ) {
return res . status ( 500 ). json ({ error : err . message });
}
});
2026-12-01 18:42:05 +03:00
// 6b. Akı llı Transkript & Bölüm (Chapters) Çekici
router . get ( '/video-transcript/:uuid' , async ( req , res ) => {
if ( isCrawlerBotRequest ( req )) {
return res . json ({ ok : false , botIgnored : true , cues : [], chapters : [] });
}
const rawParam = ( req . params . uuid || '' ). trim ();
if ( ! rawParam ) return res . status ( 400 ). json ({ ok : false , error : 'UUID required' });
const resolvedUuid = shortToUuid ( rawParam );
const reqLang = String ( req . query . lang || '' )
. trim ()
. toLowerCase ();
const forceRefresh = req . query . refresh === '1' || req . query . refresh === 'true' ;
const queryYtId =
req . query . youtubeId && /^[a-zA-Z0-9_-]{11}$/ . test ( String ( req . query . youtubeId ). trim ())
? String ( req . query . youtubeId ). trim ()
: null ;
const cacheKey = ` ${ resolvedUuid } __ ${ reqLang || 'auto' } __ ${ queryYtId || 'db' } ` ;
if ( ! forceRefresh ) {
const memHit = transcriptMemoryCache . get ( cacheKey );
if ( memHit ) {
const ttl = memHit . hasContent ? TRANSCRIPT_OK_TTL_MS : TRANSCRIPT_EMPTY_TTL_MS ;
if ( Date . now () - memHit . time < ttl ) {
return res . json ( memHit . payload );
}
}
}
const storageRoot = resolveTranscriptStorageRoot ();
const transcriptCacheDir = path . join ( storageRoot , 'cache' , 'transcripts' );
const diskCachePath = path . join (
transcriptCacheDir ,
` ${ resolvedUuid . replace ( /[^a-zA-Z0-9_-]/g , '_' ) } _ ${ reqLang || 'auto' } .json`
);
if ( ! forceRefresh && fs . existsSync ( diskCachePath )) {
try {
const st = await fs . promises . stat ( diskCachePath );
if ( Date . now () - st . mtimeMs < 7 * 24 * 60 * 60 * 1000 ) {
const diskPayload = JSON . parse ( await fs . promises . readFile ( diskCachePath , 'utf8' ));
if ( diskPayload && diskPayload . ok ) {
transcriptMemoryCache . set ( cacheKey , {
time : Date . now (),
hasContent :
( diskPayload . cues && diskPayload . cues . length > 0 ) ||
( diskPayload . chapters && diskPayload . chapters . length > 0 ),
payload : diskPayload
});
return res . json ( diskPayload );
}
}
} catch ( e ) {}
}
if ( inFlightTranscriptRequests . has ( cacheKey )) {
try {
const shared = await inFlightTranscriptRequests . get ( cacheKey );
return res . json ( shared );
} catch ( err ) {
return res . status ( 500 ). json ({ ok : false , error : err . message });
}
}
const workPromise = ( async () => {
let row = null ;
if ( /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i . test ( resolvedUuid )) {
const rows = await dbQuery (
`SELECT v.id, v.uuid, v.name, v.description, v.duration, v.language, vi."targetUrl"
FROM "video" v
LEFT JOIN "videoImport" vi ON vi."videoId" = v.id
WHERE v.uuid = $1::uuid
LIMIT 1` ,
[ resolvedUuid ]
);
if ( rows [ 0 ]) row = rows [ 0 ];
} else if ( /^\d+$/ . test ( rawParam )) {
const rows = await dbQuery (
`SELECT v.id, v.uuid, v.name, v.description, v.duration, v.language, vi."targetUrl"
FROM "video" v
LEFT JOIN "videoImport" vi ON vi."videoId" = v.id
WHERE v.id = $1::int
LIMIT 1` ,
[ parseInt ( rawParam , 10 )]
);
if ( rows [ 0 ]) row = rows [ 0 ];
}
if ( ! row && ! queryYtId ) {
return { ok : false , error : 'Video not found' , cues : [], chapters : [] };
}
let cues = [];
let chapters = row ? parseChaptersFromDescription ( row . description , Number ( row . duration || 0 )) : [];
let language = row && row . language ? row . language : null ;
let languageName = language || '' ;
let isAutoGenerated = false ;
let source = chapters . length > 0 ? 'description' : 'none' ;
let availableLanguages = [];
const youtubeId = queryYtId || extractYouTubeIdFromRow ( row );
// 1. Try local/remote PeerTube videoCaption rows if not requesting a different language
if ( row && row . id ) {
try {
const capRows = await dbQuery (
`SELECT id, language, filename, "fileUrl", "automaticallyGenerated"
FROM "videoCaption"
WHERE "videoId" = $1
ORDER BY CASE WHEN language = $2 THEN 0 WHEN language = 'tr' THEN 1 WHEN language = 'en' THEN 2 ELSE 3 END, "automaticallyGenerated" ASC, id ASC
LIMIT 6` ,
[ row . id , reqLang || 'tr' ]
);
for ( const cap of capRows ) {
availableLanguages . push ({
code : cap . language ,
name : cap . language ,
auto : Boolean ( cap . automaticallyGenerated )
});
}
const matchedCap = reqLang
? capRows . find ( c => String ( c . language || '' ). toLowerCase () === reqLang )
: capRows [ 0 ];
if ( matchedCap ) {
let capText = '' ;
const localCapPath = matchedCap . filename
? path . join ( storageRoot , 'captions' , path . basename ( matchedCap . filename ))
: '' ;
const cachedCapPath = matchedCap . filename
? path . join ( storageRoot , 'cache' , 'video-captions' , path . basename ( matchedCap . filename ))
: '' ;
if ( localCapPath && fs . existsSync ( localCapPath )) {
capText = await fs . promises . readFile ( localCapPath , 'utf8' );
} else if ( cachedCapPath && fs . existsSync ( cachedCapPath )) {
capText = await fs . promises . readFile ( cachedCapPath , 'utf8' );
} else if ( matchedCap . fileUrl ) {
capText = await fetchRemoteTextWithLimit ( matchedCap . fileUrl , 7000 );
}
const parsedLocalCues = parseSubtitlePayload ( capText );
if ( parsedLocalCues . length > 0 ) {
cues = parsedLocalCues ;
language = matchedCap . language ;
languageName = matchedCap . language ;
isAutoGenerated = Boolean ( matchedCap . automaticallyGenerated );
source = 'peertube-caption' ;
}
}
} catch ( capErr ) {}
}
// 2. Fallback to live YouTube subtitle & chapter extraction via yt-dlp when cues or chapters are still needed
if ( youtubeId && ( cues . length === 0 || chapters . length === 0 || reqLang )) {
try {
const ytdlpPath = resolveYtDlpPath ( logger , customYtDlpPath , peertubeHelpers );
const ytUrl = `https://www.youtube.com/watch?v= ${ youtubeId } ` ;
const rawJsonStr = await new Promise (( resolve , reject ) => {
execFile (
ytdlpPath ,
[ '--dump-single-json' , '--skip-download' , '--no-warnings' , '--socket-timeout' , '8' , ytUrl ],
{ maxBuffer : 14 * 1024 * 1024 , timeout : 15000 },
( err , stdout ) => {
if ( err || ! stdout ) return reject ( err || new Error ( 'Empty yt-dlp output' ));
resolve ( stdout );
}
);
});
const startIdx = rawJsonStr . indexOf ( '{' );
const endIdx = rawJsonStr . lastIndexOf ( '}' );
if ( startIdx !== - 1 && endIdx !== - 1 ) {
const ytInfo = JSON . parse ( rawJsonStr . substring ( startIdx , endIdx + 1 ));
if ( Array . isArray ( ytInfo . chapters ) && ytInfo . chapters . length > 0 ) {
chapters = ytInfo . chapters
. filter ( c => c && c . title && c . start_time !== undefined )
. map ( c => ({
title : String ( c . title ). trim (),
start : Math . max ( 0 , Math . floor ( Number ( c . start_time || 0 ))),
end : c . end_time !== undefined ? Math . floor ( Number ( c . end_time )) : null
}));
} else if ( chapters . length === 0 && ytInfo . description ) {
chapters = parseChaptersFromDescription ( ytInfo . description , Number ( ytInfo . duration || 0 ));
}
if ( cues . length === 0 || reqLang ) {
const picked = pickPreferredCaptionTrack ( ytInfo , reqLang , row && row . language );
if ( picked . availableLanguages . length > 0 ) {
availableLanguages = picked . availableLanguages ;
}
if ( picked . chosen && picked . chosen . track && picked . chosen . track . url ) {
const subText = await fetchRemoteTextWithLimit ( picked . chosen . track . url , 9000 );
const ytCues = parseSubtitlePayload ( subText );
if ( ytCues . length > 0 ) {
cues = ytCues ;
language = picked . chosen . code ;
languageName = ( picked . chosen . track && picked . chosen . track . name ) || picked . chosen . code ;
isAutoGenerated = picked . chosen . isAuto ;
source = picked . chosen . isAuto ? 'youtube-auto' : 'youtube-manual' ;
}
}
}
}
} catch ( ytErr ) {
if ( logger ) {
logger . warn ( `[YakTube Transcript] Extract warning for ${ resolvedUuid } : ${ ytErr . message } ` );
}
}
}
const payload = {
ok : true ,
uuid : ( row && row . uuid ) || resolvedUuid ,
title : ( row && row . name ) || '' ,
youtubeId : youtubeId || null ,
language ,
languageName ,
isAutoGenerated ,
source ,
cues ,
chapters ,
availableLanguages
};
const hasContent = cues . length > 0 || chapters . length > 0 ;
if ( transcriptMemoryCache . size > 400 ) transcriptMemoryCache . clear ();
transcriptMemoryCache . set ( cacheKey , {
time : Date . now (),
hasContent ,
payload
});
if ( hasContent ) {
try {
await fs . promises . mkdir ( transcriptCacheDir , { recursive : true });
await fs . promises . writeFile ( diskCachePath , JSON . stringify ( payload ), 'utf8' );
} catch ( diskErr ) {}
}
return payload ;
})(). finally (() => {
inFlightTranscriptRequests . delete ( cacheKey );
});
inFlightTranscriptRequests . set ( cacheKey , workPromise );
try {
const result = await workPromise ;
return res . json ( result );
} catch ( err ) {
return res . status ( 500 ). json ({ ok : false , error : err . message , cues : [], chapters : [] });
}
});
2026-10-03 10:45:58 +03:00
// 7. YouTube Original Comments Route
router . get ( '/youtube-comments' , async ( req , res ) => {
const videoId = ( req . query . id || req . query . v || '' ). trim ();
const continuationToken = ( req . query . token || '' ). trim ();
if ( ! videoId && ! continuationToken ) {
return res . status ( 400 ). json ({ error : 'Video ID or token is required' });
}
const baseContext = {
client : {
clientName : 'WEB' ,
clientVersion : '2.20260220.00.00' ,
hl : 'tr' ,
gl : 'TR'
}
};
try {
let token = continuationToken ;
if ( ! token ) {
const initRes = await postInnerTubeJSON ( 'https://www.youtube.com/youtubei/v1/next' , {
context : baseContext ,
videoId : videoId
});
const contents = initRes . contents ? . twoColumnWatchNextResults ? . results ? . results ? . contents || [];
const commentSection = contents . find ( t => t . itemSectionRenderer ? . targetId === 'comments-section' );
token =
commentSection ? . itemSectionRenderer ? . contents ? .[ 0 ] ? . continuationItemRenderer ? . continuationEndpoint
? . continuationCommand ? . token ;
}
if ( ! token ) {
return res . json ({ ok : true , comments : [], nextToken : null , total : 0 });
}
const commentRes = await postInnerTubeJSON ( 'https://www.youtube.com/youtubei/v1/next' , {
context : baseContext ,
continuation : token
});
const comments = [];
const mutations = commentRes . frameworkUpdates ? . entityBatchUpdate ? . mutations || [];
for ( const m of mutations ) {
const payload = m . payload ? . commentEntityPayload ;
if ( payload ) {
const author = payload . author ? . displayName || 'Kullanı cı ' ;
const authorThumb = payload . author ? . avatar ? . image ? . sources ? .[ 0 ] ? . url || '' ;
const content = payload . properties ? . content ? . content || '' ;
const published = payload . properties ? . publishedTime || '' ;
const likeCount = payload . toolbar ? . likeCountNotliked || '0' ;
comments . push ({
id : payload . properties ? . commentId ,
author ,
authorThumb ,
content ,
published ,
likeCount
});
}
}
let nextToken = null ;
const eps = commentRes . onResponseReceivedEndpoints || [];
for ( const ep of eps ) {
const items =
ep . reloadContinuationItemsCommand ? . continuationItems ||
ep . appendContinuationItemsAction ? . continuationItems ||
[];
for ( const it of items ) {
if ( it . continuationItemRenderer ) {
nextToken =
it . continuationItemRenderer . continuationEndpoint ? . continuationCommand ? . token ||
it . continuationItemRenderer . button ? . buttonRenderer ? . command ? . continuationCommand ? . token ;
}
}
}
return res . json ({
ok : true ,
comments ,
nextToken ,
count : comments . length
});
} catch ( err ) {
return res . status ( 500 ). json ({ error : 'Failed to fetch comments' , details : err . message });
}
});
// 8. User Search History Sync Route (Stores per-user history in plugin data directory on any PeerTube server)
function getHistoryFilePath () {
try {
if (
peertubeHelpers &&
peertubeHelpers . plugin &&
typeof peertubeHelpers . plugin . getDataDirectoryPath === 'function'
) {
return path . join ( peertubeHelpers . plugin . getDataDirectoryPath (), 'user-search-history.json' );
}
} catch ( e ) {}
return path . join ( process . cwd (), 'user-search-history.json' );
}
function readHistoryStore () {
try {
const fp = getHistoryFilePath ();
if ( fs . existsSync ( fp )) {
return JSON . parse ( fs . readFileSync ( fp , 'utf8' )) || {};
}
} catch ( e ) {}
return {};
}
function writeHistoryStore ( store ) {
try {
const fp = getHistoryFilePath ();
fs . writeFileSync ( fp , JSON . stringify ( store , null , 2 ), 'utf8' );
} catch ( e ) {}
}
async function resolveHistoryUserKey ( req , res ) {
try {
if ( peertubeHelpers && peertubeHelpers . user && typeof peertubeHelpers . user . getAuthUser === 'function' ) {
const u = await peertubeHelpers . user . getAuthUser ( res );
if ( u && ( u . username || u . email || u . id )) {
return String ( u . username || u . email || u . id ). toLowerCase ();
}
}
} catch ( e ) {}
const hdrUser = ( req . headers && ( req . headers [ 'x-user-username' ] || req . headers [ 'x-user-email' ])) || '' ;
return hdrUser ? String ( hdrUser ). trim (). toLowerCase () : null ;
}
router . all ( '/search-history' , async ( req , res ) => {
const userKey = await resolveHistoryUserKey ( req , res );
if ( ! userKey ) {
return res . json ({ status : 'success' , history : [] });
}
const store = readHistoryStore ();
const currentList = Array . isArray ( store [ userKey ]) ? store [ userKey ] : [];
if ( req . method === 'GET' ) {
return res . json ({ status : 'success' , history : currentList });
}
if ( req . method === 'POST' ) {
const q = (( req . body && req . body . query ) || '' ). trim ();
if ( q && q . length >= 2 ) {
const filtered = currentList . filter ( item => String ( item ). toLowerCase () !== q . toLowerCase ());
filtered . unshift ( q );
2026-10-19 18:11:43 +03:00
store [ userKey ] = filtered . slice ( 0 , 20 );
2026-10-03 10:45:58 +03:00
writeHistoryStore ( store );
}
return res . json ({ status : 'success' , history : store [ userKey ] || currentList });
}
if ( req . method === 'DELETE' ) {
2026-10-19 18:11:43 +03:00
const target = (( req . body && req . body . query ) || ( req . query && req . query . query ) || '' ). trim ();
if ( target ) {
const filtered = currentList . filter ( item => String ( item ). toLowerCase () !== target . toLowerCase ());
store [ userKey ] = filtered ;
writeHistoryStore ( store );
return res . json ({ status : 'success' , history : filtered });
}
2026-10-03 10:45:58 +03:00
delete store [ userKey ];
writeHistoryStore ( store );
return res . json ({ status : 'success' , history : [] });
}
return res . json ({ status : 'success' , history : currentList });
});
2026-08-29 12:42:36 +03:00
}
2026-10-03 11:14:03 +03:00
async function unregister () {
if ( pruneIntervalTimer ) {
clearInterval ( pruneIntervalTimer );
pruneIntervalTimer = null ;
}
}
2026-08-29 12:42:36 +03:00
module . exports = {
register ,
unregister
};