- <# :
- cls&echo off&mode con lines=5000
- rem bat及txt存为ANSI编码
- cd /d "%~dp0"
- powershell -NoProfile -ExecutionPolicy bypass "[IO.File]::ReadAllText('%~f0',[Text.Encoding]::GetEncoding('GB2312'))|Invoke-Expression"
- pause
- exit
- #>
- $listfile="电视剧名称.txt";
- $startfile="记录.txt";
- $resultfile="结果.txt";
- $okfile="有效.txt";
- $nofile="无效.txt";
-
- $minilen=150;
- $timeout=900;
-
- if(-not (test-path -liter $listfile)){write-host ('"'+$listfile+'" 未找到');exit;};
-
- $enc=[Text.Encoding]::GetEncoding('GB2312');
-
- function gethtml($u){
- $content='';
- for($j=1;$j -le 3;$j++){
- try{
- $http=New-Object -ComObject 'Msxml2.XMLHTTP';
- $http.Open('GET', $u, $false);
- $http.SetRequestHeader('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Edg/94.0.992.38');
- $http.SetRequestHeader('Host','www.tvmao.com');
- $http.SetRequestHeader('Connection','Keep-Alive');
- $http.Send();
- $content=[System.Text.Encoding]::UTF8.GetString($http.ResponseBody);
- $http.abort();
- break;
- }catch{
- write-host ('第'+$j.toString()+'次获取网页源码失败');
- start-sleep -Seconds 3;
- }
- }
- return $content;
- }
-
- function searchname($w, $f, $s){
- $arr=New-Object -TypeName System.Collections.ArrayList;
- $searchurl='https://www.tvmao.com/servlet/queryobject?type=all&term='+[System.Net.WebUtility]::UrlEncode($w);
- while(1){
- $content=gethtml $searchurl;
- if(-not $content.Contains('503 Service Temporarily Unavailable')){
- $m=[regex]::matches($content, '\{[^\}]*?"name":"([^"]+?)","typeId":11,"url":"([^"]+?)"[^\}]*?\}');
- foreach($it in $m){
- if($f -eq $true){
- if($it.groups[1].value.replace(':',':') -eq $w.replace(':',':')){
- $item=$it.groups[1].value+'|'+$it.groups[2].value;
- [void]$arr.add($item);
- }
- }else{
- $item=$it.groups[1].value+'|'+$it.groups[2].value;
- [void]$arr.add($item);
- }
- }
- break;
- }
- start-sleep -Seconds $s;
- }
- return $arr;
- }
-
- function getdrama($u){
- $arr=New-Object -TypeName System.Collections.ArrayList;
- $content=gethtml $u;
- $m1=[regex]::match($content,'<div class="epipage clear">([\s\S]+?)</div>');
- if($m1.Success){
- $m2=[regex]::matches($m1.groups[1].value, '<li(?: [^>]*?)?>.*?href="([^"]+?)"[^>]*?>(\d+)</a></li>');
- foreach($it in $m2){
- $item=$it.groups[2].value+'|'+$it.groups[1].value;
- [void]$arr.add($item);
- }
- }
- return $arr;
- }
-
- function getcontent($u){
- $content='';
- $html=gethtml $u;
- $m3=[regex]::match($html,'<article class="clear epi_c"[^>]*?>([\s\S]+?)</article>');
- if($m3.Success){
- $content=((($m3.groups[1].value -replace '<p>','') -replace ' ',' ') -replace '</p>',"`r`n") -replace '<div(?: [^>]*?)?>[\s\S]+?</div>','';
- $content=(($content -replace '<[^>]+?>','') -replace '</?[a-z]+','').trim();
- }
- return $content;
- }
-
- $startname='1-1';
- $fs1=New-Object System.IO.FileStream($resultfile, [System.IO.FileMode]::Append);
- $sw1=New-Object System.IO.StreamWriter($fs1, $enc);
- $fs2=New-Object System.IO.FileStream($okfile, [System.IO.FileMode]::Append);
- $sw2=New-Object System.IO.StreamWriter($fs2, $enc);
- $fs3=New-Object System.IO.FileStream($nofile, [System.IO.FileMode]::Append);
- $sw3=New-Object System.IO.StreamWriter($fs3, $enc);
- $text1=[IO.File]::ReadAllText($listfile, $enc).split("`r`n",[StringSplitOptions]::RemoveEmptyEntries);
- if(test-path -liter $startfile){
- $startname=[IO.File]::ReadAllText($startfile, $enc).trim();
- if($startname -match '^\d+$'){$startname=$startname+'-1';}
- }
- $startnum=$startname.split('-');
- for($i=0;$i -lt $text1.Count;$i++){
- if(($i+1) -ge [int]$startnum[0]){
- $has=$false;
- write-host ('-------------------------'+$text1[$i]+'-------------------------');
- $names=@(searchname $text1[$i] $true 2);
- if($names.length -ge 1){
- for($j=0;$j -lt $names.length;$j++){
- if(($i+1) -eq [int]$startnum[0]){
- if(($j+1) -lt [int]$startnum[1]){continue;}
- }
- [IO.File]::WriteAllText($startfile, ($i+1).toString()+'-'+($j+1).toString(), $enc);
- $arr=$names[$j].split('|');
- $iturl='https://www.tvmao.com/'+$arr[1].trim('/')+'/episode';
- write-host ('【'+$arr[0]+'】 '+$iturl);
- $title='TTT'+$arr[0];
- $result=New-Object -TypeName System.Collections.ArrayList;
- $dramas=@(getdrama $iturl);
- if($dramas.length -ge 1){
- [void]$result.add($title);
- $has=$true;
- for($k=0;$k -lt $dramas.length;$k++){
- $brr=$dramas[$k].split('|');
- $suburl='https://www.tvmao.com/'+$brr[1].trim('/');
- [void]$result.add('第'+$brr[0]+'集');
- write-host ('第'+$brr[0]+'集 '+$brr[1]);
- $t=0;
- $x=1;
- $ct='';
- while($x -le 3){
- $ct=getcontent $suburl;
- write-host $ct.length;
- if($ct.length -gt $minilen){
- break;
- }else{
- $t++;
- }
- if($t -ge 3){
- $t=0;
- write-host ('字数少于'+$minilen+',暂停'+$timeout+'秒……');
- start-sleep -Seconds $timeout;
- $x++;
- }
- }
- [void]$result.add($ct);
- }
- }
- if($result.count -ge 1){
- $s=@($result) -join "`r`n";
- $sw1.WriteLine($s);
- $sw1.WriteLine('');
- $sw1.Flush();
- }
- }
- }
- if($has){
- $sw2.WriteLine($text1[$i]);
- $sw2.Flush();
- }else{
- $sw3.WriteLine($text1[$i]);
- $sw3.Flush();
- }
- }
- }
- $sw1.Close();$fs1.Close();
- $sw2.Close();$fs2.Close();
- $sw3.Close();$fs3.Close();
复制代码
|