Sync
This commit is contained in:
82
app/Helpers/FirstParty/DFS/DFSBacklinks.php
Normal file
82
app/Helpers/FirstParty/DFS/DFSBacklinks.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers\FirstParty\DFS;
|
||||
|
||||
use App\Models\CandidateDomain;
|
||||
use Exception;
|
||||
use Http;
|
||||
|
||||
class DFSBacklinks
|
||||
{
|
||||
public static function processAndSaveToCandidateDomains($directory)
|
||||
{
|
||||
// Get all the JSON files in the directory
|
||||
$jsonFiles = glob($directory.'/*.json');
|
||||
|
||||
dd($jsonFiles);
|
||||
|
||||
// Loop through each file
|
||||
foreach ($jsonFiles as $file) {
|
||||
// Check if the file name matches the pattern (1.json, 2.json, etc.)
|
||||
if (preg_match('/\/(\d+)\.json$/', $file, $matches)) {
|
||||
// Decode the JSON file
|
||||
$data = json_decode(file_get_contents($file), false);
|
||||
|
||||
foreach ($data->result[0]->items as $item) {
|
||||
$candidate_domain = CandidateDomain::where('from_domain', $item->domain_from)->first();
|
||||
|
||||
if (is_null($candidate_domain)) {
|
||||
$candidate_domain = new CandidateDomain;
|
||||
$candidate_domain->to_url = $item->url_to;
|
||||
$candidate_domain->from_domain = $item->domain_from;
|
||||
$candidate_domain->from_tld = get_tld_from_url($item->url_from);
|
||||
|
||||
if (is_array($item->domain_from_platform_type)) {
|
||||
$candidate_domain->platform_type = implode(',', $item->domain_from_platform_type);
|
||||
} else {
|
||||
$candidate_domain->platform_type = $item->domain_from_platform_type;
|
||||
}
|
||||
|
||||
$candidate_domain->from_url = $item->url_from;
|
||||
$candidate_domain->from_title = $item->page_from_title;
|
||||
$candidate_domain->from_image_alt = $item->alt;
|
||||
$candidate_domain->from_image_url = $item->image_url;
|
||||
$candidate_domain->save();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function backlinksPaginationLive($target, $search_after_token, $value = 1000)
|
||||
{
|
||||
$api_url = config('dataforseo.url');
|
||||
|
||||
$api_version = config('dataforseo.api_version');
|
||||
|
||||
$api_timeout = config('dataforseo.timeout');
|
||||
|
||||
$query = [
|
||||
'target' => $target,
|
||||
'search_after_token' => $search_after_token,
|
||||
'value' => $value,
|
||||
'mode' => 'as_is',
|
||||
];
|
||||
|
||||
try {
|
||||
$response = Http::timeout($api_timeout)->withBasicAuth(config('dataforseo.login'), config('dataforseo.password'))->withBody(
|
||||
json_encode([(object) $query])
|
||||
)->post("{$api_url}{$api_version}backlinks/backlinks/live");
|
||||
|
||||
if ($response->successful()) {
|
||||
return $response->body();
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
132
app/Helpers/FirstParty/DFS/DFSCommon.php
Normal file
132
app/Helpers/FirstParty/DFS/DFSCommon.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers\FirstParty\DFS;
|
||||
|
||||
use Exception;
|
||||
use Http;
|
||||
|
||||
class DFSCommon
|
||||
{
|
||||
public static function getTaskResult($task)
|
||||
{
|
||||
$task_object = new DFSResponse($task);
|
||||
|
||||
return (object) [
|
||||
'type' => 'task',
|
||||
'path' => implode('/', $task_object->getPath()),
|
||||
'id' => $task_object->getId(),
|
||||
'is_successful' => $task_object->isSuccessful(),
|
||||
'result' => $task_object->getResult(),
|
||||
'message' => $task_object->getStatusMessage(),
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
public static function getTasks($response)
|
||||
{
|
||||
|
||||
$dfs_object = new DFSResponse($response);
|
||||
|
||||
if ($dfs_object->isSuccessful()) {
|
||||
return $dfs_object->getTasks();
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function getTask($response, $key)
|
||||
{
|
||||
$tasks = self::getTasks($response);
|
||||
|
||||
if (count($tasks) > 0) {
|
||||
if (isset($tasks[$key])) {
|
||||
$task_object = $tasks[$key];
|
||||
|
||||
if (count($tasks) > 1) {
|
||||
$task_object->client_message = 'There are '.count($tasks).' tasks.';
|
||||
}
|
||||
|
||||
return $task_object;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function apiCall($method, $action_url, $query)
|
||||
{
|
||||
|
||||
$api_url = self::getApiUrl();
|
||||
|
||||
$api_version = config('dataforseo.api_version');
|
||||
|
||||
$api_timeout = config('dataforseo.timeout');
|
||||
|
||||
$full_api_url = "{$api_url}{$api_version}{$action_url}";
|
||||
|
||||
dump($full_api_url);
|
||||
dump($query);
|
||||
|
||||
$http_object = Http::timeout($api_timeout)->withBasicAuth(config('dataforseo.login'), config('dataforseo.password'));
|
||||
|
||||
if (strtoupper($method) == 'GET') {
|
||||
try {
|
||||
$response = $http_object->withUrlParameters(
|
||||
json_encode([(object) $query])
|
||||
)->get('');
|
||||
|
||||
} catch (Exception $e) {
|
||||
return self::defaultFailedResponse($e);
|
||||
}
|
||||
|
||||
} elseif (strtoupper($method) == 'POST') {
|
||||
try {
|
||||
$response = $http_object->withBody(
|
||||
json_encode([(object) $query])
|
||||
)->post("{$api_url}{$api_version}{$action_url}");
|
||||
} catch (Exception $e) {
|
||||
return self::defaultFailedResponse($e);
|
||||
}
|
||||
} else {
|
||||
throw new Exception('Invalid action method parameter.');
|
||||
}
|
||||
|
||||
if ($response->successful()) {
|
||||
return json_decode($response->body(), false);
|
||||
}
|
||||
|
||||
return self::defaultFailedResponse();
|
||||
|
||||
}
|
||||
|
||||
public static function getApiUrl()
|
||||
{
|
||||
$api_url = config('dataforseo.url');
|
||||
|
||||
if (config('dataforseo.sandbox_mode')) {
|
||||
$api_url = config('dataforseo.sandbox_url');
|
||||
}
|
||||
|
||||
return $api_url;
|
||||
}
|
||||
|
||||
private static function defaultFailedResponse(Exception $e = null)
|
||||
{
|
||||
$message = 'No such url.';
|
||||
|
||||
if (! is_null($e)) {
|
||||
$message = $e->getMessage();
|
||||
}
|
||||
|
||||
return (object) [
|
||||
'version' => '-1',
|
||||
'status_code' => -1,
|
||||
'status_message' => $message,
|
||||
'time' => '0 sec.',
|
||||
'cost' => 0,
|
||||
'tasks_count' => 0,
|
||||
'tasks_error' => 0,
|
||||
'tasks' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
41
app/Helpers/FirstParty/DFS/DFSOnPage.php
Normal file
41
app/Helpers/FirstParty/DFS/DFSOnPage.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers\FirstParty\DFS;
|
||||
|
||||
class DFSOnPage
|
||||
{
|
||||
// https://docs.dataforseo.com/v3/backlinks/id_list/?bash
|
||||
public static function listId($from_days, $to_days = 0, $limit = 1000, $offset = 0)
|
||||
{
|
||||
|
||||
$query = [
|
||||
'datetime_from' => now()->subDays($from_days)->format('Y-m-d H:i:s P'),
|
||||
'datetime_to' => now()->subDays($to_days)->format('Y-m-d H:i:s P'),
|
||||
];
|
||||
|
||||
if ($limit != 1000) {
|
||||
$query['limit'] = $limit;
|
||||
}
|
||||
if ($offset != 0) {
|
||||
$query['offset'] = $offset;
|
||||
}
|
||||
|
||||
$response = DFSCommon::apiCall('POST', 'on_page/id_list', $query);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
// https://docs.dataforseo.com/v3/on_page/task_post/
|
||||
public static function taskPost($target, $max_crawl_pages = 1)
|
||||
{
|
||||
|
||||
$query = [
|
||||
'target' => $target,
|
||||
'max_crawl_pages' => $max_crawl_pages,
|
||||
];
|
||||
|
||||
$response = DFSCommon::apiCall('POST', 'on_page/task_post', $query);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
63
app/Helpers/FirstParty/DFS/DFSResponse.php
Normal file
63
app/Helpers/FirstParty/DFS/DFSResponse.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers\FirstParty\DFS;
|
||||
|
||||
class DFSResponse
|
||||
{
|
||||
protected $response;
|
||||
|
||||
public function __construct($response)
|
||||
{
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
public function isSuccessful()
|
||||
{
|
||||
return ($this->response->status_code == 20000) ? true : false;
|
||||
}
|
||||
|
||||
public function getStatusCode()
|
||||
{
|
||||
return $this->response->status_code;
|
||||
}
|
||||
|
||||
public function getStatusMessage()
|
||||
{
|
||||
return $this->response->status_message;
|
||||
}
|
||||
|
||||
public function getTasks()
|
||||
{
|
||||
return $this->response->tasks;
|
||||
}
|
||||
|
||||
public function getResult()
|
||||
{
|
||||
return $this->response->result;
|
||||
}
|
||||
|
||||
public function getResultCount()
|
||||
{
|
||||
return $this->response->result_count;
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->response->id;
|
||||
}
|
||||
|
||||
public function getCost()
|
||||
{
|
||||
return $this->response->cost;
|
||||
}
|
||||
|
||||
public function getPath()
|
||||
{
|
||||
return $this->response->path;
|
||||
}
|
||||
|
||||
public function getData()
|
||||
{
|
||||
return $this->response->data;
|
||||
}
|
||||
}
|
||||
44
app/Helpers/FirstParty/DFS/DFSSerp.php
Normal file
44
app/Helpers/FirstParty/DFS/DFSSerp.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers\FirstParty\DFS;
|
||||
|
||||
use Exception;
|
||||
use Http;
|
||||
|
||||
class DFSSerp
|
||||
{
|
||||
public static function liveAdvanced($se, $se_type, $keyword, $location_name, $language_code, $depth, $search_param = null)
|
||||
{
|
||||
|
||||
$api_url = DFSCommon::getApiUrl();
|
||||
|
||||
$api_version = config('dataforseo.api_version');
|
||||
|
||||
$api_timeout = config('dataforseo.timeout');
|
||||
|
||||
$query = [
|
||||
'keyword' => $keyword,
|
||||
'location_name' => $location_name,
|
||||
'language_code' => $language_code,
|
||||
];
|
||||
|
||||
if (! is_empty($search_param)) {
|
||||
$query['search_param'] = $search_param;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Http::timeout($api_timeout)->withBasicAuth(config('dataforseo.login'), config('dataforseo.password'))->withBody(
|
||||
json_encode([(object) $query])
|
||||
)->post("{$api_url}{$api_version}serp/{$se}/{$se_type}/live/advanced");
|
||||
|
||||
if ($response->successful()) {
|
||||
return $response->body();
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user