From 7eca5271602f5529b47008d63fbfe319f422b951 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dawid=20Wr=C3=B3bel?= <me@dawidwrobel.com>
Date: Sat, 15 Apr 2023 18:40:49 +0200
Subject: [PATCH] [eBayBridge] New bridge (#3349)

Fixes #3268
---
 bridges/EBayBridge.php | 97 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)
 create mode 100644 bridges/EBayBridge.php

diff --git a/bridges/EBayBridge.php b/bridges/EBayBridge.php
new file mode 100644
index 00000000..f2919938
--- /dev/null
+++ b/bridges/EBayBridge.php
@@ -0,0 +1,97 @@
+<?php
+
+class EBayBridge extends BridgeAbstract
+{
+    const NAME = 'eBay';
+    const DESCRIPTION = 'Returns the search results from the eBay auctioning platforms';
+    const URI = 'https://www.eBay.com';
+    const MAINTAINER = 'wrobelda';
+    const PARAMETERS = [[
+        'url' => [
+            'name' => 'Search URL',
+            'title' => 'Copy the URL from your browser\'s address bar after searching for your items and paste it here',
+            'pattern' => '^(https:\/\/)?(www.)?ebay\.(com|com\.au|at|be|ca|ch|cn|es|fr|de|com\.hk|ie|it|com\.my|nl|ph|pl|com\.sg|co\.uk).*$',
+            'exampleValue' => 'https://www.ebay.com/sch/i.html?_nkw=atom+rss',
+            'required' => true,
+        ]
+    ]];
+
+    public function getURI()
+    {
+        if ($this->getInput('url')) {
+            # make sure we order by the most recently listed offers
+            $uri = trim(preg_replace('/([?&])_sop=[^&]+(&|$)/', '$1', $this->getInput('url')), '?&/');
+            $uri .= (parse_url($uri, PHP_URL_QUERY) ? '&' : '?') . '_sop=10';
+
+            return $uri;
+        } else {
+            return parent::getURI();
+        }
+    }
+
+    public function getName()
+    {
+        $urlQueries = explode('&', parse_url($this->getInput('url'), PHP_URL_QUERY));
+
+        $searchQuery = array_reduce($urlQueries, function ($q, $p) {
+            if (preg_match('/^_nkw=(.+)$/i', $p, $matches)) {
+                $q[] = str_replace('+', ' ', urldecode($matches[1]));
+            }
+
+            return $q;
+        });
+
+        if ($searchQuery) {
+            return $searchQuery[0];
+        }
+
+        return parent::getName();
+    }
+
+    public function collectData()
+    {
+        $html = getSimpleHTMLDOM($this->getURI());
+
+        // Remove any unsolicited results, e.g. "Results matching fewer words"
+        foreach ($html->find('ul.srp-results > li.srp-river-answer--REWRITE_START ~ li') as $inexactMatches) {
+            $inexactMatches->remove();
+        }
+
+        $results = $html->find('ul.srp-results > li.s-item');
+        foreach ($results as $listing) {
+            $item = [];
+
+            // Remove "NEW LISTING" label, we sort by the newest, so this is redundant
+            foreach ($listing->find('.LIGHT_HIGHLIGHT') as $new_listing_label) {
+                $new_listing_label->remove();
+            }
+
+            $item['title'] = $listing->find('.s-item__title', 0)->plaintext;
+
+            $subtitle = implode('', $listing->find('.s-item__subtitle'));
+
+            $item['uri'] = $listing->find('.s-item__link', 0)->href;
+
+            preg_match('/.*\/itm\/(\d+).*/i', $item['uri'], $matches);
+            $item['uid'] = $matches[1];
+
+            $price = $listing->find('.s-item__details > .s-item__detail > .s-item__price', 0)->plaintext;
+            $shippingFree = $listing->find('.s-item__details > .s-item__detail > .s-item__freeXDays', 0)->plaintext ?? '';
+            $localDelivery = $listing->find('.s-item__details > .s-item__detail > .s-item__localDelivery', 0)->plaintext ?? '';
+            $logisticsCost = $listing->find('.s-item__details > .s-item__detail > .s-item__logisticsCost', 0)->plaintext ?? '';
+
+            $location = $listing->find('.s-item__details > .s-item__detail > .s-item__location', 0)->plaintext ?? '';
+
+            $sellerInfo = $listing->find('.s-item__seller-info-text', 0)->plaintext ?? '';
+
+            $item['enclosures'] = [ $listing->find('.s-item__image-wrapper > img', 0)->src . '#.image' ];
+
+            $item['content'] = <<<CONTENT
+<p>$sellerInfo $location</p>
+<p><span style="font-weight:bold">$price</span> $shippingFree $localDelivery $logisticsCost<span></span></p>
+<p>$subtitle</p>
+CONTENT;
+            $this->items[] = $item;
+        }
+    }
+}