Skip to content

Instantly share code, notes, and snippets.

@tunglam14
Created February 10, 2013 12:50
Show Gist options
  • Save tunglam14/4749500 to your computer and use it in GitHub Desktop.
Save tunglam14/4749500 to your computer and use it in GitHub Desktop.
get download link from nhaccuatui - lamdt@familug.org
<?
// Get arg
$arguments = getopt("h::u:d::");
// For help
if(isset($arguments['h']) or empty($arguments))
{
$html = "Options: \n\r
-h: \t Help \n\r
-u: \t Nhaccuatui URL \n\r
--d: \t Enable download \n\r
Ex: php url.php -u http://www.nhaccuatui.com/bai-hat/our-same-word-orchestra-ver-yiruma.U6_h_PbuBH.html -l /home/lamdt/Music -d\n\r";
echo $html; exit(0);
}
// Get url from arg
$url = $arguments['u'];
// validate url
if(preg_match("#^http://www.nhaccuatui.com.+#", $url) and filter_var($url, FILTER_VALIDATE_URL))
{
// IF url is valid
// get key of this song
$ex = explode('.', $url);
$song_key = $ex[count($ex) - 2];
// get content from api
$api_result = file_get_contents("http://www.nhaccuatui.com/download/song/$song_key");
$r = json_decode($api_result);
if($r == null)
{
echo "Could not connect nhaccuatui";
exit(0);
}
// check error_code
if($r->error_code != 0)
{
echo "ERROR: ".$r->error_message;
exit(0);
}
// get download link
$link = $r->data->stream_url;
echo "
Download Link:
\n\r
".$link."
\n\r
=====================================\n\r";
// if download enable
if(isset($arguments['d']))
{
echo "Download start..................";
$d1 = exec("wget $link");
echo $d1;
}
}
else
{
echo "Only for http://www.nhaccuatui.com/";
}
echo "\n\r";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment