From 35ced33b7e7d92975f68c5b1da56c452b49a5014 Mon Sep 17 00:00:00 2001 From: Tweeticoats <60335703+Tweeticoats@users.noreply.github.com> Date: Tue, 10 Jan 2023 05:11:19 +1030 Subject: [PATCH] Updating CopyFromScene to use py_common libraries. (#864) --- SCRAPERS-LIST.md | 1 + scrapers/CopyFromScene.py | 95 +++++++++----------------------------- scrapers/CopyFromScene.yml | 2 +- 3 files changed, 24 insertions(+), 74 deletions(-) diff --git a/SCRAPERS-LIST.md b/SCRAPERS-LIST.md index 22042e52e..418591369 100644 --- a/SCRAPERS-LIST.md +++ b/SCRAPERS-LIST.md @@ -1443,6 +1443,7 @@ For each scraper a short description, an optional comment with the usage and the Scraper | Description | Comments | PR --------|-------------|----------|:--: ComicInfoXML.yml| A ComixInfo XML gallery scraper | A python scraper that looks for ComicInfo xml compatible files in the gallery's folder/filename and parses them | [#827](https://github.com/stashapp/CommunityScrapers/pull/827) +CopyFromScene.yml| A gallery scraper that returns metadata from the first linked scene | A python scraper that returns metadata from copied scenes, first link the scene to the gallery then run the scraper on the gallery | CopyToGallery.yml| A scene to gallery scraper | A python scene scraper that copies metadata from a scene to the associated galleries. Can optionally (check .py file) associate and copy meta to all galleries in the same folder as the scene| [#895](https://github.com/stashapp/CommunityScrapers/pull/895) dc-onlyfans.yml| An Onlyfans DB scene scraper | A python scraper that scrapes Only Fans scenes using the DB file (user_data.db) created from DIGITALCRIMINAL's tool | [#847](https://github.com/stashapp/CommunityScrapers/pull/847) Filename.yml | Scrape a scenes (local) filename to set as scene title | Utility scraper useful if you've bulk updated filenames outside of stash and want the changes synced back into stash | [#1136](https://github.com/stashapp/CommunityScrapers/pull/1136) diff --git a/scrapers/CopyFromScene.py b/scrapers/CopyFromScene.py index 8bc559a47..2267695a1 100644 --- a/scrapers/CopyFromScene.py +++ b/scrapers/CopyFromScene.py @@ -1,90 +1,39 @@ - import json +import requests import sys try: - import requests + import py_common.graphql as graphql + import py_common.log as log except ModuleNotFoundError: - print("You need to install the requests module. (https://docs.python-requests.org/en/latest/user/install/)", file=sys.stderr) - print("If you have pip (normally installed with python), run this command in a terminal (cmd): pip install requests", file=sys.stderr) + print("You need to download the folder 'py_common' from the community repo! (CommunityScrapers/tree/master/scrapers/py_common)", file=sys.stderr) sys.exit() -url="http://localhost:9999/graphql" -headers = { - "Accept-Encoding": "gzip, deflate, br", - "Content-Type": "application/json", - "Accept": "application/json", - "Connection": "keep-alive", - "DNT": "1" -} - -def __callGraphQL(query, variables=None): - - json = {} - json['query'] = query - if variables != None: - json['variables'] = variables - - # handle cookies - response = requests.post(url, json=json, headers=headers) - - if response.status_code == 200: - result = response.json() - if result.get("error", None): - for error in result["error"]["errors"]: - raise Exception("GraphQL error: {}".format(error)) - if result.get("data", None): - return result.get("data") - else: - raise Exception( - "GraphQL query failed:{} - {}. Query: {}. Variables: {}".format(response.status_code, response.content, query, variables)) - -def findGallery(id): - query = """query findGallery($gallery_id: ID!){ -findGallery(id: $gallery_id){ -id -path -scenes{ -title -url -date -rating -details -organized -studio{ - id - name -} -performers{ -id - name -} -tags{ -id - name -} -} -} -}""" - - variables = {'gallery_id': id, } - result = __callGraphQL(query, variables) - if result is not None: - return result["findGallery"] +def get_names(data: list): + kn = [] + if data: + for i in data: + if (i.get('name')): + kn.append({"name": i['name']}) + return kn + +def get_name(data: dict): + if data and (data.get("name")): + return { "name": data["name"] } return None - if sys.argv[1] == "gallery_query": fragment = json.loads(sys.stdin.read()) - print("input: " + json.dumps(fragment),file=sys.stderr) - result = findGallery(fragment['id']) + log.debug("input: " + json.dumps(fragment)) + result=graphql.getGallery(fragment['id']) if not result: - print(f"Could not determine details for gallery: `{fragment['id']}`",file=sys.stderr) + log.info(f"Could not determine details for gallery: `{fragment['id']}`") print("{}") else: if len(result["scenes"]) > 0: - print (json.dumps(result["scenes"][0])) + s=result["scenes"][0] + log.debug("data: " + json.dumps(s)) + res={"title":s["title"],"details":s["details"],"url":s["url"],"date":s["date"],"studio":get_name(s["studio"]),"performers":get_names(s["performers"]),"tags":get_names(s["tags"])} + print (json.dumps(res)) else: print ("{}") - - diff --git a/scrapers/CopyFromScene.yml b/scrapers/CopyFromScene.yml index 03bc7af30..7801dd1b4 100644 --- a/scrapers/CopyFromScene.yml +++ b/scrapers/CopyFromScene.yml @@ -5,4 +5,4 @@ galleryByFragment: - python3 - CopyFromScene.py - gallery_query -# Last Updated November 19, 2021 +# Last Updated January 09, 2023