Files
webproxyscan/app/Console/Commands/FetchProxyScrape.php
T
2025-03-30 19:37:01 +03:00

93 lines
2.8 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use App\Models\Proxy;
class FetchProxyScrape extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'proxies:fetch-scrape';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Fetch proxies from ProxyScrape API and save them to the database';
/**
* Execute the console command.
*/
public function handle()
{
$this->info('Fetching proxies from ProxyScrape API...');
try {
$response = Http::get('https://api.proxyscrape.com/v3/free-proxy-list/get?request=displayproxies&protocol=http&proxy_format=ipport&format=text&timeout=20000');
if (!$response->successful()) {
throw new \Exception('Failed to fetch proxies: ' . $response->status());
}
$proxyList = explode("\n", trim($response->body()));
$this->info('Found ' . count($proxyList) . ' proxies');
$bar = $this->output->createProgressBar(count($proxyList));
$bar->start();
$saved = 0;
$skipped = 0;
foreach ($proxyList as $proxyString) {
$proxyString = trim($proxyString);
if (empty($proxyString)) continue;
// Parse IP and Port from the proxy string
list($ip, $port) = explode(':', $proxyString);
$proxyUrl = "http://{$ip}:{$port}";
// Check if proxy already exists
$exists = Proxy::where('proxy', $proxyUrl)->exists();
if (!$exists) {
Proxy::create([
'proxy' => $proxyUrl,
'protocol' => 'http',
'ip' => $ip,
'port' => (int) $port,
'https' => false, // Default to false since these are HTTP proxies
'anonymity' => 'unknown', // ProxyScrape doesn't provide anonymity info
'score' => 1, // Default score
'country' => null, // ProxyScrape doesn't provide geolocation
'city' => null,
]);
$saved++;
} else {
$skipped++;
}
$bar->advance();
}
$bar->finish();
$this->newLine();
$this->info("Proxies imported successfully!");
$this->info("New proxies saved: {$saved}");
$this->info("Existing proxies skipped: {$skipped}");
} catch (\Exception $e) {
$this->error('Error: ' . $e->getMessage());
return 1;
}
return 0;
}
}