| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License version 2 as |
| 5 |
# published by the Free Software Foundation. |
| 6 |
|
| 7 |
"""Extractors for https://vanlifetrader.com/""" |
| 8 |
|
| 9 |
from .common import Extractor, Message |
| 10 |
from .. import text |
| 11 |
|
| 12 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?vanlifetrader\.com" |
| 13 |
|
| 14 |
|
| 15 |
class VanlifetraderExtractor(Extractor): |
| 16 |
"""Base class for vanlifetrader extractors""" |
| 17 |
category = "vanlifetrader" |
| 18 |
root = "https://vanlifetrader.com" |
| 19 |
|
| 20 |
def _api_listing_metadata(self, listing): |
| 21 |
title = (listing.get("yoast_head_json") or {}).get("og_title", "") |
| 22 |
if title.endswith(" - Vanlife Trader"): |
| 23 |
title = title[:-17] |
| 24 |
|
| 25 |
return { |
| 26 |
"listing_id": listing["id"], |
| 27 |
"slug" : listing["slug"], |
| 28 |
"title" : title, |
| 29 |
"date" : self.parse_datetime_iso(listing["date"]), |
| 30 |
} |
| 31 |
|
| 32 |
def _api_listing_images(self, listing_id): |
| 33 |
url = f"{self.root}/wp-json/wp/v2/media" |
| 34 |
params = { |
| 35 |
"parent" : listing_id, |
| 36 |
"per_page": 100, |
| 37 |
"_fields" : "id,source_url", |
| 38 |
} |
| 39 |
return self.request_json(url, params=params) |
| 40 |
|
| 41 |
def _api_listings(self, params=None): |
| 42 |
url = f"{self.root}/wp-json/wp/v2/listing" |
| 43 |
p = { |
| 44 |
"per_page": 100, |
| 45 |
"page" : 1, |
| 46 |
"_fields" : "id,slug,link,date,yoast_head_json", |
| 47 |
} |
| 48 |
if params: |
| 49 |
p.update(params) |
| 50 |
|
| 51 |
while True: |
| 52 |
listings = self.request_json(url, params=p) |
| 53 |
if not listings: |
| 54 |
return |
| 55 |
yield from listings |
| 56 |
if len(listings) < p["per_page"]: |
| 57 |
return |
| 58 |
p["page"] += 1 |
| 59 |
|
| 60 |
|
| 61 |
class VanlifetraderListingExtractor(VanlifetraderExtractor): |
| 62 |
"""Extractor for individual van listings on vanlifetrader.com""" |
| 63 |
subcategory = "listing" |
| 64 |
directory_fmt = ("{category}", "{title}") |
| 65 |
filename_fmt = "{listing_id}_{num:>02}.{extension}" |
| 66 |
archive_fmt = "{listing_id}_{num}" |
| 67 |
pattern = BASE_PATTERN + r"/listing/([^/?#]+)" |
| 68 |
example = "https://vanlifetrader.com/listing/YEAR-MAKE-MODEL-HASH/" |
| 69 |
|
| 70 |
def items(self): |
| 71 |
url = f"{self.root}/wp-json/wp/v2/listing" |
| 72 |
params = { |
| 73 |
"slug" : self.groups[0], |
| 74 |
"_fields": "id,slug,link,date,yoast_head_json", |
| 75 |
} |
| 76 |
listings = self.request_json(url, params=params) |
| 77 |
|
| 78 |
if not listings: |
| 79 |
raise self.exc.NotFoundError("listing") |
| 80 |
|
| 81 |
data = self._api_listing_metadata(listings[0]) |
| 82 |
yield Message.Directory, "", data |
| 83 |
|
| 84 |
for num, media in enumerate( |
| 85 |
self._api_listing_images(listings[0]["id"]), 1): |
| 86 |
image_url = media["source_url"] |
| 87 |
image_data = text.nameext_from_url(image_url, dict(data)) |
| 88 |
image_data["num"] = num |
| 89 |
image_data["media_id"] = media["id"] |
| 90 |
yield Message.Url, image_url, image_data |
| 91 |
|
| 92 |
|
| 93 |
class VanlifetraderExploreExtractor(VanlifetraderExtractor): |
| 94 |
"""Extractor for explore and search pages on vanlifetrader.com""" |
| 95 |
subcategory = "explore" |
| 96 |
pattern = BASE_PATTERN + r"/explore" |
| 97 |
example = "https://vanlifetrader.com/explore/" |
| 98 |
|
| 99 |
def items(self): |
| 100 |
data = {"_extractor": VanlifetraderListingExtractor} |
| 101 |
for listing in self._api_listings(): |
| 102 |
yield Message.Queue, listing["link"], data |