Skip to content

Commit a8087f4

Browse files
[fix] [Timestamp Trade] Use context manager with connection to cleanup and reduce db locking (#538)
1 parent 4703e3f commit a8087f4

File tree

2 files changed

+95
-95
lines changed

2 files changed

+95
-95
lines changed

plugins/timestampTrade/timestampTrade.py

Lines changed: 94 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -379,32 +379,32 @@ def processSceneTimestamTrade(s):
379379
log.debug(s.keys())
380380
for fs in data["funscripts"]:
381381
log.debug(fs["md5"])
382-
conn = db_migrations()
383-
cur = conn.cursor()
384-
res = cur.execute(
385-
"select id,filename,scene_id from script_index where md5=?",
386-
(fs["md5"],),
387-
)
388-
for row in res.fetchall():
389-
if len(s["files"]) > 0:
390-
log.debug(
391-
"found matching funscript, copying funscript"
392-
)
393-
scriptfile_source = Path(row[1])
394-
video_file = Path(s["files"][0]["path"])
395-
scriptfile_destination = video_file.parent / (
396-
video_file.stem + ".funscript"
397-
)
398-
log.info(
399-
"copying funscript %s, to destination %s,"
400-
% (
401-
scriptfile_source,
402-
scriptfile_destination,
403-
)
404-
)
405-
shutil.copyfile(
406-
scriptfile_source, scriptfile_destination
407-
)
382+
with db_migrations() as conn:
383+
cur = conn.cursor()
384+
res = cur.execute(
385+
"select id,filename,scene_id from script_index where md5=?",
386+
(fs["md5"],),
387+
)
388+
for row in res.fetchall():
389+
if len(s["files"]) > 0:
390+
log.debug(
391+
"found matching funscript, copying funscript"
392+
)
393+
scriptfile_source = Path(row[1])
394+
video_file = Path(s["files"][0]["path"])
395+
scriptfile_destination = video_file.parent / (
396+
video_file.stem + ".funscript"
397+
)
398+
log.info(
399+
"copying funscript %s, to destination %s,"
400+
% (
401+
scriptfile_source,
402+
scriptfile_destination,
403+
)
404+
)
405+
shutil.copyfile(
406+
scriptfile_source, scriptfile_destination
407+
)
408408

409409
if needs_update:
410410
log.debug("updating scene: %s" % (new_scene,))
@@ -696,20 +696,20 @@ def submitScene(query):
696696
if settings["submitFunscriptHash"]:
697697
log.debug(s)
698698
s["funscriptHashes"] = []
699-
conn = db_migrations()
700-
cur = conn.cursor()
701-
res = cur.execute(
702-
"select id,filename,metadata,scene_id,md5 from script_index where scene_id=?",
703-
(s["id"],),
704-
)
705-
for row in res.fetchall():
706-
s["funscriptHashes"].append(
707-
{
708-
"filename": str(Path(row[1]).name),
709-
"metadata": json.loads(row[2]),
710-
"md5": row[4],
711-
}
712-
)
699+
with db_migrations() as conn:
700+
cur = conn.cursor()
701+
res = cur.execute(
702+
"select id,filename,metadata,scene_id,md5 from script_index where scene_id=?",
703+
(s["id"],),
704+
)
705+
for row in res.fetchall():
706+
s["funscriptHashes"].append(
707+
{
708+
"filename": str(Path(row[1]).name),
709+
"metadata": json.loads(row[2]),
710+
"md5": row[4],
711+
}
712+
)
713713
s.pop("id")
714714
log.debug(s)
715715
request_s.post("https://timestamp.trade/submit-stash", json=s)
@@ -1230,60 +1230,60 @@ def db_migrations():
12301230

12311231

12321232
def funscript_index(path):
1233-
conn = db_migrations()
1234-
cur = conn.cursor()
1235-
for file in path.glob("**/*.funscript"):
1236-
log.info("indexing script file %s" % (file,))
1237-
with open(file, "rb") as f:
1238-
data = f.read()
1239-
hash = hashlib.md5(data).hexdigest()
1240-
log.debug(hash)
1241-
d = json.loads(data)
1242-
metadata = {}
1243-
if "metadata" in d:
1244-
metadata = d["metadata"]
1245-
res = cur.execute(
1246-
"select count(*) from script_index where filename=? ",
1247-
(str(file.resolve()),),
1248-
)
1249-
if res.fetchone()[0] == 0:
1250-
cur.execute(
1251-
"insert into script_index (filename,metadata,md5)values (?,?,?)",
1252-
(str(file.resolve()), json.dumps(metadata), hash),
1253-
)
1254-
conn.commit()
1255-
res = cur.execute("select count(*) from script_index ")
1256-
funscript_count = res.fetchone()[0]
1257-
log.info(
1258-
"finished indexing funscripts, %s scripts indexed, matching to scenes"
1259-
% (funscript_count,)
1260-
)
1261-
res = cur.execute(
1262-
"select id,filename,scene_id from script_index where scene_id is null;"
1263-
)
1264-
for row in res.fetchall():
1265-
id = row[0]
1266-
filename = row[1]
1267-
scenes = stash.find_scenes(
1268-
f={"path": {"modifier": "INCLUDES", "value": Path(filename).stem}},
1269-
fragment="id\nfiles{basename}",
1270-
)
1271-
i = 0
1272-
for s in scenes:
1273-
1274-
for f in s["files"]:
1275-
if Path(filename).stem == Path(f["basename"]).stem:
1276-
log.info(
1277-
"matching scene %s to script %s"
1278-
% (
1279-
s["id"],
1280-
filename,
1281-
)
1282-
)
1283-
cur.execute(
1284-
"update script_index set scene_id=? where id=?", (s["id"], id)
1285-
)
1286-
conn.commit()
1233+
with db_migrations() as conn:
1234+
cur = conn.cursor()
1235+
for file in path.glob("**/*.funscript"):
1236+
log.info("indexing script file %s" % (file,))
1237+
with open(file, "rb") as f:
1238+
data = f.read()
1239+
hash = hashlib.md5(data).hexdigest()
1240+
log.debug(hash)
1241+
d = json.loads(data)
1242+
metadata = {}
1243+
if "metadata" in d:
1244+
metadata = d["metadata"]
1245+
res = cur.execute(
1246+
"select count(*) from script_index where filename=? ",
1247+
(str(file.resolve()),),
1248+
)
1249+
if res.fetchone()[0] == 0:
1250+
cur.execute(
1251+
"insert into script_index (filename,metadata,md5)values (?,?,?)",
1252+
(str(file.resolve()), json.dumps(metadata), hash),
1253+
)
1254+
conn.commit()
1255+
res = cur.execute("select count(*) from script_index ")
1256+
funscript_count = res.fetchone()[0]
1257+
log.info(
1258+
"finished indexing funscripts, %s scripts indexed, matching to scenes"
1259+
% (funscript_count,)
1260+
)
1261+
res = cur.execute(
1262+
"select id,filename,scene_id from script_index where scene_id is null;"
1263+
)
1264+
for row in res.fetchall():
1265+
id = row[0]
1266+
filename = row[1]
1267+
scenes = stash.find_scenes(
1268+
f={"path": {"modifier": "INCLUDES", "value": Path(filename).stem}},
1269+
fragment="id\nfiles{basename}",
1270+
)
1271+
i = 0
1272+
for s in scenes:
1273+
1274+
for f in s["files"]:
1275+
if Path(filename).stem == Path(f["basename"]).stem:
1276+
log.info(
1277+
"matching scene %s to script %s"
1278+
% (
1279+
s["id"],
1280+
filename,
1281+
)
1282+
)
1283+
cur.execute(
1284+
"update script_index set scene_id=? where id=?", (s["id"], id)
1285+
)
1286+
conn.commit()
12871287

12881288

12891289
def excluded_marker_tag(marker):

plugins/timestampTrade/timestampTrade.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Timestamp Trade
22
description: Sync Markers with timestamp.trade, a new database for sharing markers.
3-
version: 0.8
3+
version: 0.8.1
44
url: https://github.com/stashapp/CommunityScripts/
55
exec:
66
- python

0 commit comments

Comments
 (0)