automatyzacje-comit2

This commit is contained in:
Mateusz Gruszczyński
2026-05-07 08:11:58 +02:00
parent 440b187c39
commit 5d7abe6e59
3 changed files with 79 additions and 23 deletions

View File

@@ -165,30 +165,39 @@ def _apply_effects_bulk(c: Any, profile: dict[str, Any], torrents: list[dict[str
'keep_seeding': bool(eff.get('keep_seeding')),
}
result = rtorrent.action(profile, hashes, 'move', payload)
applied.append({'type': 'move', 'path': path, 'count': len(hashes), 'move_data': payload['move_data'], 'recheck': payload['recheck'], 'keep_seeding': payload['keep_seeding'], 'result': result})
applied.append({'type': 'move', 'path': path, 'count': len(hashes), 'target_hashes': hashes, 'move_data': payload['move_data'], 'recheck': payload['recheck'], 'keep_seeding': payload['keep_seeding'], 'result': result})
elif typ == 'add_label':
label = str(eff.get('label') or '').strip()
if label:
for h in hashes:
# Note: Add-label automations are idempotent; torrents that already have the label are ignored.
target_hashes = [h for h in hashes if label not in labels_by_hash.get(h, [])]
for h in target_hashes:
labels = labels_by_hash.setdefault(h, [])
if label not in labels:
labels.append(label); c.call('d.custom1.set', h, _label_value(labels))
applied.append({'type': 'add_label', 'label': label, 'count': len(hashes)})
labels.append(label); c.call('d.custom1.set', h, _label_value(labels))
if target_hashes:
applied.append({'type': 'add_label', 'label': label, 'count': len(target_hashes), 'target_hashes': target_hashes})
elif typ == 'remove_label':
label = str(eff.get('label') or '').strip()
if label:
for h in hashes:
# Note: Remove-label automations run only on torrents that actually contain the label.
target_hashes = [h for h in hashes if label in labels_by_hash.get(h, [])]
for h in target_hashes:
labels = [x for x in labels_by_hash.get(h, []) if x != label]
labels_by_hash[h] = labels; c.call('d.custom1.set', h, _label_value(labels))
applied.append({'type': 'remove_label', 'label': label, 'count': len(hashes)})
if target_hashes:
applied.append({'type': 'remove_label', 'label': label, 'count': len(target_hashes), 'target_hashes': target_hashes})
elif typ == 'set_labels':
value = _label_value(_label_names(eff.get('labels')))
for h in hashes:
labels_by_hash[h] = _label_names(value); c.call('d.custom1.set', h, value)
applied.append({'type': 'set_labels', 'labels': value, 'count': len(hashes)})
target_labels = _label_names(value)
# Note: Set-labels skips torrents whose current label list already matches the requested list.
target_hashes = [h for h in hashes if labels_by_hash.get(h, []) != target_labels]
for h in target_hashes:
labels_by_hash[h] = list(target_labels); c.call('d.custom1.set', h, value)
if target_hashes:
applied.append({'type': 'set_labels', 'labels': value, 'count': len(target_hashes), 'target_hashes': target_hashes})
elif typ in {'pause', 'stop', 'start', 'resume', 'recheck'}:
result = rtorrent.action(profile, hashes, typ, {})
applied.append({'type': typ, 'count': len(hashes), 'result': result})
applied.append({'type': typ, 'count': len(hashes), 'target_hashes': hashes, 'result': result})
return applied
@@ -213,14 +222,20 @@ def check(profile: dict | None = None, user_id: int | None = None, force: bool =
try:
actions = _apply_effects_bulk(c, profile, matched, rule.get('effects') or [])
except Exception as exc:
actions = [{'error': str(exc), 'count': len(hashes)}]
for t in matched:
h = str(t.get('hash') or '')
actions = [{'error': str(exc), 'count': len(hashes), 'target_hashes': hashes}]
changed_hashes = sorted({h for a in actions for h in (a.get('target_hashes') or [])})
if not actions or not changed_hashes:
# Note: Matching torrents with no real action are not logged and do not restart the cooldown.
continue
matched_by_hash = {str(t.get('hash') or ''): t for t in matched}
for h in changed_hashes:
t = matched_by_hash.get(h, {})
conn.execute('INSERT INTO automation_rule_state(rule_id,profile_id,torrent_hash,last_matched_at,last_applied_at,updated_at) VALUES(?,?,?,?,?,?) ON CONFLICT(rule_id,profile_id,torrent_hash) DO UPDATE SET last_matched_at=excluded.last_matched_at, last_applied_at=excluded.last_applied_at, updated_at=excluded.updated_at', (rule['id'], profile_id, h, now, now, now))
applied.append({'rule_id': rule['id'], 'rule_name': rule.get('name'), 'hash': h, 'name': t.get('name'), 'actions': [{'type': a.get('type', 'error'), 'count': a.get('count', len(hashes))} for a in actions]})
applied.append({'rule_id': rule['id'], 'rule_name': rule.get('name'), 'hash': h, 'name': t.get('name'), 'actions': [{'type': a.get('type', 'error'), 'count': a.get('count', len(changed_hashes))} for a in actions]})
_mark_rule_cooldown(conn, rule, profile_id, now)
torrent_name = str(matched[0].get('name') or '') if len(matched) == 1 else f'{len(matched)} torrents'
torrent_hash = hashes[0] if len(hashes) == 1 else f'batch:{rule["id"]}:{now}'
conn.execute('INSERT INTO automation_history(user_id,profile_id,rule_id,torrent_hash,torrent_name,rule_name,actions_json,created_at) VALUES(?,?,?,?,?,?,?,?)', (user_id, profile_id, rule['id'], torrent_hash, torrent_name, str(rule.get('name') or ''), json.dumps(actions), now))
batches.append({'rule_id': rule['id'], 'rule_name': rule.get('name'), 'count': len(hashes), 'actions': actions})
torrent_name = str(matched_by_hash.get(changed_hashes[0], {}).get('name') or '') if len(changed_hashes) == 1 else f'{len(changed_hashes)} torrents'
torrent_hash = changed_hashes[0] if len(changed_hashes) == 1 else f'batch:{rule["id"]}:{now}'
history_actions = [{k: v for k, v in a.items() if k != 'target_hashes'} for a in actions]
conn.execute('INSERT INTO automation_history(user_id,profile_id,rule_id,torrent_hash,torrent_name,rule_name,actions_json,created_at) VALUES(?,?,?,?,?,?,?,?)', (user_id, profile_id, rule['id'], torrent_hash, torrent_name, str(rule.get('name') or ''), json.dumps(history_actions), now))
batches.append({'rule_id': rule['id'], 'rule_name': rule.get('name'), 'count': len(changed_hashes), 'actions': history_actions})
return {'ok': True, 'checked': len(torrents), 'rules': len(rules), 'applied': applied, 'batches': batches}