chinese-voice/download.php

34 lines
1.1 KiB
PHP

<?php
include 'vendor/autoload.php';
$pinyin = json_decode(file_get_contents('pinyin.json'), true);
$not_exists = [];
foreach ($pinyin as $py) {
if (!file_exists(__DIR__ . "/data/{$py}.mp3")) {
$not_exists[] = $py.'.mp3';
}
}
$http = new \GuzzleHttp\Client([
'base_uri' => 'http://appcdn.fanyi.baidu.com/zhdict/mp3/',
'timeout' => 10.0,
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
]
]);
// $chars = explode(',',file_get_contents(__DIR__.'/chinese.txt'));
foreach ($not_exists as $py) {
echo "download {$py} ";
try{
$response = $http->request('GET', $py);
if ($response->getStatusCode() == 404) {
echo "404 \n";
continue;
}
$stream = $response->getBody();
file_put_contents(__DIR__ . '/data/'.$py, $stream);
echo "success \n";
}catch (Exception $e){
echo $e->getMessage()." \n";
}
}