Created
November 22, 2021 08:21
-
-
Save mysteriouss/083919b99d8bf8095698702bd8323721 to your computer and use it in GitHub Desktop.
weibo video downloader
This file contains 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 | |
date_default_timezone_set('Asia/Shanghai'); | |
if(!isset($argv)){ | |
echo 'Usage: php downloader.php $url' . PHP_EOL; | |
return; | |
} | |
$dir = __DIR__ ; | |
$ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.3 Safari/605.1.15'; | |
foreach($argv as $url){ | |
if($url === 'downloader.php'){ | |
continue; | |
} | |
$url_components = explode('/',$url); | |
$url_id = end($url_components); | |
$url = 'https://weibo.com/ajax/statuses/show?id=' . $url_id; | |
echo $url . PHP_EOL; | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 3, | |
CURLOPT_TIMEOUT => 10, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "GET", | |
CURLOPT_HTTPHEADER => array( | |
"User-Agent: $ua", | |
"Cache-Control: no-cache", | |
), | |
)); | |
$response = curl_exec($curl); | |
$err = curl_error($curl); | |
curl_close($curl); | |
if ($err) { | |
echo "cURL Error #:" . $err . PHP_EOL; | |
continue; | |
} | |
$data = json_decode($response); | |
//print_r($data); | |
if(!isset($data->page_info->media_info->playback_list)){ | |
echo "Data Error #: no page_info->media_info->playback_list" . PHP_EOL; | |
continue; | |
} | |
$playback_list = $data->page_info->media_info->playback_list; | |
if(!is_array($playback_list) || !count($playback_list)){ | |
echo "Data Error #: page_info->media_info->playback_list empty" . PHP_EOL; | |
continue; | |
} | |
$playback = $playback_list[0]; | |
if(!isset($playback->play_info->url)){ | |
echo "Data Error #: page_info->media_info->playback_list first no url" . PHP_EOL; | |
continue; | |
} | |
$mp4_url = $playback->play_info->url; | |
echo $mp4_url . PHP_EOL; | |
$title = $url_id . '.mp4'; | |
// customization | |
// if(isset($data->page_info->media_info->kol_title)){ | |
// $title = md5($data->page_info->media_info->kol_title) . '-' . $title; | |
// } | |
if(file_exists("$dir/$title")){ | |
continue; | |
} | |
exec("wget '$mp4_url' -O '$dir/$title'"); | |
//break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment