forked from stashapp/CommunityScrapers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating CopyFromScene to use py_common libraries. (stashapp#864)
- Loading branch information
1 parent
37b4fef
commit 35ced33
Showing
3 changed files
with
24 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ("{}") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters