| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2019-2025 Mike Fährmann |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
|
| 9 |
"""Extractors for Shopify instances""" |
| 10 |
|
| 11 |
from .common import BaseExtractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
|
| 15 |
class ShopifyExtractor(BaseExtractor): |
| 16 |
"""Base class for Shopify extractors""" |
| 17 |
basecategory = "shopify" |
| 18 |
filename_fmt = "{product[title]}_{num:>02}_{id}.{extension}" |
| 19 |
archive_fmt = "{id}" |
| 20 |
|
| 21 |
def items(self): |
| 22 |
data = self.metadata() |
| 23 |
yield Message.Directory, "", data |
| 24 |
|
| 25 |
for product in self.products(): |
| 26 |
for num, image in enumerate(product.pop("images"), 1): |
| 27 |
text.nameext_from_url(image["src"], image) |
| 28 |
image.update(data) |
| 29 |
image["product"] = product |
| 30 |
image["num"] = num |
| 31 |
yield Message.Url, image["src"], image |
| 32 |
|
| 33 |
def metadata(self): |
| 34 |
"""Return general metadata""" |
| 35 |
return {} |
| 36 |
|
| 37 |
def products(self): |
| 38 |
"""Return an iterable with all relevant product URLs""" |
| 39 |
|
| 40 |
|
| 41 |
BASE_PATTERN = ShopifyExtractor.update({ |
| 42 |
"chelseacrew": { |
| 43 |
"root": "https://chelseacrew.com", |
| 44 |
"pattern": r"(?:www\.)?chelseacrew\.com", |
| 45 |
}, |
| 46 |
"fashionnova": { |
| 47 |
"root": "https://www.fashionnova.com", |
| 48 |
"pattern": r"(?:www\.)?fashionnova\.com", |
| 49 |
}, |
| 50 |
"loungeunderwear": { |
| 51 |
"root": "https://loungeunderwear.com", |
| 52 |
"pattern": r"(?:[a-z]+\.)?loungeunderwear\.com", |
| 53 |
}, |
| 54 |
"michaelscameras": { |
| 55 |
"root": "https://michaels.com.au", |
| 56 |
"pattern": r"michaels\.com\.au", |
| 57 |
}, |
| 58 |
"modcloth": { |
| 59 |
"root": "https://modcloth.com", |
| 60 |
"pattern": r"modcloth\.com", |
| 61 |
}, |
| 62 |
"ohpolly": { |
| 63 |
"root": "https://www.ohpolly.com", |
| 64 |
"pattern": r"(?:www\.)?ohpolly\.com", |
| 65 |
}, |
| 66 |
"omgmiamiswimwear": { |
| 67 |
"root": "https://www.omgmiamiswimwear.com", |
| 68 |
"pattern": r"(?:www\.)?omgmiamiswimwear\.com", |
| 69 |
}, |
| 70 |
"pinupgirlclothing": { |
| 71 |
"root": "https://pinupgirlclothing.com", |
| 72 |
"pattern": r"pinupgirlclothing\.com", |
| 73 |
}, |
| 74 |
"raidlondon": { |
| 75 |
"root": "https://www.raidlondon.com", |
| 76 |
"pattern": r"(?:www\.)?raidlondon\.com", |
| 77 |
}, |
| 78 |
"unique-vintage": { |
| 79 |
"root": "https://www.unique-vintage.com", |
| 80 |
"pattern": r"(?:www\.)?unique\-vintage\.com", |
| 81 |
}, |
| 82 |
"windsorstore": { |
| 83 |
"root": "https://www.windsorstore.com", |
| 84 |
"pattern": r"(?:www\.)?windsorstore\.com", |
| 85 |
}, |
| 86 |
}) + "(?:/[a-z]{2}(?:-[^/?#]+)?)?" |
| 87 |
|
| 88 |
|
| 89 |
class ShopifyCollectionExtractor(ShopifyExtractor): |
| 90 |
"""Base class for collection extractors for Shopify based sites""" |
| 91 |
subcategory = "collection" |
| 92 |
directory_fmt = ("{category}", "{collection[title]}") |
| 93 |
pattern = BASE_PATTERN + r"(/collections/[\w-]+)/?(?:$|[?#])" |
| 94 |
example = "https://www.fashionnova.com/collections/TITLE" |
| 95 |
|
| 96 |
def metadata(self): |
| 97 |
url = f"{self.root}{self.groups[-1]}.json" |
| 98 |
return self.request_json(url) |
| 99 |
|
| 100 |
def products(self): |
| 101 |
url = f"{self.root}{self.groups[-1]}/products.json" |
| 102 |
params = {"page": 1} |
| 103 |
|
| 104 |
while True: |
| 105 |
data = self.request_json(url, params=params)["products"] |
| 106 |
if not data: |
| 107 |
return |
| 108 |
yield from data |
| 109 |
params["page"] += 1 |
| 110 |
|
| 111 |
|
| 112 |
class ShopifyProductExtractor(ShopifyExtractor): |
| 113 |
"""Base class for product extractors for Shopify based sites""" |
| 114 |
subcategory = "product" |
| 115 |
directory_fmt = ("{category}", "Products") |
| 116 |
pattern = BASE_PATTERN + r"((?:/collections/[\w-]+)?/products/[\w-]+)" |
| 117 |
example = "https://www.fashionnova.com/collections/TITLE/products/NAME" |
| 118 |
|
| 119 |
def products(self): |
| 120 |
url = f"{self.root}{self.groups[-1]}.json" |
| 121 |
product = self.request_json(url)["product"] |
| 122 |
del product["image"] |
| 123 |
return (product,) |