get ebay kleinanzeigen from graphql

This commit is contained in:
Jonas Leder 2022-03-08 13:42:03 +01:00
parent abe8db6e8b
commit 92ce267baf
No known key found for this signature in database
GPG key ID: 8A53DD45A7D7B44B
5 changed files with 187 additions and 124 deletions

View file

@ -1,20 +1,24 @@
class ebkBanner extends HTMLElement {
constructor(){
super();
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if(xhr.readyState === 4 && xhr.status === 200){
if(xhr.responseText > 0) {
const h2 = document.createElement("h2");
h2.classList.add("red");
h2.innerHTML = "Ich biete aktuell wieder verschiedene Artikel zum verkauf an, eine genaue Übersicht ist <a class=\"red\" href=\"/selling.html\">hier</a> zu sehen."
this.appendChild(h2);
}
}
}
this.generateBanner();
}
xhr.open("GET", "/API/ebk.php?count");
xhr.send();
async generateBanner() {
var graphql = JSON.stringify({
query: 'query { ebayKleinanzeigen{ count }}',
})
var requestOptions = {
method: 'POST',
body: graphql,
};
let elementCount = (await (await fetch("http://localhost:1234/API/graphql.php", requestOptions)).json()).data.ebayKleinanzeigen.count;
if(elementCount > 0) {
const h2 = document.createElement("h2");
h2.classList.add("red");
h2.innerHTML = "Ich biete aktuell wieder verschiedene Artikel zum verkauf an, eine genaue Übersicht ist <a class=\"red\" href=\"/selling.html\">hier</a> zu sehen."
this.appendChild(h2);
}
}
}

View file

