Last active
September 3, 2025 10:51
-
-
Save poontology/ae6777dbc342d51b31473807ca2df399 to your computer and use it in GitHub Desktop.
Prefill stash scene studio codes from JAV video filenames
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| # coding: utf-8 | |
| # utility script for https://github.com/stashapp/stash | |
| # 2025, poontology | |
| # Requires: working regexp_capture function, | |
| # wget -q https://github.com/nalgeon/sqlean/releases/download/0.28.0/sqlean-linux-x64.zip | |
| # unzip -q sqlean-linux-x64.zip -d /usr/lib/sqlite3/ | |
| import sqlite3 | |
| import sys | |
| import os | |
| def update_scene_codes_from_filenames(cur, prefix): | |
| rx = '(%s-\d+)' % prefix | |
| # ignores files <10MB in size | |
| cur.execute("""UPDATE scenes | |
| SET code = regexp_capture(f.basename, ?, 1) | |
| FROM files f | |
| JOIN scenes_files sf ON sf.file_id = f.id | |
| WHERE sf.scene_id = scenes.id | |
| AND f.basename REGEXP ? AND f.size > 1e7 | |
| AND scenes.code IS NULL; | |
| """, (rx, rx)) | |
| def ask_continue(): | |
| try: | |
| answer = input("Continue (y/n)? ") | |
| return answer.lower() == 'y' | |
| except KeyboardInterrupt: | |
| return False | |
| if __name__ == '__main__': | |
| if len(sys.argv) != 3: | |
| print("Syntax:", sys.argv[0], "stash-go.sqlite JAV_ID_PREFIX") | |
| sys.exit(1) | |
| dbfile, jav = sys.argv[1:3] | |
| if not os.access(dbfile, os.R_OK): | |
| print("Error: file not readable: ", dbfile) | |
| sys.exit(2) | |
| if not isinstance(jav, str) or not (3 <= len(jav) <= 5): | |
| print("Warning: JAV prefix doesn't seem valid (3-5 char long alphabetic value):", jav) | |
| if not ask_continue(): | |
| print("cancelled") | |
| sys.exit(3) | |
| con = sqlite3.connect(dbfile) | |
| con.enable_load_extension(True) | |
| con.execute("SELECT load_extension('/usr/lib/sqlite3/regexp.so')") | |
| try: | |
| cur = con.cursor() | |
| update_scene_codes_from_filenames(cur, jav) | |
| print("copied", cur.rowcount, "JAV codes for prefix:", jav) | |
| input("Press enter to continue (ctrl-c to cancel)") | |
| con.commit() | |
| print("done") | |
| except KeyboardInterrupt: | |
| print("cancelled") | |
| finally: | |
| con.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment