Skip to content

Commit

Permalink
Updating CopyFromScene to use py_common libraries. (stashapp#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tweeticoats authored Jan 9, 2023
1 parent 37b4fef commit 35ced33
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 74 deletions.
1 change: 1 addition & 0 deletions SCRAPERS-LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
95 changes: 22 additions & 73 deletions scrapers/CopyFromScene.py
Original file line number Diff line number Diff line change
@@ -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 ("{}")


2 changes: 1 addition & 1 deletion scrapers/CopyFromScene.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ galleryByFragment:
- python3
- CopyFromScene.py
- gallery_query
# Last Updated November 19, 2021
# Last Updated January 09, 2023

0 comments on commit 35ced33

Please sign in to comment.