Wednesday, June 25, 2008

用 .bat (batch) + PHP + flashget script 自動抓網頁檔案

用 .bat (batch) + PHP + flashget script 自動抓網頁檔案

今天偷學到的一招,在 batch file (.bat) 內坎 php code,以下是簡單的範例


====== sample.bat begin ======
@GOTO START
<?php

// put some php code here
echo "Hello World";

/*
:START
@C:\web\php5.2.5\php.exe %0
@pause
@exit
*/
?>
====== sample.bat end ======


底下兩隻 Run_Start.bat 和 fetch.bat 是比較實用的例子,用途是 執行 Run_Start.bat 後,它會再執行 PHP code ,接著分析網頁中的 檔案連結 (免費的空中英語教室的 mp3 檔),並配合 FlashGet 抓取 .mp3 檔

======= Run_Start.bat begin ===========
@GOTO START
<?php
error_reporting( 255);
set_time_limit(0);

$SavePath = "E:/English/StudioClassroom.com/"; // the place to save the files.

$ThisYear = date("Y");
$ThisMonth = date("n");
$ThisDay = date("j");

if($ThisDay == 1) { // if it is the first day of the month, create a new folder
//mkdir($ThisMonth);
}

echo $ThisYear."-".$ThisMonth."-".$ThisDay."<P>\n\n";
$URL = "http://www.studioclassroom.com/";
$Classes = array('lt/lt_radio.php', 'sc/sc_radio.php', 'ad/ad_radio.php');
$ClassesPathName = array('01letstalkinenglish/', '02studioclassroom/', '03advanced/');


for($i=0;$i<=2;$i++) {
//echo $Classes[$i]."<BR>";
$PageContent = file_get_contents($URL.$Classes[$i]);

//echo $PageContent;

$patternURL = '|mms://(.*).wma|';
preg_match_all( $patternURL, $PageContent, $matchesResult, PREG_PATTERN_ORDER);

//print_r($matchesResult);

//echo $matchesResult[0][0];
$fileURL = $matchesResult[0][0];
$fileName = basename($matchesResult[0][0]);
echo $fileURL."<br>\n";

$SaveToHere1 = $SavePath.$ClassesPathName[$i].$ThisYear."/".$ThisMonth."/";
$SaveToHere2 = $SaveToHere1.$fileName;

if( file_exists($SaveToHere2) ) {
echo "<font color=red><b>The file exist!!!</b></font><p>";
echo "==================<p>";
continue;
}

$currentDir = getcwd();
exec( "$currentDir/fetch.bat $fileURL $SaveToHere1" );

//echo "Save to: ".$SaveToHere2."<p>";

//echo "==================<p>";
usleep(1000000); // wait for 2 secondes
}



/*
:START
@C:\web\php5.2.5\php.exe %0
@pause
@exit
*/
?>
======= Run_Start.bat end ===========


======= fetch.bat begin ===========
@GOTO START

:START
@E:\English\StudioClassroom.com\psTools\psexec.exe -d "c:\program files\FlashGet\flashget.exe" %1 %2
@exit
======= fetch.bat end ===========