1
0
mirror of https://koodu.h-i.works/projects/thebadspace synced 2025-06-25 16:04:37 -05:00

Added Source Repository Class

Created a class to handle source data and methodology, and to offload
actions from service classes to make them easier to manage
This commit is contained in:
ro
2024-02-19 13:30:00 -06:00
parent 91297c809d
commit 894364debc
4 changed files with 117 additions and 94 deletions

View File

@ -4,68 +4,27 @@ namespace App\Services;
use App\Models\Location;
use App\Repositories\LocationRepository;
use App\Models\Source;
use App\Repositories\SourceRepository;
use Ramsey\Uuid\Uuid;
use Carbon\Carbon;
use GuzzleHttp\Exception\ConnectException;
class UpdateService
{
private $limit = 15;
protected $model;
protected $locationRepository;
protected $location;
protected $source;
public function __construct(LocationRepository $locationRepository)
{
$this->locationRepository = $locationRepository;
public function __construct(
LocationRepository $locationRepository,
SourceRepository $sourceRepository
) {
$this->location = $locationRepository;
$this->source = $sourceRepository;
}
public function data()
{
$sources = Source::where("active", true)->get();
$missing = [];
$checked = [];
//checks source url to make sure they valid
foreach ($sources as $source) {
$result = [];
if ($source['type'] == 'mastodon') {
if ($source['token'] == null) {
try {
$result = \Mastodon::domain('https://' . $source['url'])
->get('/instance/domain_blocks');
array_push($checked, ['source' => $source->url]);
} catch (ConnectException $e) {
array_push($missing, ['source' => $source->url]);
}
} else {
try {
$result = \Mastodon::domain('https://' . $source['url'])
->token($source['token'])
->get('/instance/domain_blocks');
array_push($checked, ['source' => $source->url]);
} catch (ConnectException $e) {
}
}
} elseif ($source['type'] == 'custom' && $source['format'] == 'csv') {
try {
$denylist = array_map('str_getcsv', file('https://' . $source['url']));
foreach ($denylist as $item) {
array_push($result, [
'domain' => $item[0],
'severity' => $item[1],
'comment' => $item[2]]);
}
array_push($checked, ['source' => $source->url]);
} catch (Exception $e) {
array_push($missing, ['source' => $source->url]);
}
}
$source->list_data = json_encode($result);
$source->last_updated = Carbon::now();
$source->save();
}
return count($checked) . ' SOURCES UPDATED - ' . count($missing) . ' SOURCES NOT CHECKED';
$response = $this->source->updateSourceData();
return count($response['checked']) . ' SOURCES UPDATED - ' .
count($response['notchecked']) . ' SOURCES NOT CHECKED';
}
public function list()
@ -74,7 +33,7 @@ class UpdateService
$fresh = 0;
$unified = [];
$sources = Source::where("active", true)->get();
$sources = $this->source->getActive();
foreach ($sources as $source) {
//$listData = json_decode();
@ -116,7 +75,7 @@ class UpdateService
}
foreach ($unified as $item) {
$location = $this->locationRepository->getLocation($item['url']);
$location = $this->location->getLocation($item['url']);
if ($location) {
++$duplicates;
//update block count for existing item
@ -173,23 +132,4 @@ class UpdateService
//TODO: Send update post to TBS social account
return $duplicates . ' LOCATIONS UPDATED | ' . $fresh . ' NEW LOCATIONS CREATED';
}
public function urlExists($url)
{
// Remove all illegal characters from a url
$url = filter_var($url, FILTER_SANITIZE_URL);
// Validate URI
if (
filter_var($url, FILTER_VALIDATE_URL) === false || // check only for http/https schemes.
!in_array(
strtolower(parse_url($url, PHP_URL_SCHEME)),
["http", "https"],
true
)
) {
return false;
} // Check that URL exists
$file_headers = @get_headers($url);
return !(!$file_headers || $file_headers[0] === "HTTP/1.1 404 Not Found");
}
}