diff --git a/js/customElements/ebkBanner.js b/js/customElements/ebkBanner.js index 6be307612863c023f2015a7843e96d93c9d760c9..9ff55f32438a20c6fbcd298fe86a0ba45f085d6e 100644 --- a/js/customElements/ebkBanner.js +++ b/js/customElements/ebkBanner.js @@ -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); + } } } diff --git a/js/customElements/sellingTable.js b/js/customElements/sellingTable.js index cf88d68a1471e10e89282c421c530e8d4f9f85c4..53bb82da5e084cbab7914d1a242f3dd6581ea855 100644 --- a/js/customElements/sellingTable.js +++ b/js/customElements/sellingTable.js @@ -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); + }); + + }); } } diff --git a/public/API/ebk.php b/public/API/ebk.php deleted file mode 100644 index 29541c1e17e824e809ede226252a95a8369ae9ea..0000000000000000000000000000000000000000 --- a/public/API/ebk.php +++ /dev/null @@ -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); \ No newline at end of file diff --git a/public/API/queries/ebayKleinanzeigen.php b/public/API/queries/ebayKleinanzeigen.php new file mode 100644 index 0000000000000000000000000000000000000000..a479dec181fd9dc35abcf7fd9a9f749c890635d1 --- /dev/null +++ b/public/API/queries/ebayKleinanzeigen.php @@ -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 + ]; +} \ No newline at end of file diff --git a/public/API/queries/queries.php b/public/API/queries/queries.php index e7558057e1717ff17046004e6755747a7d84ee70..4e38aa624dd76224821053ee913f846dfd059843 100644 --- a/public/API/queries/queries.php +++ b/public/API/queries/queries.php @@ -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"]), ] ],