You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

285 lines
15 KiB

<?php
namespace App\Services;
use App\AcfunVideo;
use Illuminate\Support\Arr;
use Log;
use QL\QueryList;
class AcfunService
{
private $CollectionUrl = "https://www.acfun.cn/rest/pc-direct/favorite/dougaList";
private $upBaseUrl = "https://m.acfun.cn/upPage/";
public function queryCollectionVideos()
{
for ($i = 1; $i < 100; $i++) {
sleep(5);
$param = "perpage=100&page=" . $i;
$result = $this->request($this->CollectionUrl, $param);
$parsedResult = json_decode($result, true);
if(count(Arr::get($parsedResult, "favoriteList", [])) > 0) {
foreach ($parsedResult["favoriteList"] as $item) {
AcfunVideo::firstOrCreate(["content_id" => $item["contentId"]],
[
"title" => $item["contentTitle"],
"from_type" => 1,
"from_collection_name" => "默认",
"from_up_name" => $item["userName"],
"from_up_user_id" => $item["userId"]
]);
}
}
}
}
public function request($url, $param)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>$param,
CURLOPT_HTTPHEADER => array(
"authority: www.acfun.cn",
"pragma: no-cache",
"cache-control: no-cache",
"accept: */*",
"x-requested-with: XMLHttpRequest",
"user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36",
"content-type: application/x-www-form-urlencoded; charset=UTF-8",
"origin: https://www.acfun.cn",
"sec-fetch-site: same-origin",
"sec-fetch-mode: cors",
"sec-fetch-dest: empty",
"referer: https://www.acfun.cn/member/",
"accept-language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6,ja;q=0.5",
"cookie: _did=web_32586178239268B; sign_remind=1; ac__avi=1010934062045114530882ab2cd25b90ef370ea22b151084981cf6c2c6febafe3a364146315ef65213; uuid=d7fdb48c886459236faa794ffafbfbb0; analytics=GA1.2.626694313.1592365512; lsv_js_player_v2_main=62b719; csrfToken=TnNz3lG1IfA-93kw4M-Px06m; session_id=63965769975BFD30; webp_supported=%7B%22lossy%22%3Atrue%2C%22lossless%22%3Atrue%2C%22alpha%22%3Atrue%2C%22animation%22%3Atrue%7D; Hm_lvt_2af69bc2b378fb58ae04ed2a04257ed1=1592481541,1592489023,1592797550,1592976124; lsv_js_player_v1_main=f2c6e6; stochastic=NWFlaDB1ZjlmNXE%3D; acPasstoken=ChVpbmZyYS5hY2Z1bi5wYXNzdG9rZW4ScJrFeEdlHcrvoHjYpzy_V7pAPxxMhNmo45RlMccpB84XwmIx9rJocLwXSPnJu4btgtqVGa04P9AIMBNdQknDd4QRTbjBp5aoQgis-Q88BjMvqVTGHAjG04W7zjswa0ZC5k5sdJCeTsLJKEiEsE8EY90aEhwsbiM70HUviQANoYC6Iptv6yIg0X6u8ikzzwuDe1VMKWNNYXWwFR81YKYQNq9P4gsk5oMoBTAB; auth_key=572984; ac_username=Nicksxs; acPostHint=1cc641584039a19bc0941d00e8eff73bd4b0; ac_userimg=https%3A%2F%2Fimgs.aixifan.com%2Fstyle%2Fimage%2F201907%2FuQ5Vc06d3HSVTjY3VZocT81X40FByVpw.jpg; clientlanguage=zh_CN; safety_id=AAG%2B9AbVZS6fTHVxwLtsI9Jl; notice_status=1; online_status=0; userGroupLevel=1; checkMobile=1; checkEmail=1; cur_req_id=1296724223CA632E_self_0fb782b0474cbe335574779cf53ed145; cur_group_id=1296724223CA632E_self_0fb782b0474cbe335574779cf53ed145_0; Hm_lpvt_2af69bc2b378fb58ae04ed2a04257ed1=1592976174"
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
public function onlyRequest()
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.acfun.cn/rest/pc-direct/favorite/dougaList",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "perpage=100&page=1",
CURLOPT_HTTPHEADER => array(
"authority: www.acfun.cn",
"pragma: no-cache",
"cache-control: no-cache",
"accept: */*",
"x-requested-with: XMLHttpRequest",
"user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36",
"content-type: application/x-www-form-urlencoded; charset=UTF-8",
"origin: https://www.acfun.cn",
"sec-fetch-site: same-origin",
"sec-fetch-mode: cors",
"sec-fetch-dest: empty",
"referer: https://www.acfun.cn/member/",
"accept-language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6,ja;q=0.5",
"cookie: _did=web_32586178239268B; sign_remind=1; ac__avi=1010934062045114530882ab2cd25b90ef370ea22b151084981cf6c2c6febafe3a364146315ef65213; uuid=d7fdb48c886459236faa794ffafbfbb0; analytics=GA1.2.626694313.1592365512; lsv_js_player_v2_main=62b719; csrfToken=TnNz3lG1IfA-93kw4M-Px06m; session_id=63965769975BFD30; webp_supported=%7B%22lossy%22%3Atrue%2C%22lossless%22%3Atrue%2C%22alpha%22%3Atrue%2C%22animation%22%3Atrue%7D; Hm_lvt_2af69bc2b378fb58ae04ed2a04257ed1=1592481541,1592489023,1592797550,1592976124; lsv_js_player_v1_main=f2c6e6; stochastic=NWFlaDB1ZjlmNXE%3D; acPasstoken=ChVpbmZyYS5hY2Z1bi5wYXNzdG9rZW4ScJrFeEdlHcrvoHjYpzy_V7pAPxxMhNmo45RlMccpB84XwmIx9rJocLwXSPnJu4btgtqVGa04P9AIMBNdQknDd4QRTbjBp5aoQgis-Q88BjMvqVTGHAjG04W7zjswa0ZC5k5sdJCeTsLJKEiEsE8EY90aEhwsbiM70HUviQANoYC6Iptv6yIg0X6u8ikzzwuDe1VMKWNNYXWwFR81YKYQNq9P4gsk5oMoBTAB; auth_key=572984; ac_username=Nicksxs; acPostHint=1cc641584039a19bc0941d00e8eff73bd4b0; ac_userimg=https%3A%2F%2Fimgs.aixifan.com%2Fstyle%2Fimage%2F201907%2FuQ5Vc06d3HSVTjY3VZocT81X40FByVpw.jpg; clientlanguage=zh_CN; safety_id=AAG%2B9AbVZS6fTHVxwLtsI9Jl; notice_status=1; online_status=0; userGroupLevel=1; checkMobile=1; checkEmail=1; cur_req_id=1296724223CA632E_self_0fb782b0474cbe335574779cf53ed145; cur_group_id=1296724223CA632E_self_0fb782b0474cbe335574779cf53ed145_0; Hm_lpvt_2af69bc2b378fb58ae04ed2a04257ed1=1592976174",
// "Content-Type: text/plain"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
public function downloadVideo()
{
$innerDir = "/Volumes/Crucial X6/Video/acfun";
$list = AcfunVideo::all();
$files = scandir($innerDir);
// dump($files);
foreach ($list as $item) {
if ($item["is_downloaded"] == 1) {
continue;
}
if (in_array($item['title'] . " (" . $item["from_up_name"] . ").mp4", $files)) {
Log::info("file " . $item["title"] . ".mp4" . " exists continue");
continue;
}
Log::info($item['title'] . " (" . $item["from_up_name"] . ").mp4" . " does not exists to download");
// exit;
$downloadResult = shell_exec('cd "' .$innerDir .'" && annie https://www.acfun.cn/v/ac' . $item["content_id"]);
Log::info($downloadResult);
$item["is_downloaded"] = 1;
$item->save();
}
}
public function queryUpUsersVideos($upId)
{
$upUrl = $this->upBaseUrl . $upId;
$mainQl = QueryList::getInstance();
$pCursor = "";
$queried = 0;
do {
$result = $this->requestUpPageApi($upId, $pCursor);
$resourceIds = $mainQl->setHtml($result["html"])->find(".video-item")->attrs("resource-id");
$titles = $mainQl->setHtml($result["html"])->find(".video-name")->htmls();
dump($resourceIds);
foreach ($resourceIds as $key => $resourceId) {
AcfunVideo::firstOrCreate(["content_id" => $resourceId],
[
"title" => $titles[$key],
"from_type" => 2,
"from_collection_name" => "",
"from_up_name" => "香菜猫饼",
"from_up_user_id" => $upId
]);
}
$queried += $result["pageSize"];
Log::info("current queried: " . $queried);
if ($result["noMore"] || $queried > (int)$result["totalCount"]) {
break;
}
$pCursor = $result["pcursor"];
} while (true);
}
public function requestUpPage($upId)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://m.acfun.cn/upPage/$upId?",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'authority: m.acfun.cn',
'pragma: no-cache',
'cache-control: no-cache',
'upgrade-insecure-requests: 1',
'user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site: none',
'sec-fetch-mode: navigate',
'sec-fetch-user: ?1',
'sec-fetch-dest: document',
'accept-language: zh-CN,zh;q=0.9',
'cookie: _did=web_492135297EB70FE5; acPasstoken=ChVpbmZyYS5hY2Z1bi5wYXNzdG9rZW4ScMqMOf7IIDgLFifcwDByiCz1Wv6zcyhwUzxFnkKxtITs0jGXUUuLWqDsl-7NGWvWwo5AcSoY2-Kjd3CZOXY7PNERtpJIdfyEJ4AFL7nlbdkjI_V_uUanyorcfQ7kaiWNig2_E6FVh7O8nRoGss5vf9saErqF0TDd7uDbNNp-mP7P0DOW_CIg24xq_0sWM6gTjuCWKKX9x7Yg_OpB1KtIk17LYCXdAPwoBTAB; auth_key=572984; ac_username=Nicksxs; acPostHint=b9f4cda120751d7200fa5158fa16dcbcaf88; ac_userimg=https%3A%2F%2Fimgs.aixifan.com%2Fstyle%2Fimage%2F201907%2FuQ5Vc06d3HSVTjY3VZocT81X40FByVpw.jpg; safety_id=AAJRRbRCoai0XjYzLsf70ktB; EGG_SESS=LeuVrD7spUl4PQfNroW9BEVd1JGguP_VHNuClb-BCn-E0TK-gvszCD8JicS8W_cFD8r34_e30N8vQt95J-5Pn7CoX6NDxA_Sm_p3S-E8GaWKY5L1qIfhe_n0viZOlC1RZN9e9wm6bVjMwJc04acrWA==; Hm_lvt_c68e829637dac3d1ad7a134d18b6064f=1641224120; webp_supported=%7B%22lossy%22%3Atrue%2C%22lossless%22%3Atrue%2C%22alpha%22%3Atrue%2C%22animation%22%3Atrue%7D; Hm_lvt_2af69bc2b378fb58ae04ed2a04257ed1=1641223365,1641261026; Hm_lpvt_2af69bc2b378fb58ae04ed2a04257ed1=1641261026; _did=web_492135297EB70FE5'
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
public function requestUpPageApi($upId, $pcursor = "") {
if ($upId == null) {
return "";
}
$url = "https://m.acfun.cn/upPage/$upId?pagelets=video-list&reqID=4&ajaxpipe=1&userId=$upId&type=3";
if ($pcursor != null && $pcursor != "") {
$url .= "&pcursor=$pcursor";
}
$url .= "&t=" . (int)(microtime(true)*1000);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'authority: m.acfun.cn',
'pragma: no-cache',
'cache-control: no-cache',
'user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
'x-requested-with: XMLHttpRequest',
'accept: */*',
'sec-fetch-site: same-origin',
'sec-fetch-mode: cors',
'sec-fetch-dest: empty',
'referer: https://m.acfun.cn/upPage/4537972?',
'accept-language: zh-CN,zh;q=0.9',
'cookie: _did=web_492135297EB70FE5; acPasstoken=ChVpbmZyYS5hY2Z1bi5wYXNzdG9rZW4ScMqMOf7IIDgLFifcwDByiCz1Wv6zcyhwUzxFnkKxtITs0jGXUUuLWqDsl-7NGWvWwo5AcSoY2-Kjd3CZOXY7PNERtpJIdfyEJ4AFL7nlbdkjI_V_uUanyorcfQ7kaiWNig2_E6FVh7O8nRoGss5vf9saErqF0TDd7uDbNNp-mP7P0DOW_CIg24xq_0sWM6gTjuCWKKX9x7Yg_OpB1KtIk17LYCXdAPwoBTAB; auth_key=572984; ac_username=Nicksxs; acPostHint=b9f4cda120751d7200fa5158fa16dcbcaf88; ac_userimg=https%3A%2F%2Fimgs.aixifan.com%2Fstyle%2Fimage%2F201907%2FuQ5Vc06d3HSVTjY3VZocT81X40FByVpw.jpg; safety_id=AAJRRbRCoai0XjYzLsf70ktB; EGG_SESS=LeuVrD7spUl4PQfNroW9BEVd1JGguP_VHNuClb-BCn-E0TK-gvszCD8JicS8W_cFD8r34_e30N8vQt95J-5Pn7CoX6NDxA_Sm_p3S-E8GaWKY5L1qIfhe_n0viZOlC1RZN9e9wm6bVjMwJc04acrWA==; webp_supported=%7B%22lossy%22%3Atrue%2C%22lossless%22%3Atrue%2C%22alpha%22%3Atrue%2C%22animation%22%3Atrue%7D; Hm_lvt_2af69bc2b378fb58ae04ed2a04257ed1=1641223365,1641261026; Hm_lpvt_2af69bc2b378fb58ae04ed2a04257ed1=1641261026; cur_req_id=9320591763A17878_self_e9df062d443158972b38326847c62945; cur_group_id=9320591763A17878_self_e9df062d443158972b38326847c62945_0; Hm_lvt_c68e829637dac3d1ad7a134d18b6064f=1641224120,1641263656; Hm_lpvt_c68e829637dac3d1ad7a134d18b6064f=1641263656; _did=web_492135297EB70FE5'
),
));
$response = curl_exec($curl);
curl_close($curl);
// echo $response;
$replaced = str_replace("/*<!-- fetch-stream -->*/", "", $response);
$result = json_decode($replaced, true);
$scripts = $result["scripts"][0];
preg_match_all("#no_more#", $scripts, $noMore);
if (count($noMore[0]) > 0) {
dump($noMore);
$result["noMore"] = true;
} else {
preg_match_all("#\"pcursor\"\:\"(\d+)\"#", $scripts, $pcursors);
$parsed["pcursor"] = $pcursors[1][0];
}
preg_match_all("#\"totalCount\"\:(\d+)#", $scripts, $totalCounts);
$parsed["totalCount"] = $totalCounts[1][0];
preg_match_all("#\"videoFeedLength\"\:(\d+)#", $scripts, $pageSizes);
$parsed["pageSize"] = $pageSizes[1][0];
$parsed["html"] = $result["html"];
unset($result);
return $parsed;
}
}