37 lines
1.7 KiB
PHP
37 lines
1.7 KiB
PHP
<?php
|
|
if(($_GET['k']??'')!=='WEVADS2026') die('auth');
|
|
|
|
// Fix the scrapy API to cd to project dir
|
|
$api='<?php
|
|
header("Content-Type: application/json");
|
|
header("Access-Control-Allow-Origin: *");
|
|
if(($_GET["k"]??"")!=="WEVADS2026") die(json_encode(["error"=>"auth"]));
|
|
$action=$_GET["action"]??"status";
|
|
$project="/opt/weval-scrapy";
|
|
|
|
switch($action){
|
|
case "status":
|
|
$spiders=array_values(array_filter(array_map(fn($f)=>basename($f,".py"),glob("$project/weval_scrapy/spiders/*.py")),fn($n)=>$n!=="__init__"));
|
|
$list=trim(shell_exec("cd $project && python3 -m scrapy list 2>&1"));
|
|
echo json_encode(["ok"=>true,"project"=>$project,"spiders"=>$spiders,"scrapy_list"=>$list,"scrapy"=>trim(shell_exec("python3 -m scrapy version 2>&1"))]);
|
|
break;
|
|
case "run":
|
|
$spider=preg_replace("/[^a-z0-9_]/","", $_GET["spider"]??"");
|
|
if($spider){
|
|
$out=shell_exec("cd $project && timeout 60 python3 -m scrapy crawl $spider -o /tmp/scrapy-$spider.json --nolog 2>&1");
|
|
$items=@json_decode(@file_get_contents("/tmp/scrapy-$spider.json"),true);
|
|
echo json_encode(["ok"=>true,"spider"=>$spider,"items"=>count($items?:[]),"output"=>trim($out),"data"=>array_slice($items?:[],0,5)]);
|
|
}
|
|
break;
|
|
case "results":
|
|
$spider=preg_replace("/[^a-z0-9_]/","", $_GET["spider"]??"");
|
|
$file="/tmp/scrapy-$spider.json";
|
|
echo file_exists($file)?file_get_contents($file):json_encode(["error"=>"no results"]);
|
|
break;
|
|
case "list":
|
|
echo json_encode(["spiders"=>explode("\n",trim(shell_exec("cd $project && python3 -m scrapy list 2>&1")))]);
|
|
break;
|
|
}';
|
|
file_put_contents("/var/www/html/api/scrapy-api.php",$api);
|
|
echo json_encode(["ok"=>true,"api"=>"fixed"]);
|