Last active
February 20, 2016 13:41
-
-
Save x-magic/158b99848c3c0d62a4a3 to your computer and use it in GitHub Desktop.
Download lyrics using geci.me's API @ https://github.com/solos/geci.me-api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(0); | |
ini_set('display_errors', 0); | |
/* SQL for `lyrics` table | |
CREATE TABLE `lyrics` ( | |
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, | |
`title` varchar(256) NOT NULL, | |
`artist` varchar(256) NOT NULL, | |
`lrc` varchar(256), | |
`lyrics` text | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
*/ | |
$conn = new mysqli("localhost", "lyrics", "lyrics", "lyrics"); | |
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error); | |
$lyricsbase = "http://geci.me/api/lyric"; | |
$result = $conn->query("SELECT `id`, `title`, `artist` FROM `lyrics` WHERE `lyrics` IS NULL"); | |
if ($result->num_rows > 0) { | |
while($row = $result->fetch_assoc()) { | |
echo "Processing ".str_pad($row['id'], 3, "0", STR_PAD_LEFT); | |
$url = implode("/", array($lyricsbase, urlencode($row['title']), urlencode($row['artist']))); | |
echo " - Getting JSON, "; | |
$json = json_decode(file_get_contents($url)); | |
$resourcecount = count($json->result); | |
if ($resourcecount > 0) { | |
echo "Found"; | |
for ($i = 0; $i < $resourcecount; $i++) { | |
$getlyrics = false; | |
$resourceurl = $json->result[0]->lrc; | |
if ($lyrics = file_get_contents($resourceurl)) { | |
echo " - Got lyrics"; | |
$lyricstrim = trim(preg_replace("/\[.*\]/", "", $lyrics)); | |
$stmt = $conn->prepare("UPDATE `lyrics` SET `lrc`=?,`lyrics`=? WHERE `id`=?"); | |
$stmt->bind_param("ssi", $resourceurl, $lyricstrim, $row['id']); | |
if ($stmt->execute()) echo " - lyrics inserted.\n"; | |
$stmt->close(); | |
$getlyrics = true; | |
break; | |
} else echo ",Fail"; | |
} | |
if ($getlyrics == false) echo " - no lyrics for [".$row['title']."] by [".$row['artist']."]!\n"; | |
} else echo "no lyrics for [".$row['title']."] by [".$row['artist']."]!\n"; | |
} | |
} else echo "No songs in database"; | |
$conn->close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment