| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2023-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 https://lexica.art/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
|
| 15 |
class LexicaSearchExtractor(Extractor): |
| 16 |
"""Extractor for lexica.art search results""" |
| 17 |
category = "lexica" |
| 18 |
subcategory = "search" |
| 19 |
root = "https://lexica.art" |
| 20 |
directory_fmt = ("{category}", "{search_tags}") |
| 21 |
archive_fmt = "{id}" |
| 22 |
pattern = r"(?:https?://)?lexica\.art/?\?q=([^&#]+)" |
| 23 |
example = "https://lexica.art/?q=QUERY" |
| 24 |
|
| 25 |
def __init__(self, match): |
| 26 |
Extractor.__init__(self, match) |
| 27 |
self.query = match[1] |
| 28 |
self.text = text.unquote(self.query).replace("+", " ") |
| 29 |
|
| 30 |
def items(self): |
| 31 |
base = ("https://lexica-serve-encoded-images2.sharif.workers.dev" |
| 32 |
"/full_jpg/") |
| 33 |
tags = self.text |
| 34 |
|
| 35 |
for image in self.posts(): |
| 36 |
image["filename"] = image["id"] |
| 37 |
image["extension"] = "jpg" |
| 38 |
image["search_tags"] = tags |
| 39 |
yield Message.Directory, "", image |
| 40 |
yield Message.Url, base + image["id"], image |
| 41 |
|
| 42 |
def posts(self): |
| 43 |
url = self.root + "/api/infinite-prompts" |
| 44 |
headers = { |
| 45 |
"Accept" : "application/json, text/plain, */*", |
| 46 |
"Referer": f"{self.root}/?q={self.query}", |
| 47 |
} |
| 48 |
json = { |
| 49 |
"text" : self.text, |
| 50 |
"searchMode": "images", |
| 51 |
"source" : "search", |
| 52 |
"cursor" : 0, |
| 53 |
"model" : "lexica-aperture-v2", |
| 54 |
} |
| 55 |
|
| 56 |
while True: |
| 57 |
data = self.request_json( |
| 58 |
url, method="POST", headers=headers, json=json) |
| 59 |
|
| 60 |
prompts = { |
| 61 |
prompt["id"]: prompt |
| 62 |
for prompt in data["prompts"] |
| 63 |
} |
| 64 |
|
| 65 |
for image in data["images"]: |
| 66 |
image["prompt"] = prompts[image["promptid"]] |
| 67 |
del image["promptid"] |
| 68 |
yield image |
| 69 |
|
| 70 |
cursor = data.get("nextCursor") |
| 71 |
if not cursor: |
| 72 |
return |
| 73 |
|
| 74 |
json["cursor"] = cursor |