Commit 6751648b authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

Basic Admitad Partnershop working now

parent e36255ab
......@@ -93,6 +93,10 @@ class MetaGerSearch extends Controller
# Ergebnisse der Suchmaschinen kombinieren:
$metager->prepareResults($timings);
$admitad = null;
if(!$metager->isApiAuthorized() && !$metager->isDummy()){
$admitad = new \App\Models\Admitad($metager);
}
$finished = true;
foreach ($metager->getEngines() as $engine) {
......@@ -106,6 +110,7 @@ class MetaGerSearch extends Controller
"metager" => [
"apiAuthorized" => $metager->isApiAuthorized(),
],
"admitad" => $admitad,
"adgoal" => [
"loaded" => $metager->isAdgoalLoaded(),
"adgoalHash" => $metager->getAdgoalHash(),
......@@ -190,6 +195,7 @@ class MetaGerSearch extends Controller
$engines = $cached["engines"];
$adgoal = $cached["adgoal"];
$admitad = $cached["admitad"];
$mg = $cached["metager"];
$metager = new MetaGer(substr($hash, strpos($hash, "loader_") + 7));
......@@ -209,6 +215,7 @@ class MetaGerSearch extends Controller
$metager->rankAll();
$metager->prepareResults();
$metager->parseAdmitad($admitad);
$result = [
'finished' => true,
......
......@@ -435,7 +435,9 @@ class MetaGer
}
}
public function parseAdmitad(\App\Models\Admitad &$admitad){
$admitad->parseAffiliates($this->results);
}
public function humanVerification(&$results)
{
......@@ -1718,6 +1720,9 @@ class MetaGer
$this->results = $results;
}
/**
* @return \App\Models\Result[]
*/
public function getResults()
{
return $this->results;
......@@ -1931,6 +1936,10 @@ class MetaGer
return $this->headerPrinted;
}
public function isDummy(){
return $this->dummy;
}
/**
* Used by JS result loader to restore MetaGer Object of previous request
*/
......
<?php
namespace App\Models;
use Cache;
use Log;
use LaravelLocalization;
use Illuminate\Support\Facades\Redis;
class Admitad
{
const VALID_LANGS = [
"de",
"en"
];
private $hash;
/**
* Creates a new Admitad object which will start a request for affiliate links
* based on a result List from MetaGer.
* It will parse the Links of the results and query any affiliate shops.
*
* @param \App\MetaGer $metager
*/
public function __construct(&$metager)
{
$results = $metager->getResults();
// Generate a list of URLs
$resultLinks = [];
foreach($results as $result){
$resultLinks[] = $result->originalLink;
}
$lang = LaravelLocalization::getCurrentLocale();
if(!in_array($lang, self::VALID_LANGS)){
$lang = "de";
}
$requestData = [
"lang" => $lang,
"urls" => $resultLinks,
];
$requestData = json_encode($requestData);
$this->hash = md5($requestData);
$url = "https://direct.metager.de/check";
$token = env("ADMITAD_TOKEN", "");
// Submit fetch job to worker
$mission = [
"resulthash" => $this->hash,
"url" => $url,
"useragent" => "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0",
"username" => null,
"password" => null,
"headers" => [
"Authorization" => "Bearer $token"
],
"cacheDuration" => 60,
"name" => "Admitad",
"curlopts" => [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $requestData
]
];
$mission = json_encode($mission);
Redis::rpush(\App\MetaGer::FETCHQUEUE_KEY, $mission);
}
/**
* Parses the response from Admitad server and converts all Affiliate Links.
*
* @param \App\Models\Result[] $results
*/
public function parseAffiliates(&$results){
$answer = Cache::get($this->hash);
$answer = json_decode($answer, true);
if(empty($answer) || !isset($answer["error"]) || $answer["error"] || !is_array($answer["result"])){
return;
}
foreach($answer["result"] as $linkResult){
$originalUrl = $linkResult["originalUrl"];
$redirUrl = $linkResult["redirUrl"];
$image = $linkResult["image"];
if(empty($redirUrl)){
// No Partnershop
continue;
}
foreach ($results as $result) {
if ($result->originalLink === $originalUrl) {
# Ein Advertiser gefunden
if ($result->image !== "") {
$result->logo = $image;
} else {
$result->image = $image;
}
# Den Link hinzufügen:
$result->link = $redirUrl;
$result->partnershop = true;
$result->changed = true;
}
}
}
Log::info("tzewst");
}
}
......@@ -9,6 +9,7 @@ class Result
{
public $provider; # Die Engine von der das Suchergebnis kommt
public $titel; # Der Groß Angezeigte Name für das Suchergebnis
public $originalLink;
public $link; # Der Link auf die Ergebnisseite
public $anzeigeLink; # Der tatsächlich angezeigte Link (rein optisch)
public $descr; # Die eventuell gekürzte Beschreibung des Suchergebnisses
......@@ -41,6 +42,7 @@ class Result
$this->provider = $provider;
$this->titel = $this->sanitizeText(strip_tags(trim($titel)));
$this->link = trim($link);
$this->originalLink = trim($link);
$this->anzeigeLink = trim($anzeigeLink);
$this->anzeigeLink = preg_replace("/(http[s]{0,1}:\/\/){0,1}(www\.){0,1}/si", "", $this->anzeigeLink);
$this->descr = $this->sanitizeText(strip_tags(trim($descr), '<p>'));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment