From 0312c57aceb84c9b7c5cdc8796f83f9250c0b43d Mon Sep 17 00:00:00 2001 From: Tobias Hopp Date: Sun, 12 Jul 2020 18:25:18 +0200 Subject: [PATCH] Bugfix when artist was found direct Bugfix when son was found direct Bugfix when user entered a search Took 1 hour 47 minutes --- .gitignore | 1 + AudioVideoHandler.php | 23 ++++++++++++++++++----- Downloader.php | 18 ++++++++---------- start.bat | 1 + 4 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 .gitignore create mode 100644 start.bat diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bb7423d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/sortTest.php diff --git a/AudioVideoHandler.php b/AudioVideoHandler.php index 40b886b..1e8c298 100644 --- a/AudioVideoHandler.php +++ b/AudioVideoHandler.php @@ -16,19 +16,20 @@ class AudioVideoHandler return; } - $dir = realpath( DOWNLOAD_PATH . '/' . $artist ); - $dir = str_replace( "\\", '/', $dir ); + $searchDir = DOWNLOAD_PATH . '/' . $artist; + $searchDir = str_replace( "\\", '/', $searchDir ); echo "[INFO] Loading results..."; $matches = 0; - $index = 0; + $_indexmatch = 0; $_found = ''; foreach ( $this->directories as $index => $_dir ) { - if ( ( $lev = levenshtein( $_dir, $dir ) ) < 3 ) + if ( ( $lev = levenshtein( $_dir, $searchDir ) ) < 3 ) { $_found .= "$index | $_dir\n"; + $_indexmatch = $index; $matches++; } #echo $lev . "\n"; @@ -48,16 +49,23 @@ class AudioVideoHandler echo $_found; $index = readline( "Name the index-number: " ); } + else + { + $index = $_indexmatch; + } if ( isset( $this->directories[$index] ) === false ) { echo "[ERROR] Index is invalid!\n"; goto indexChoose; } + $dir = $this->directories[$index]; echo "[INFO] Scanning Artist directory..."; $files = scandir( $dir ); + unset( $files[0], $files[1] ); + echo "OK\n"; if ( count( $files ) < 1 ) @@ -90,7 +98,7 @@ class AudioVideoHandler $matches = 0; $artist = basename( $dir ); $_found = ''; - $index = 0; + $_indexmatch = 0; foreach ( $files as $index => $name ) { $name = str_replace( array( '.txt', '.mp3', '.mp4' ), '', $name ); @@ -98,6 +106,7 @@ class AudioVideoHandler { $_found .= "$index | $song\n"; $matches++; + $_indexmatch = $index; } } if ( $matches === 0 ) @@ -111,6 +120,10 @@ class AudioVideoHandler echo $_found; $index = readline( "Name the index-number: " ); } + else + { + $index = $_indexmatch; + } if ( isset( $files[$index] ) === false ) { diff --git a/Downloader.php b/Downloader.php index 2fc9563..64e14ab 100644 --- a/Downloader.php +++ b/Downloader.php @@ -11,8 +11,9 @@ class Downloader } } - private function login( ) + private function login( ): void { + echo "[LOGIN] Logging in..."; $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, "http://usdb.animux.de/index.php?&link=home" ); @@ -30,15 +31,10 @@ class Downloader // 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 ); + self::$loggedIn = true; echo "OK\n"; } @@ -46,15 +42,17 @@ class Downloader * @param int $a_id * * @return bool + * * @throws Exception */ - public function requestSong( int $a_id ): ?bool + public function requestSong( int $a_id ): bool { if( ( $out = $this->downloadFile( $a_id ) ) === false ) { return false; } $this->saveFile( $out ); + return true; } /** @@ -145,7 +143,7 @@ class Downloader return false; } - if( ( $file = fopen( DOWNLOAD_PATH . "/$artist/$artist - $title.txt", 'w+' ) ) === false ) + if( ( $file = fopen( DOWNLOAD_PATH . "/$artist/$artist - $title.txt", 'wb+' ) ) === false ) { throw new RuntimeException( "$artist - $title *** Cannot open file handler" ); } diff --git a/start.bat b/start.bat new file mode 100644 index 0000000..199852b --- /dev/null +++ b/start.bat @@ -0,0 +1 @@ +php C:\Users\hoppt\PhpstormProjects\karaoke-management\karaoke-management.php \ No newline at end of file