Skip to content

Commit

Permalink
Merge pull request #34 from akashrchandran/develop
Browse files Browse the repository at this point in the history
Fixed Implicit conversion
  • Loading branch information
akashrchandran authored Jan 18, 2024
2 parents f5fa223 + 080c0c6 commit f553cbd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
68 changes: 33 additions & 35 deletions api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,45 @@

require_once __DIR__ . '/../vendor/autoload.php';

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Content-Type: application/json');
header( 'Access-Control-Allow-Origin: *' );
header( 'Access-Control-Allow-Methods: GET, POST, OPTIONS' );
header( 'Access-Control-Allow-Headers: Content-Type' );
header( 'Content-Type: application/json' );

$trackid = $_GET['trackid'] ?? null;
$url = $_GET['url'] ?? null;
$format = $_GET['format'] ?? null;
$trackid = $_GET[ 'trackid' ] ?? null;
$url = $_GET[ 'url' ] ?? null;
$format = $_GET[ 'format' ] ?? null;

$re = '~[\bhttps://open.\b]*spotify[\b.com\b]*[/:]*track[/:]*([A-Za-z0-9]+)~';

if (!$trackid && !$url) {
http_response_code(400);
$reponse = json_encode(["error" => true, "message" => "url or trackid parameter is required!", "usage" => "https://github.com/akashrchandran/spotify-lyrics-api"]);
echo $reponse;
return;
if ( !$trackid && !$url ) {
http_response_code( 400 );
$reponse = json_encode( [ 'error' => true, 'message' => 'url or trackid parameter is required!', 'usage' => 'https://github.com/akashrchandran/spotify-lyrics-api' ] );
echo $reponse;
return;
}
if ($url) {
preg_match($re, $url, $matches, PREG_OFFSET_CAPTURE, 0);
$trackid = $matches[1][0];
if ( $url ) {
preg_match( $re, $url, $matches, PREG_OFFSET_CAPTURE, 0 );
$trackid = $matches[ 1 ][ 0 ];
}
$spotify = new SpotifyLyricsApi\Spotify(getenv( 'SP_DC' ));
$spotify = new SpotifyLyricsApi\Spotify( getenv( 'SP_DC' ) );
$spotify->checkTokenExpire();
$reponse = $spotify -> getLyrics(track_id: $trackid);
echo make_reponse($reponse, $format);
$reponse = $spotify->getLyrics( track_id: $trackid );
echo make_reponse( $reponse, $format );

function make_reponse($response, $format)
{
global $spotify;
$temp = json_decode($response, true)['lyrics'];
if (!$temp) {
http_response_code(404);
return json_encode(["error" => true, "message" => "lyrics for this track is not available on spotify!"]);
}
if ($format == 'lrc') {
$lines = $spotify -> getLrcLyrics($temp["lines"]);
$response = ["error" => false, "syncType" => $temp["syncType"], "lines" => $lines];
} else {
$response = ["error" => false, "syncType" => $temp["syncType"], "lines" => $temp["lines"]];
}
return json_encode($response);
function make_reponse( $response, $format )
{
global $spotify;
$temp = json_decode( $response, true )[ 'lyrics' ];
if ( !$temp ) {
http_response_code( 404 );
return json_encode( [ 'error' => true, 'message' => 'lyrics for this track is not available on spotify!' ] );
}
if ( $format == 'lrc' ) {
$lines = $spotify->getLrcLyrics( $temp[ 'lines' ] );
$response = [ 'error' => false, 'syncType' => $temp[ 'syncType' ], 'lines' => $lines ];
} else {
$response = [ 'error' => false, 'syncType' => $temp[ 'syncType' ], 'lines' => $temp[ 'lines' ] ];
}
return json_encode( $response );
}

?>
2 changes: 1 addition & 1 deletion src/Spotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function getLrcLyrics($lyrics): array
*/
function formatMS($milliseconds): string
{
$lrc_timetag = sprintf('%02d:%02d.%02d', ($milliseconds / 1000) / 60, ($milliseconds / 1000) % 60, ($milliseconds % 1000) / 10);
$lrc_timetag = sprintf('%02d:%02d.%02d', intval(($milliseconds / 1000) / 60), intval(($milliseconds / 1000) % 60), intval(($milliseconds % 1000) / 10));
return $lrc_timetag;
}
}

0 comments on commit f553cbd

Please sign in to comment.