Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to timestamp.trade and stashdb-performer-gallery #282

Merged
merged 5 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
trim_trailing_whitespace = false
213 changes: 151 additions & 62 deletions plugins/stashdb-performer-gallery/stashdb-performer-gallery.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import stashapi.log as log
from stashapi.stashapp import StashInterface,StashItem
from stashapi.stashapp import StashInterface, StashItem
from stashapi.stashbox import StashBoxInterface
import os
import sys
Expand All @@ -15,10 +15,12 @@
per_page = 100
request_s = requests.Session()
stash_boxes = {}
scrapers={}
scrapers = {}


def processImages(img):
log.debug("image: %s" % (img,))
image_data = None
for file in [x["path"] for x in img["visual_files"]]:
if settings["path"] in file:
index_file = Path(Path(file).parent) / (Path(file).stem + ".json")
Expand All @@ -27,21 +29,25 @@ def processImages(img):
log.debug("loading index file %s" % (index_file,))
with open(index_file) as f:
index = json.load(f)

index["id"] = img["id"]

stash.update_image(index)
if image_data:
image_data["gallery_ids"].extend(index["gallery_ids"])
else:
image_data = index
if image_data:
# log.debug(image_data)
stash.update_image(image_data)


def processPerformers():
query={
"tags": {
"depth": 0,
"excludes": [],
"modifier": "INCLUDES_ALL",
"value": [tag_stashbox_performer_gallery],
}
query = {
"tags": {
"depth": 0,
"excludes": [],
"modifier": "INCLUDES_ALL",
"value": [tag_stashbox_performer_gallery],
}
}
performers = stash.find_performers(f=query)

for performer in performers:
Expand All @@ -59,8 +65,6 @@ def processPerformer(performer):


def get_stashbox(endpoint):
# if endpoint in stash_boxes:
# return stash_boxes[endpoint]
for sbx_config in stash.get_configuration()["general"]["stashBoxes"]:
if sbx_config["endpoint"] == endpoint:
stashbox = StashBoxInterface(
Expand Down Expand Up @@ -186,11 +190,11 @@ def processPerformerStashid(endpoint, stashid, p):
log.debug("image already downloaded")

# scrape urls on the performer using the url scrapers in stash
if settings['runPerformerScraper'] and len(perf['urls'])>0:
if settings["runPerformerScraper"] and len(perf["urls"]) > 0:

# we need to determine what scrapers we have and what url patterns they accept, query what url patterns are supported, should only need to check once
if len(scrapers) == 0:
scrapers_graphql="""query ListPerformerScrapers {
scrapers_graphql = """query ListPerformerScrapers {
listScrapers(types: [PERFORMER]) {
id
name
Expand All @@ -201,64 +205,132 @@ def processPerformerStashid(endpoint, stashid, p):
}
}"""
res = stash.callGQL(scrapers_graphql)
for r in res['listScrapers']:
if r['performer']['urls']:
for url in r['performer']['urls']:
scrapers[url]=r
for r in res["listScrapers"]:
if r["performer"]["urls"]:
for url in r["performer"]["urls"]:
scrapers[url] = r

for u in perf['urls']:
for u in perf["urls"]:
for url in scrapers.keys():
if url in u['url']:
log.info('Running stash scraper on performer url: %s' % (u['url'],))
res=stash.scrape_performer_url(u['url'])
if url in u["url"]:
log.info(
"Running stash scraper on performer url: %s" % (u["url"],)
)
res = stash.scrape_performer_url(u["url"])
# Check if the scraper returned a result
if res is not None:
log.debug(res)
# it's possible for multiple images to be returned by a scraper so incriment a number each image
image_id = 1
if res['images']:
for image in res['images']:
image_index = Path(settings["path"]) / p["id"] / ("%s-%s.json" % (scrapers[url]['id'],image_id ,))
if res["images"]:
for image in res["images"]:
image_index = (
Path(settings["path"])
/ p["id"]
/ (
"%s-%s.json"
% (
scrapers[url]["id"],
image_id,
)
)
)
if not image_index.exists():
with open(image_index, "w") as f:
image_data = {
"title": '%s - %s ' % (scrapers[url]['id'],image_id,),
"details": "name: %s\ngender: %s\nurl: %s\ntwitter: %s\ninstagram: %s\nbirthdate: %s\nethnicity: %s\ncountry: %s\neye_color: %s\nheight: %s\nmeasurements: %s\nfake tits: %s\npenis_length: %s\n career length: %s\ntattoos: %s\npiercings: %s\nhair_color: %s\nweight: %s\n description: %s\n" % (res['name'], res['gender'], res['url'], res['twitter'], res['instagram'], res['birthdate'], res['ethnicity'], res['country'], res['eye_color'], res['height'], res['measurements'], res['fake_tits'], res['penis_length'], res['career_length'], res['tattoos'], res['piercings'], res['hair_color'], res['weight'], res['details'],),
"urls": [u['url'],],
"title": "%s - %s "
% (
scrapers[url]["id"],
image_id,
),
"details": "name: %s\ngender: %s\nurl: %s\ntwitter: %s\ninstagram: %s\nbirthdate: %s\nethnicity: %s\ncountry: %s\neye_color: %s\nheight: %s\nmeasurements: %s\nfake tits: %s\npenis_length: %s\n career length: %s\ntattoos: %s\npiercings: %s\nhair_color: %s\nweight: %s\n description: %s\n"
% (
res["name"],
res["gender"],
res["url"],
res["twitter"],
res["instagram"],
res["birthdate"],
res["ethnicity"],
res["country"],
res["eye_color"],
res["height"],
res["measurements"],
res["fake_tits"],
res["penis_length"],
res["career_length"],
res["tattoos"],
res["piercings"],
res["hair_color"],
res["weight"],
res["details"],
),
"urls": [
u["url"],
],
"performer_ids": [p["id"]],
"tag_ids": [tag_stashbox_performer_gallery],
"gallery_ids": [index["galleries"][endpoint]],
"tag_ids": [
tag_stashbox_performer_gallery
],
"gallery_ids": [
index["galleries"][endpoint]
],
}
json.dump(image_data, f)
filename = Path(settings["path"]) / p["id"] / ("%s-%s.jpg" % (scrapers[url]['id'],image_id ,))
filename = (
Path(settings["path"])
/ p["id"]
/ (
"%s-%s.jpg"
% (
scrapers[url]["id"],
image_id,
)
)
)
if not filename.exists():
if image.startswith('data:'):
if image.startswith("data:"):
with open(filename, "wb") as f:
f.write(base64.b64decode(image.split('base64,')[1]))
f.write(
base64.b64decode(
image.split("base64,")[1]
)
)
f.close()
else:
with open(image_index, "w") as f:
image_data = {
"title": '%s - %s ' % (scrapers[url]['id'],image_id,),
"details": "%s"% (res,),
"urls": [u['url'],image],
"title": "%s - %s "
% (
scrapers[url]["id"],
image_id,
),
"details": "%s" % (res,),
"urls": [u["url"], image],
"performer_ids": [p["id"]],
"tag_ids": [tag_stashbox_performer_gallery],
"gallery_ids": [index["galleries"][endpoint]],
"tag_ids": [
tag_stashbox_performer_gallery
],
"gallery_ids": [
index["galleries"][endpoint]
],
}
json.dump(image_data, f)
filename = Path(settings["path"]) / p["id"] / ("%s.jpg" % (image_id,))
filename = (
Path(settings["path"])
/ p["id"]
/ ("%s.jpg" % (image_id,))
)
r = requests.get(img["url"])
if r.status_code==200:
if r.status_code == 200:
with open(filename, "wb") as f:
f.write(r.content)
f.close()
image_id=image_id+1

image_id = image_id + 1

# log.debug('%s %s' % (url['url'],url['type'],))
# stash.scraper
# scrape=stash.scrape_performer_url(ur)
# stash.scraper
# scrape=stash.scrape_performer_url(ur)

else:
log.error("endpoint %s not configured, skipping" % (endpoint,))
Expand Down Expand Up @@ -305,23 +377,35 @@ def processQueue():
]["queue"].removeprefix(settings["queue"])
},
)
stash.run_plugin_task("stashdb-performer-gallery", "Process Performers")
stash.run_plugin_task(
"stashdb-performer-gallery", "Process Performers", args={"full": False}
)


def relink_images():
query={
"path": {"modifier": "INCLUDES", "value": settings["path"]},
"is_missing": "galleries"
def relink_images(performer_id=None):
query = {
"path": {"modifier": "INCLUDES", "value": settings["path"]},
}
if performer_id == None:
query["is_missing"] = "galleries"
query["path"] = {"modifier": "INCLUDES", "value": settings["path"]}
else:
query["path"] = {
"modifier": "INCLUDES",
"value": str(Path(settings["path"]) / performer_id / ""),
}
total = stash.find_images(f=query,get_count=True)[0]
# else:
# query["file_count"] = {"modifier": "NOT_EQUALS", "value": 1}

total = stash.find_images(f=query, get_count=True)[0]
i = 0
images=[]
images = []
while i < total:
images = stash.find_images(f=query,filter={"page": 0, "per_page": per_page})
images = stash.find_images(f=query, filter={"page": 0, "per_page": per_page})
for img in images:
log.debug('image: %s' %(img,))
log.debug("image: %s" % (img,))
processImages(img)
i=i+1
i = i + 1
log.progress((i / total))


Expand All @@ -333,7 +417,7 @@ def relink_images():
config = stash.get_configuration()["plugins"]
settings = {
"path": "/download_dir",
"runPerformerScraper":False,
"runPerformerScraper": False,
}
if "stashdb-performer-gallery" in config:
settings.update(config["stashdb-performer-gallery"])
Expand All @@ -354,18 +438,23 @@ def relink_images():
p = stash.find_performer(json_input["args"]["performer"])
if tag_stashbox_performer_gallery in [x["id"] for x in p["tags"]]:
processPerformer(p)
stash.metadata_scan(paths=[settings["path"]])
stash.run_plugin_task(
"stashdb-performer-gallery", "relink missing images", args={}
)
stash.metadata_scan(paths=[settings["path"]])
stash.run_plugin_task(
"stashdb-performer-gallery",
"relink missing images",
args={"performer_id": p["id"]},
)
elif "processPerformers" in PLUGIN_ARGS:
processPerformers()
stash.metadata_scan([settings["path"]])
stash.run_plugin_task(
"stashdb-performer-gallery", "relink missing images", args={}
)
elif "processImages" in PLUGIN_ARGS:
relink_images()
if "performer_id" in json_input["args"]:
relink_images(performer_id=json_input["args"]["performer_id"])
else:
relink_images()


elif "hookContext" in json_input["args"]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: stashdb performer gallery
description: Automatically download performer images from stashdb or other stash-boxes. Add the [Stashbox Performer Gallery] tag to a performer and it will create a gallery of images from that stash-box database. Apply the tag [Set Profile Image] to an image to set it as the profile image of that performer. Note you will need to configure the download path and add this as a path under settings > library
version: 0.1
version: 0.2
url: https://github.com/stashapp/CommunityScripts/
exec:
- python
Expand Down
Loading