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

Update stash-sqlite scraper to use data image URLs #460

Merged
merged 1 commit into from
Apr 2, 2021
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
64 changes: 32 additions & 32 deletions scrapers/stash-sqlite.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
import base64
import imghdr
import json
import mimetypes
import sys
import sqlite3
from os import path

''' This script uses the sqlite database from another stash database and allows you to parse performers
Copy stash-go.sqlite to the scrapers directory
This script needs python3 and sqlite3

To support extracting images from the database we need to provide a url where these image are located.
Stash allows you to serve custom files by adding a few lines to the configuration.
This plugin will then export the performer images to that folder and add the url of that image in the response.

Make the directory and add the following to config.yml configuration:
custom_served_folders:
/stash_sqlite: /root/.stash/stash_sqlite

Make the directory /root/.stash/stash_sqlite or update the configuration with the path
To enable this feature change enable_images to: True
'''

http_prefix='http://127.0.0.1:9999/custom/stash_sqlite/'
image_output_dir='/root/.stash/stash_sqlite/'
enable_images=False
from os import path

'''
This script uses the sqlite database from another stash database and allows you to parse performers
Copy stash-go.sqlite to the scrapers directory

This script needs python3
'''

def query_performers(name):
c = conn.cursor()
Expand All @@ -33,6 +23,13 @@ def query_performers(name):
rec.append(res)
return rec

def make_image_data_url(image_data):
# type: (bytes,) -> str
img_type = imghdr.what(None, h=image_data) or 'jpeg'
mime = mimetypes.types_map.get('.' + img_type, 'image/jpeg')
encoded = base64.b64encode(image_data).decode()
return 'data:{0};base64,{1}'.format(mime, encoded)

def fetch_performer_name(name):
c = conn.cursor()
c.execute('SELECT name,gender,url,twitter,instagram,date(birthdate),ethnicity,country,eye_color,height,measurements,fake_tits,career_length,tattoos,piercings,aliases,id FROM performers WHERE lower(name) = lower(?)', (name,))
Expand All @@ -57,15 +54,16 @@ def fetch_performer_name(name):
res['tattoos']=row[13]
res['piercings']=row[14]
res['aliases']=row[15]
if enable_images:
performer_id=row[16]
c.execute('select image from performers_image where performer_id=?',(performer_id,))
row=c.fetchone()
if row == None:
return res
with open("%s%d.jpg" % (image_output_dir,performer_id), 'wb') as file:
file.write(row[0])
res['image']="%s%d.jpg" % (http_prefix,performer_id)

performer_id=row[16]
c.execute('select image from performers_image where performer_id=?',(performer_id,))
row=c.fetchone()
if row == None:
return res

image = make_image_data_url(row[0])
res['image']=image

return res


Expand All @@ -79,7 +77,7 @@ def fetch_performer_name(name):

if sys.argv[1] == "query":
fragment = json.loads(sys.stdin.read())
print(json.dumps(fragment),file=sys.stderr)
print("input: " + json.dumps(fragment),file=sys.stderr)
result = query_performers(fragment['name'])
if not result:
print(f"Could not determine details for performer: `{fragment['name']}`",file=sys.stderr)
Expand All @@ -90,11 +88,13 @@ def fetch_performer_name(name):

if sys.argv[1] == "fetch":
fragment = json.loads(sys.stdin.read())
print(json.dumps(fragment),file=sys.stderr)
print("input: " + json.dumps(fragment),file=sys.stderr)
result = fetch_performer_name(fragment['name'])
if not result:
print(f"Could not determine details for performer: `{fragment['name']}`",file=sys.stderr)
print("{}")
else:
print (json.dumps(result))
conn.close()

# Last Updated March 31, 2021
6 changes: 4 additions & 2 deletions scrapers/stash-sqlite.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
name: "stash sqlite"
name: stash sqlite

performerByFragment:
action: script
script:
- python
- stash-sqlite.py
- fetch

performerByName:
action: script
script:
- python
- stash-sqlite.py
- query

# Last Updated August 12, 2020
# Last Updated March 31, 2021