Adding files and adding cli
Took 1 hour 8 minutes
This commit is contained in:
parent
a32147c9a5
commit
186ca6c85a
125
AudioVideoHandler.php
Normal file
125
AudioVideoHandler.php
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class AudioVideoHandler
|
||||||
|
{
|
||||||
|
private $directories;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
echo "[INFO] Fetching directories...";
|
||||||
|
$this->directories = glob( DOWNLOAD_PATH . '/*', GLOB_ONLYDIR );
|
||||||
|
echo "OK\n";
|
||||||
|
|
||||||
|
$dir = realpath( DOWNLOAD_PATH . '/' . readline( "Artist: " ) );
|
||||||
|
$dir = str_replace( "\\", '/', $dir );
|
||||||
|
echo "[INFO] Loading results...\n";
|
||||||
|
echo $dir;
|
||||||
|
$matches = 0;
|
||||||
|
foreach ( $this->directories as $index => $_dir )
|
||||||
|
{
|
||||||
|
|
||||||
|
if ( ( $lev = levenshtein( $_dir, $dir ) ) < 3 )
|
||||||
|
{
|
||||||
|
echo "[MATCH#$lev] Did you mean: $_dir? [$index]\n";
|
||||||
|
$matches++;
|
||||||
|
}
|
||||||
|
#echo $lev . "\n";
|
||||||
|
|
||||||
|
}
|
||||||
|
if ( $matches === 0 )
|
||||||
|
{
|
||||||
|
echo "[INFO] No matches found!\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$index = readline( "Name the index-number: " );
|
||||||
|
if ( isset( $this->directories[$index] ) === false )
|
||||||
|
{
|
||||||
|
echo "[ERROR] Index is invalid!\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$dir = $this->directories[$index];
|
||||||
|
|
||||||
|
|
||||||
|
echo "[INFO] Scanning Artist directory...";
|
||||||
|
$files = scandir( $dir );
|
||||||
|
echo "OK\n";
|
||||||
|
|
||||||
|
if ( count( $files ) < 1 )
|
||||||
|
{
|
||||||
|
echo "[WARNING] Directory $dir is empty!\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$_titleOrFull = readline( "Do you want to download all songs or just an explicit title?[A/t]: " );
|
||||||
|
if ( stripos( $_titleOrFull, 't' ) !== false )
|
||||||
|
{
|
||||||
|
echo "\n";
|
||||||
|
// Only one
|
||||||
|
$song = readline( "Enter the Song name: " );
|
||||||
|
echo "[INFO] Searching...\n";
|
||||||
|
$matches = 0;
|
||||||
|
$artist = basename( $dir );
|
||||||
|
foreach ( $files as $index => $name )
|
||||||
|
{
|
||||||
|
$name = str_replace( array( '.txt', '.mp3', '.mp4' ), '', $name );
|
||||||
|
if ( ( $lev = levenshtein( $name, $artist . ' - ' . $song ) ) < 4 )
|
||||||
|
{
|
||||||
|
echo "[MATCH#$lev] Did you mean: $name? [$index]\n";
|
||||||
|
$matches++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ( $matches === 0 )
|
||||||
|
{
|
||||||
|
echo "[WARNING] No matches found!\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$index = readline( "Name the index-number: " );
|
||||||
|
if ( isset( $files[$index] ) === false )
|
||||||
|
{
|
||||||
|
echo "[ERROR] Index is invalid!\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$files[$index] = str_replace( array( '.txt', '.mp3', '.mp4' ), '', $files[$index] );
|
||||||
|
$this->download( $files[$index] );
|
||||||
|
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
echo "\n";
|
||||||
|
// All files
|
||||||
|
echo "[INFO] Downloading " . count( $files ) . " songs...\n";
|
||||||
|
foreach ( $files as $file )
|
||||||
|
{
|
||||||
|
if ( $file === '.' || $file === '..' || strpos( $file, '.mp3' ) !== false || strpos( $file, '.mp4' ) !== false )
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$file = str_replace( array( '.txt', '.mp3', '.mp4' ), '', $file );
|
||||||
|
|
||||||
|
$this->download( $file );
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function download( $file )
|
||||||
|
{
|
||||||
|
echo "[INFO] Downloading $file...";
|
||||||
|
exec( 'youtube-dl.exe --extract-audio --no-warnings -q --embed-thumbnail --audio-format mp3 --recode-video mp4 --audio-quality 4 -k -o downloaded.%(ext)s "ytsearch: ' . $file . '"' );
|
||||||
|
echo "OK\n";
|
||||||
|
list( $artist, $title ) = explode( ' - ', $file );
|
||||||
|
if ( file_exists( './downloaded.f135.mp4' ) === true )
|
||||||
|
{
|
||||||
|
rename( "./downloaded.f135.mp4", DOWNLOAD_PATH . '/' . $artist . "/$artist - $title.mp4" );
|
||||||
|
echo "[SUCCESS] MP4 saved!\n";
|
||||||
|
}
|
||||||
|
if ( file_exists( './downloaded.mp3' ) === true )
|
||||||
|
{
|
||||||
|
rename( "./downloaded.mp3", DOWNLOAD_PATH . '/' . $artist . "/$artist - $title.mp3" );
|
||||||
|
echo "[SUCCESS] MP3 saved!\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
array_map( 'unlink', glob( "./downloaded.*" ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
124
Downloader.php
Normal file
124
Downloader.php
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
<?php
|
||||||
|
class Downloader
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param int $a_id
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function requestSong( int $a_id )
|
||||||
|
{
|
||||||
|
if( ( $out = $this->downloadFile( $a_id ) ) === false )
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$this->saveFile( $out );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $a_file_id
|
||||||
|
*
|
||||||
|
* @return bool|string
|
||||||
|
*/
|
||||||
|
private function downloadFile( $a_file_id )
|
||||||
|
{
|
||||||
|
echo "[INFO] Requesting file id $a_file_id...";
|
||||||
|
$ch = curl_init();
|
||||||
|
|
||||||
|
curl_setopt( $ch, CURLOPT_URL, "http://usdb.animux.de/index.php?link=gettxt&id=$a_file_id" );
|
||||||
|
curl_setopt( $ch, CURLOPT_POST, 1 );
|
||||||
|
curl_setopt( $ch, CURLOPT_COOKIEFILE, DOWNLOAD_PATH . '/cookies.txt' );
|
||||||
|
curl_setopt( $ch, CURLOPT_COOKIEJAR, DOWNLOAD_PATH . '/cookies.txt' );
|
||||||
|
// In real life you should use something like:
|
||||||
|
curl_setopt( $ch, CURLOPT_POSTFIELDS,
|
||||||
|
http_build_query(
|
||||||
|
array(
|
||||||
|
'wd' => 1,
|
||||||
|
) ) );
|
||||||
|
|
||||||
|
// Receive server response ...
|
||||||
|
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
|
||||||
|
|
||||||
|
$out = curl_exec( $ch );
|
||||||
|
|
||||||
|
if( strpos( $out, 'Datensatz nicht gefunden' ) !== false )
|
||||||
|
{
|
||||||
|
echo "Not found - Skip\n";
|
||||||
|
curl_close( $ch );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
curl_close( $ch );
|
||||||
|
echo "OK\n";
|
||||||
|
|
||||||
|
return utf8_decode( $out );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $a_input
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function saveFile( $a_input ): bool
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
$out = strpos( $a_input, '<textarea' ) + 37;
|
||||||
|
if( $out === false )
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$_to = strpos( $a_input, '</textarea>' );
|
||||||
|
$out = substr( $a_input, $out, $_to - $out );
|
||||||
|
|
||||||
|
|
||||||
|
$artist = str_replace( array( '#ARTIST:', '/' ), array( '', '-' ), strtok( $out, "\n" ) );
|
||||||
|
$artist = preg_replace( "/[^a-zA-Z0-9_.!äöü\s-]/m/u", "", $artist );
|
||||||
|
|
||||||
|
$title = str_replace( array( '#TITLE:', '/' ), array( '', '-' ), strtok( "\n" ) );
|
||||||
|
$title = preg_replace( "/[^a-zA-Z0-9_.!äöü\s-]/m/u", "", $title );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
echo "[INFO] Found $artist - $title\n";
|
||||||
|
if( is_dir( DOWNLOAD_PATH . '/' . $artist . '/' ) === false )
|
||||||
|
{
|
||||||
|
echo "[INFO] Creating Artist-Directory...";
|
||||||
|
if ( !mkdir( $concurrentDirectory = DOWNLOAD_PATH . '/' . $artist . '/' ) && !is_dir( $concurrentDirectory ) )
|
||||||
|
{
|
||||||
|
throw new Exception( 'Cannot create the directory for the Artist' );
|
||||||
|
}
|
||||||
|
echo "OK";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
echo "[INFO] Artist-Directory exists\n";
|
||||||
|
|
||||||
|
}
|
||||||
|
if( file_exists( DOWNLOAD_PATH . '/' . $artist . '/' . $artist . ' - ' . $title . '.txt' ) === true )
|
||||||
|
{
|
||||||
|
echo "[INFO] Already downloaded - Skip\n";
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if( ( $file = fopen( DOWNLOAD_PATH . "/$artist/$artist - $title.txt", 'w+' ) ) === false )
|
||||||
|
{
|
||||||
|
throw new RuntimeException( "$artist - $title *** Cannot open file handler" );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( fwrite( $file, $out ) === false )
|
||||||
|
{
|
||||||
|
throw new RuntimeException( "$artist - $title *** Cannot write to file" );
|
||||||
|
}
|
||||||
|
fclose( $file );
|
||||||
|
|
||||||
|
echo "[SUCCESS] Downloaded \"$title - $artist\"\n";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
10
config.inc.php
Normal file
10
config.inc.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
define( 'DOWNLOAD_PATH', 'C:/Users/hoppt/Documents/Karaoke' );
|
||||||
|
mb_internal_encoding("UTF-8");
|
||||||
|
|
||||||
|
error_reporting(0);
|
||||||
|
|
||||||
|
|
||||||
|
require_once 'AudioVideoHandler.php';
|
||||||
|
require_once 'Downloader.php';
|
109
karaoke-management.php
Normal file
109
karaoke-management.php
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'config.inc.php';
|
||||||
|
|
||||||
|
|
||||||
|
for ($i = 0; $i < 50; $i++)
|
||||||
|
{
|
||||||
|
echo "\r\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
function main()
|
||||||
|
{
|
||||||
|
|
||||||
|
$cmd = readline( "KM@CLI# " );
|
||||||
|
$cmd = explode( ' ', $cmd );
|
||||||
|
switch( $cmd[0] )
|
||||||
|
{
|
||||||
|
case 'help':
|
||||||
|
help( $cmd );
|
||||||
|
break;
|
||||||
|
case 'getfile':
|
||||||
|
if( !isset( $cmd[1] ) )
|
||||||
|
{
|
||||||
|
echo "Wrong usage, please use help for help.\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$downloader = new Downloader();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$downloader->requestSong( $cmd[1] );
|
||||||
|
} catch ( RuntimeException $e )
|
||||||
|
{
|
||||||
|
echo "An error occured while processing the download.\n";
|
||||||
|
echo $e->getMessage() . "\n";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'downloadmusic':
|
||||||
|
echo "This function has an old CLI! Opening CLI...\n-----------\n";
|
||||||
|
$audiovideo = new AudioVideoHandler();
|
||||||
|
echo "CLI exited. Continue with new CLI...\n";
|
||||||
|
break;
|
||||||
|
case 'setsid':
|
||||||
|
if( !isset( $cmd[1] ) )
|
||||||
|
{
|
||||||
|
echo "Wrong usage, please use help for help.\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
echo "Function not completed yet!\n";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
echo "Unkown command. Use help for help.\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
### Global System
|
||||||
|
while( true )
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
main();
|
||||||
|
} catch( Exception $e )
|
||||||
|
{
|
||||||
|
echo "\n\n\nWhoops! - An error occured.";
|
||||||
|
echo "\n" . $e->getMessage();
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
function help( $a_cmd )
|
||||||
|
{
|
||||||
|
echo "--- Help ---\n";
|
||||||
|
if( isset( $a_cmd[1] ) )
|
||||||
|
{
|
||||||
|
switch( $a_cmd[1] )
|
||||||
|
{
|
||||||
|
case 'getfile':
|
||||||
|
echo "getfile id(int)";
|
||||||
|
break;
|
||||||
|
case 'downloadmusic':
|
||||||
|
echo "downloadmusic [artist(string)] [title(title)]";
|
||||||
|
break;
|
||||||
|
case 'setsid':
|
||||||
|
echo "setsid id(string)";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
echo "help for this command not found!";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
echo "
|
||||||
|
getfile - Download beatmap with an id
|
||||||
|
downloadmusic - Download mp3 and mp4 for an beatmap
|
||||||
|
help - Show this help
|
||||||
|
setsid - Set the php session id
|
||||||
|
";
|
||||||
|
}
|
||||||
|
echo "\n--- Help ---\n";
|
||||||
|
|
||||||
|
}
|
BIN
youtube-dl.exe
Normal file
BIN
youtube-dl.exe
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user