@ -1,13 +1,17 @@
import * as basicLightbox from 'basiclightbox'
class sellingTable extends HTMLElement {
constructor(){
const config = [
constructor() {
super();
this.config = [
{
"title": "Bild",
"fieldName": "previewImage",
"fieldName": "preview",
"displayType": "image",
"fullImage": "image"
"fullImage": "image",
"index": 0
},
{
"title": "Titel",
@ -31,68 +35,70 @@ class sellingTable extends HTMLElement {
"linkText": "Anzeige ansehen",
"target": "_blank"
},
]
];
super();
this.generateTable();
}
async generateTable() {
const table = document.createElement("table");
this.appendChild(table);
const tr = document.createElement("tr");
table.appendChild(tr);
config.forEach(element => {
this.config.forEach(element => {
const th = document.createElement("th");
th.innerText = element["title"];
tr.appendChild(th);
});
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if(xhr.readyState === 4 && xhr.status === 200){
const response = JSON.parse(xhr.responseText);
response.forEach( ad => {
const tr = document.createElement("tr");
table.appendChild(tr);
config.forEach(element => {
const th = document.createElement("th");
var graphql = JSON.stringify({
query: 'query { ebayKleinanzeigen(imageCount: 1) { elements { images { preview image } title price shipping link }}}',
})
var requestOptions = {
method: 'POST',
body: graphql,
};
let elements = (await (await fetch("http://localhost:1234/API/graphql.php", requestOptions)).json()).data.ebayKleinanzeigen.elements;
elements.forEach(ad => {
const tr = document.createElement("tr");
table.appendChild(tr);
this.config.forEach(element => {
const th = document.createElement("th");
switch(element["displayType"]) {
case "text":
th.innerText = ad[element["fieldName"]];
break;
case "link":
const link = document.createElement("a");
th.appendChild(link);
link.href = ad[element["fieldName"]];
link.innerText = element["linkText"];
if("target" in element) {
link.target = element["target"];
}
break;
case "image":
const img = document.createElement("img");
th.appendChild(img);
img.src = ad[element["fieldName"]];
img.onclick = () => {
const instance = basicLightbox.create(`
<img src="${ad[element["fullImage"]]}">
`);
instance.show();
}
break;
switch (element["displayType"]) {
case "text":
th.innerText = ad[element["fieldName"]];
break;
case "link":
const link = document.createElement("a");
th.appendChild(link);
link.href = ad[element["fieldName"]];
link.innerText = element["linkText"];
if ("target" in element) {
link.target = element["target"];
}
break;
case "image":
const img = document.createElement("img");
th.appendChild(img);
img.src = ad["images"][element["index"]][element["fieldName"]];
img.onclick = () => {
const instance = basicLightbox.create(`
<img src="${ad["images"][element["index"]][element["fullImage"]]}">
`);
instance.show();
}
break;
tr.appendChild(th);
});
})
}
}
}
xhr.open("GET", "/API/ebk.php");
xhr.send();
tr.appendChild(th);
});
});
}
}

View file

@ -1,62 +0,0 @@
<?php
require 'vendor/autoload.php';
require "./lib/config.php";
use GuzzleHttp\Client;
$responseJSON = [];
$client = new Client();
$headers = [
'authorization' => 'Basic ' . $ebayKleinanzeigenToken,
'user-agent' => 'okhttp/4.9.1',
'x-ebayk-app' => '4e10d7fd-6fef-4f87-afb0-b8ede2f494071636475109828',
'Host' => 'api.ebay-kleinanzeigen.de',
'Accept' => '*/*',
'Accept-Encoding' => 'gzip, deflate, br'
];
$response = $client->request('GET', "https://api.ebay-kleinanzeigen.de/api/ads.json?_in=title,price,pictures,link,features-active,search-distance,negotiation-enabled,attributes,medias,medias.media,medias.media.title,medias.media.media-link,store-id,store-title&page=0&size=31&userIds=$ebayKleinanzeigenUserId&pictureRequired=false&includeTopAds=false&limitTotalResultCount=true", [
'headers' => $headers ]);
$response = json_decode($response->getBody(), true);
$ads = $response["{http://www.ebayclassifiedsgroup.com/schema/ad/v1}ads"]["value"]["ad"];
foreach($ads as $ad) {
$element = [
"title" => $ad["title"]["value"],
"price" => $ad["price"]["amount"]["value"] . "",
"shipping" => "nein"
];
foreach($ad["attributes"]["attribute"] as $attribute) {
if(str_contains($attribute["name"], "versand")) {
$element["shipping"] = $attribute["value"][0]["value"];
}
}
foreach($ad["link"] as $link) {
if($link["rel"] == "self-public-website") {
$element["link"] = $link["href"];
}
}
if(sizeof($ad["pictures"]["picture"]) > 0) {
foreach($ad["pictures"]["picture"][0]["link"] as $picture) {
if($picture["rel"] == "teaser") {
$element["previewImage"] = str_replace("https://i.ebayimg.com", "/API/ebayimg.php?url=", $picture["href"]);
}
if($picture["rel"] == "XXL") {
$element["image"] = str_replace("https://i.ebayimg.com", "/API/ebayimg.php?url=", $picture["href"]);
}
}
}
array_push($responseJSON, $element);
}
if(isset($_GET["count"])) {
echo sizeof($responseJSON);
die();
}
echo json_encode($responseJSON);

View file

@ -0,0 +1,103 @@
<?php
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ObjectType;
use GuzzleHttp\Client;
$ebayKleinanzeigenImages = new ObjectType([
"name" => "EBK Image",
"fields" => [
"preview" => Type::string(),
"image" => Type::string()
]
]);
$ebayKleinanzeigenElements = new ObjectType([
"name" => "EBK Elements",
"fields" => [
"title" => Type::string(),
"price" => Type::string(),
"shipping" => Type::string(),
"link" => Type::string(),
"images" => [
"type" => Type::listOf($ebayKleinanzeigenImages),
"args" => [
"count" => Type::int()
]
],
"id" => Type::string()
]
]);
$ebayKleinanzeigenFields = new ObjectType([
"name" => "Ebay Kleinanzeigen",
"fields" => [
"count" => Type::int(),
"elements" => Type::listOf($ebayKleinanzeigenElements)
],
]);
function ebayKleinanzeigen($imageCount) {
require "./lib/config.php";
$elements = [];
$client = new Client();
$headers = [
'authorization' => 'Basic ' . $ebayKleinanzeigenToken,
'user-agent' => 'okhttp/4.9.1',
'x-ebayk-app' => '4e10d7fd-6fef-4f87-afb0-b8ede2f494071636475109828',
'Host' => 'api.ebay-kleinanzeigen.de',
'Accept' => '*/*',
'Accept-Encoding' => 'gzip, deflate, br'
];
$response = $client->request('GET', "https://api.ebay-kleinanzeigen.de/api/ads.json?_in=title,price,pictures,link,features-active,search-distance,negotiation-enabled,attributes,medias,medias.media,medias.media.title,medias.media.media-link,store-id,store-title&page=0&size=31&userIds=$ebayKleinanzeigenUserId&pictureRequired=false&includeTopAds=false&limitTotalResultCount=true", [
'headers' => $headers ]);
$response = json_decode($response->getBody(), true);
$ads = $response["{http://www.ebayclassifiedsgroup.com/schema/ad/v1}ads"]["value"]["ad"];
foreach($ads as $ad) {
$element = [
"title" => html_entity_decode($ad["title"]["value"]),
"id" => $ad["id"],
"price" => $ad["price"]["amount"]["value"] . "",
"shipping" => "nein"
];
foreach($ad["attributes"]["attribute"] as $attribute) {
if(str_contains($attribute["name"], "versand")) {
$element["shipping"] = $attribute["value"][0]["value"];
}
}
foreach($ad["link"] as $link) {
if($link["rel"] == "self-public-website") {
$element["link"] = $link["href"];
}
}
$images = [];
foreach(array_slice($ad["pictures"]["picture"], 0, $imageCount) as $picture) {
$image = [];
foreach($picture["link"] as $pictureSize) {
if($pictureSize["rel"] == "teaser") {
$image["preview"] = str_replace("https://i.ebayimg.com", "/API/ebayimg.php?url=", $pictureSize["href"]);
}
if($pictureSize["rel"] == "XXL") {
$image["image"] = str_replace("https://i.ebayimg.com", "/API/ebayimg.php?url=", $pictureSize["href"]);
}
}
array_push($images, $image);
}
$element["images"] = $images;
array_push($elements, $element);
}
return [
"count" => sizeof($elements),
"elements" => $elements
];
}

View file

@ -6,6 +6,8 @@ require "./queries/skills.php";
require "./queries/blogPost.php";
require "./queries/comments.php";
require "./queries/mailAddress.php";
require "./queries/ebayKleinanzeigen.php";
$queryType = new ObjectType([
'name' => 'Query',
@ -50,6 +52,16 @@ $queryType = new ObjectType([
"article" => Type::nonNull(Type::string()),
],
'resolve' => fn ($rootValue, $args) => comments($args["article"], $rootValue["db"]),
],
'ebayKleinanzeigen' => [
"type" => $ebayKleinanzeigenFields,
"args" => [
"imageCount" => [
"type" => Type::int(),
"defaultValue" => 0
]
],
'resolve' => fn ($rootValue, $args) => ebayKleinanzeigen($args["imageCount"]),
]
],