labels and automatizations
This commit is contained in:
@@ -111,8 +111,11 @@ def _conditions_match(conn, rule: dict[str, Any], profile_id: int, t: dict[str,
|
|||||||
if not h: return False
|
if not h: return False
|
||||||
immediate_ok = True; delayed_ok = True; now = utcnow(); now_ts = _now_ts()
|
immediate_ok = True; delayed_ok = True; now = utcnow(); now_ts = _now_ts()
|
||||||
for cond in rule.get('conditions') or []:
|
for cond in rule.get('conditions') or []:
|
||||||
ok = _condition_true(t, cond)
|
raw_ok = _condition_true(t, cond)
|
||||||
if cond.get('type') == 'no_seeds' and int(cond.get('minutes') or 0) > 0:
|
negated = bool(cond.get('negate'))
|
||||||
|
ok = (not raw_ok) if negated else raw_ok
|
||||||
|
# Note: Conditions can now be negated in automation rules. Timed no-seeds keeps its old delayed behavior only for the positive condition, so old rules do not change.
|
||||||
|
if cond.get('type') == 'no_seeds' and int(cond.get('minutes') or 0) > 0 and not negated:
|
||||||
row = conn.execute('SELECT condition_since_at FROM automation_rule_state WHERE rule_id=? AND profile_id=? AND torrent_hash=?', (rule['id'], profile_id, h)).fetchone()
|
row = conn.execute('SELECT condition_since_at FROM automation_rule_state WHERE rule_id=? AND profile_id=? AND torrent_hash=?', (rule['id'], profile_id, h)).fetchone()
|
||||||
if ok:
|
if ok:
|
||||||
since = row['condition_since_at'] if row and row.get('condition_since_at') else now
|
since = row['condition_since_at'] if row and row.get('condition_since_at') else now
|
||||||
|
|||||||
@@ -132,6 +132,39 @@ def _excluded_hashes(profile_id: int, user_id: int) -> set[str]:
|
|||||||
return {r['torrent_hash'] for r in list_exclusions(profile_id, user_id)}
|
return {r['torrent_hash'] for r in list_exclusions(profile_id, user_id)}
|
||||||
|
|
||||||
|
|
||||||
|
def _label_names(value: str | None) -> list[str]:
|
||||||
|
names: list[str] = []
|
||||||
|
for part in str(value or '').replace(';', ',').replace('|', ',').split(','):
|
||||||
|
label = part.strip()
|
||||||
|
if label and label not in names:
|
||||||
|
names.append(label)
|
||||||
|
return names
|
||||||
|
|
||||||
|
|
||||||
|
def _label_value(labels: list[str]) -> str:
|
||||||
|
out: list[str] = []
|
||||||
|
for label in labels:
|
||||||
|
label = str(label or '').strip()
|
||||||
|
if label and label not in out:
|
||||||
|
out.append(label)
|
||||||
|
return ', '.join(out)
|
||||||
|
|
||||||
|
|
||||||
|
def _has_smart_queue_label(value: str | None) -> bool:
|
||||||
|
return SMART_QUEUE_LABEL in _label_names(value)
|
||||||
|
|
||||||
|
|
||||||
|
def _without_smart_queue_label(value: str | None) -> str:
|
||||||
|
return _label_value([label for label in _label_names(value) if label != SMART_QUEUE_LABEL])
|
||||||
|
|
||||||
|
|
||||||
|
def _with_smart_queue_label(value: str | None) -> str:
|
||||||
|
labels = _label_names(value)
|
||||||
|
if SMART_QUEUE_LABEL not in labels:
|
||||||
|
labels.append(SMART_QUEUE_LABEL)
|
||||||
|
return _label_value(labels)
|
||||||
|
|
||||||
|
|
||||||
def _remember_auto_label(profile_id: int, torrent_hash: str, previous_label: str) -> None:
|
def _remember_auto_label(profile_id: int, torrent_hash: str, previous_label: str) -> None:
|
||||||
now = utcnow()
|
now = utcnow()
|
||||||
with connect() as conn:
|
with connect() as conn:
|
||||||
@@ -165,20 +198,17 @@ def _restore_auto_label(client: Any, profile_id: int, torrent_hash: str, current
|
|||||||
(profile_id, torrent_hash),
|
(profile_id, torrent_hash),
|
||||||
).fetchone()
|
).fetchone()
|
||||||
live_label = _read_label(client, torrent_hash, current_label or '')
|
live_label = _read_label(client, torrent_hash, current_label or '')
|
||||||
if not row:
|
if not row and not _has_smart_queue_label(live_label):
|
||||||
if live_label != SMART_QUEUE_LABEL:
|
|
||||||
return False
|
return False
|
||||||
|
restored = _without_smart_queue_label(live_label)
|
||||||
|
previous = _without_smart_queue_label((row or {}).get('previous_label') or '')
|
||||||
|
if not restored and previous:
|
||||||
|
restored = previous
|
||||||
try:
|
try:
|
||||||
# Note: Clear the Smart Queue label even when the torrent was marked earlier but no previous-label entry remains.
|
# Note: Smart Queue now removes only its technical label, preserving labels added manually while the torrent was waiting in the queue.
|
||||||
client.call('d.custom1.set', torrent_hash, '')
|
if _has_smart_queue_label(live_label) or current_label is None:
|
||||||
return True
|
client.call('d.custom1.set', torrent_hash, restored)
|
||||||
except Exception:
|
if row:
|
||||||
return False
|
|
||||||
previous = row.get('previous_label') or ''
|
|
||||||
try:
|
|
||||||
# Note: On resume, Smart Queue restores the previous label only while it still sees its own technical label.
|
|
||||||
if live_label == SMART_QUEUE_LABEL or current_label is None:
|
|
||||||
client.call('d.custom1.set', torrent_hash, previous)
|
|
||||||
conn.execute('DELETE FROM smart_queue_auto_labels WHERE profile_id=? AND torrent_hash=?', (profile_id, torrent_hash))
|
conn.execute('DELETE FROM smart_queue_auto_labels WHERE profile_id=? AND torrent_hash=?', (profile_id, torrent_hash))
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -282,10 +312,12 @@ def _read_live_start_state(client: Any, torrent_hash: str) -> dict[str, Any]:
|
|||||||
result['started'] = bool(int(result.get('active') or 0))
|
result['started'] = bool(int(result.get('active') or 0))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _set_smart_queue_label(client: Any, torrent_hash: str, attempts: int = 3) -> bool:
|
def _set_smart_queue_label(client: Any, torrent_hash: str, current_label: str | None = None, attempts: int = 3) -> bool:
|
||||||
|
# Note: The queue label is appended as a technical label instead of replacing the user's labels.
|
||||||
|
target = _with_smart_queue_label(current_label if current_label is not None else _read_label(client, torrent_hash, ''))
|
||||||
for attempt in range(max(1, attempts)):
|
for attempt in range(max(1, attempts)):
|
||||||
try:
|
try:
|
||||||
client.call('d.custom1.set', torrent_hash, SMART_QUEUE_LABEL)
|
client.call('d.custom1.set', torrent_hash, target)
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
if attempt < attempts - 1:
|
if attempt < attempts - 1:
|
||||||
@@ -298,15 +330,15 @@ def _mark_auto_paused(client: Any, profile_id: int, torrent: dict[str, Any]) ->
|
|||||||
if not torrent_hash:
|
if not torrent_hash:
|
||||||
return False
|
return False
|
||||||
previous = str(torrent.get('label') or '')
|
previous = str(torrent.get('label') or '')
|
||||||
if previous != SMART_QUEUE_LABEL:
|
if not _has_smart_queue_label(previous):
|
||||||
_remember_auto_label(profile_id, torrent_hash, previous)
|
_remember_auto_label(profile_id, torrent_hash, _without_smart_queue_label(previous))
|
||||||
return _set_smart_queue_label(client, torrent_hash)
|
return _set_smart_queue_label(client, torrent_hash, previous)
|
||||||
|
|
||||||
|
|
||||||
def _is_smart_queue_hold(torrent: dict[str, Any] | None, manage_stopped: bool = True) -> bool:
|
def _is_smart_queue_hold(torrent: dict[str, Any] | None, manage_stopped: bool = True) -> bool:
|
||||||
if not torrent or int(torrent.get('complete') or 0):
|
if not torrent or int(torrent.get('complete') or 0):
|
||||||
return False
|
return False
|
||||||
if str(torrent.get('label') or '') == SMART_QUEUE_LABEL:
|
if _has_smart_queue_label(torrent.get('label')):
|
||||||
return True
|
return True
|
||||||
# Note: Paused in rTorrent usually has state=1 and active=0, so state=0 must not be required.
|
# Note: Paused in rTorrent usually has state=1 and active=0, so state=0 must not be required.
|
||||||
# This lets Smart Queue treat paused torrents as pending and fill the queue target later.
|
# This lets Smart Queue treat paused torrents as pending and fill the queue target later.
|
||||||
@@ -319,11 +351,11 @@ def _is_smart_queue_hold(torrent: dict[str, Any] | None, manage_stopped: bool =
|
|||||||
|
|
||||||
|
|
||||||
def _clear_untracked_smart_queue_label(client: Any, torrent_hash: str, current_label: str) -> bool:
|
def _clear_untracked_smart_queue_label(client: Any, torrent_hash: str, current_label: str) -> bool:
|
||||||
if current_label != SMART_QUEUE_LABEL:
|
if not _has_smart_queue_label(current_label):
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
# Note: Clear an orphaned Smart Queue label when no previous-label entry exists in the database.
|
# Note: Orphan cleanup removes only the Smart Queue technical label and keeps manual labels intact.
|
||||||
client.call('d.custom1.set', torrent_hash, '')
|
client.call('d.custom1.set', torrent_hash, _without_smart_queue_label(current_label))
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
@@ -346,7 +378,7 @@ def _cleanup_auto_labels(client: Any, profile_id: int, torrents: list[dict[str,
|
|||||||
if _restore_auto_label(client, profile_id, h, None if t is None else current_label):
|
if _restore_auto_label(client, profile_id, h, None if t is None else current_label):
|
||||||
restored.append(h)
|
restored.append(h)
|
||||||
continue
|
continue
|
||||||
if current_label != SMART_QUEUE_LABEL:
|
if not _has_smart_queue_label(current_label):
|
||||||
_set_smart_queue_label(client, h)
|
_set_smart_queue_label(client, h)
|
||||||
|
|
||||||
for h, t in by_hash.items():
|
for h, t in by_hash.items():
|
||||||
@@ -363,7 +395,7 @@ def _is_running_download_slot(t: dict[str, Any]) -> bool:
|
|||||||
# Paused can have state=1/open=1, so a slot is counted only after d.is_active=1.
|
# Paused can have state=1/open=1, so a slot is counted only after d.is_active=1.
|
||||||
if int(t.get('complete') or 0):
|
if int(t.get('complete') or 0):
|
||||||
return False
|
return False
|
||||||
if str(t.get('label') or '') == SMART_QUEUE_LABEL:
|
if _has_smart_queue_label(t.get('label')):
|
||||||
return False
|
return False
|
||||||
status = str(t.get('status') or '').lower()
|
status = str(t.get('status') or '').lower()
|
||||||
if status == 'checking' or status == 'paused' or bool(t.get('paused')):
|
if status == 'checking' or status == 'paused' or bool(t.get('paused')):
|
||||||
@@ -375,7 +407,7 @@ def _is_waiting_download_candidate(t: dict[str, Any], manage_stopped: bool) -> b
|
|||||||
"""Return True for paused/held torrents Smart Queue may resume later."""
|
"""Return True for paused/held torrents Smart Queue may resume later."""
|
||||||
if int(t.get('complete') or 0):
|
if int(t.get('complete') or 0):
|
||||||
return False
|
return False
|
||||||
if str(t.get('label') or '') == SMART_QUEUE_LABEL:
|
if _has_smart_queue_label(t.get('label')):
|
||||||
return True
|
return True
|
||||||
# Note: Paused items are the primary source for filling the queue, regardless of manage_stopped.
|
# Note: Paused items are the primary source for filling the queue, regardless of manage_stopped.
|
||||||
if bool(t.get('paused')) or str(t.get('status') or '').lower() == 'paused':
|
if bool(t.get('paused')) or str(t.get('status') or '').lower() == 'paused':
|
||||||
@@ -406,7 +438,7 @@ def check(profile: dict | None = None, user_id: int | None = None, force: bool =
|
|||||||
excluded = _excluded_hashes(profile_id, user_id)
|
excluded = _excluded_hashes(profile_id, user_id)
|
||||||
manage_stopped = bool(settings.get('manage_stopped'))
|
manage_stopped = bool(settings.get('manage_stopped'))
|
||||||
def is_managed_hold(t: dict[str, Any]) -> bool:
|
def is_managed_hold(t: dict[str, Any]) -> bool:
|
||||||
return str(t.get('label') or '') == SMART_QUEUE_LABEL
|
return _has_smart_queue_label(t.get('label'))
|
||||||
|
|
||||||
# Note: Count Smart Queue slots by d.is_active because Paused can have state=1/open=1 and must not occupy the limit.
|
# Note: Count Smart Queue slots by d.is_active because Paused can have state=1/open=1 and must not occupy the limit.
|
||||||
downloading = [
|
downloading = [
|
||||||
@@ -538,7 +570,7 @@ def check(profile: dict | None = None, user_id: int | None = None, force: bool =
|
|||||||
keep_labels = (
|
keep_labels = (
|
||||||
set(paused)
|
set(paused)
|
||||||
| {str(t.get('hash') or '') for t in to_label_waiting}
|
| {str(t.get('hash') or '') for t in to_label_waiting}
|
||||||
| {str(t.get('hash') or '') for t in stopped if str(t.get('label') or '') == SMART_QUEUE_LABEL and str(t.get('hash') or '') not in set(resumed)}
|
| {str(t.get('hash') or '') for t in stopped if _has_smart_queue_label(t.get('label')) and str(t.get('hash') or '') not in set(resumed)}
|
||||||
)
|
)
|
||||||
restored = _cleanup_auto_labels(c, profile_id, torrents, keep_labels, manage_stopped)
|
restored = _cleanup_auto_labels(c, profile_id, torrents, keep_labels, manage_stopped)
|
||||||
details = {'excluded': len(excluded), 'enabled': bool(settings.get('enabled')), 'auto_label': SMART_QUEUE_LABEL, 'labels_restored': restored, 'labels_failed': label_failed, 'start_failed': start_failed, 'start_no_effect': start_no_effect, 'start_results': start_results, 'resume_requested': resume_requested, 'active_verified': active_verified, 'waiting_labeled': len(to_label_waiting), 'manage_stopped': manage_stopped, 'max_active_downloads': max_active, 'active_before': len(downloading), 'active_after_expected': active_after_pause + len(resumed), 'paused_planned': len(to_pause), 'resumed_planned': len(to_resume), 'rtorrent_cap': rtorrent_cap}
|
details = {'excluded': len(excluded), 'enabled': bool(settings.get('enabled')), 'auto_label': SMART_QUEUE_LABEL, 'labels_restored': restored, 'labels_failed': label_failed, 'start_failed': start_failed, 'start_no_effect': start_no_effect, 'start_results': start_results, 'resume_requested': resume_requested, 'active_verified': active_verified, 'waiting_labeled': len(to_label_waiting), 'manage_stopped': manage_stopped, 'max_active_downloads': max_active, 'active_before': len(downloading), 'active_after_expected': active_after_pause + len(resumed), 'paused_planned': len(to_pause), 'resumed_planned': len(to_resume), 'rtorrent_cap': rtorrent_cap}
|
||||||
|
|||||||
@@ -528,10 +528,10 @@
|
|||||||
// Note: Builder queues allow many conditions and many ordered actions in one automation without changing old single-step saves.
|
// Note: Builder queues allow many conditions and many ordered actions in one automation without changing old single-step saves.
|
||||||
let automationConditionQueue=[];
|
let automationConditionQueue=[];
|
||||||
let automationEffectQueue=[];
|
let automationEffectQueue=[];
|
||||||
function automationCondition(){ const type=$('autoConditionType')?.value||'completed'; const cond={type}; if(type==='no_seeds'){cond.seeds=Number($('autoCondSeeds')?.value||0);cond.minutes=Number($('autoCondMinutes')?.value||0);} if(type==='ratio_gte')cond.ratio=Number($('autoCondRatio')?.value||1); if(type==='label_missing'||type==='label_has')cond.label=$('autoCondLabel')?.value||''; if(type==='status')cond.status=$('autoCondStatus')?.value||'Seeding'; if(type==='path_contains')cond.text=$('autoCondText')?.value||''; return cond; }
|
function automationCondition(){ const type=$('autoConditionType')?.value||'completed'; const cond={type}; if($('autoCondNegate')?.checked) cond.negate=true; if(type==='no_seeds'){cond.seeds=Number($('autoCondSeeds')?.value||0);cond.minutes=Number($('autoCondMinutes')?.value||0);} if(type==='ratio_gte')cond.ratio=Number($('autoCondRatio')?.value||1); if(type==='label_missing'||type==='label_has')cond.label=$('autoCondLabel')?.value||''; if(type==='status')cond.status=$('autoCondStatus')?.value||'Seeding'; if(type==='path_contains')cond.text=$('autoCondText')?.value||''; return cond; }
|
||||||
function automationEffect(){ const type=$('autoEffectType')?.value||'add_label'; const eff={type}; if(type==='move'){eff.path=$('autoEffectPath')?.value||''; eff.move_data=!!($('autoMoveDataPhysical')?.checked); eff.recheck=!!($('autoMoveRecheck')?.checked); eff.keep_seeding=!!($('autoMoveKeepSeeding')?.checked);} if(type==='add_label'||type==='remove_label')eff.label=$('autoEffectLabel')?.value||''; if(type==='set_labels')eff.labels=$('autoEffectLabels')?.value||''; return eff; }
|
function automationEffect(){ const type=$('autoEffectType')?.value||'add_label'; const eff={type}; if(type==='move'){eff.path=$('autoEffectPath')?.value||''; eff.move_data=!!($('autoMoveDataPhysical')?.checked); eff.recheck=!!($('autoMoveRecheck')?.checked); eff.keep_seeding=!!($('autoMoveKeepSeeding')?.checked);} if(type==='add_label'||type==='remove_label')eff.label=$('autoEffectLabel')?.value||''; if(type==='set_labels')eff.labels=$('autoEffectLabels')?.value||''; return eff; }
|
||||||
function updateAutomationForm(){ const ct=$('autoConditionType')?.value||''; document.querySelectorAll('[data-auto-cond]').forEach(el=>el.classList.toggle('d-none', !el.dataset.autoCond.split(',').includes(ct))); const et=$('autoEffectType')?.value||''; document.querySelectorAll('[data-auto-effect]').forEach(el=>el.classList.toggle('d-none', !el.dataset.autoEffect.split(',').includes(et))); }
|
function updateAutomationForm(){ const ct=$('autoConditionType')?.value||''; document.querySelectorAll('[data-auto-cond]').forEach(el=>el.classList.toggle('d-none', !el.dataset.autoCond.split(',').includes(ct))); const et=$('autoEffectType')?.value||''; document.querySelectorAll('[data-auto-effect]').forEach(el=>el.classList.toggle('d-none', !el.dataset.autoEffect.split(',').includes(et))); }
|
||||||
function conditionSummary(c){ return c.type==='no_seeds'?`no seeds <=${c.seeds||0} for ${c.minutes||0} min`:c.type==='ratio_gte'?`ratio >= ${c.ratio}`:c.type==='label_missing'?`missing label ${c.label||''}`:c.type==='label_has'?`has label ${c.label||''}`:c.type==='status'?`status ${c.status||''}`:c.type==='path_contains'?`path contains ${c.text||''}`:'completed'; }
|
function conditionSummary(c){ const base=c.type==='no_seeds'?`no seeds <=${c.seeds||0} for ${c.minutes||0} min`:c.type==='ratio_gte'?`ratio >= ${c.ratio}`:c.type==='label_missing'?`missing label ${c.label||''}`:c.type==='label_has'?`has label ${c.label||''}`:c.type==='status'?`status ${c.status||''}`:c.type==='path_contains'?`path contains ${c.text||''}`:'completed'; return c.negate?`NOT (${base})`:base; }
|
||||||
function effectSummary(e){ return e.type==='move'?`move to ${e.path||'default path'}${e.move_data?' + data move':''}${e.keep_seeding?' + keep seeding':''}${e.recheck?' + recheck':''}`:e.type==='add_label'?`add label ${e.label||''}`:e.type==='remove_label'?`remove label ${e.label||''}`:e.type==='set_labels'?`set labels ${e.labels||''}`:e.type; }
|
function effectSummary(e){ return e.type==='move'?`move to ${e.path||'default path'}${e.move_data?' + data move':''}${e.keep_seeding?' + keep seeding':''}${e.recheck?' + recheck':''}`:e.type==='add_label'?`add label ${e.label||''}`:e.type==='remove_label'?`remove label ${e.label||''}`:e.type==='set_labels'?`set labels ${e.labels||''}`:e.type; }
|
||||||
function renderAutomationBuilder(){ const cbox=$('autoConditionsList'), ebox=$('autoEffectsList'); if(cbox) cbox.innerHTML=automationConditionQueue.length?automationConditionQueue.map((c,i)=>`<div class="automation-chip"><span>${esc(i+1)}. ${esc(conditionSummary(c))}</span><button class="btn btn-xs btn-outline-secondary auto-remove-condition" data-index="${i}" type="button">×</button></div>`).join(''):'<div class="small text-muted">No extra conditions added; current condition will be used on save.</div>'; if(ebox) ebox.innerHTML=automationEffectQueue.length?automationEffectQueue.map((e,i)=>`<div class="automation-chip"><span>${esc(i+1)}. ${esc(effectSummary(e))}</span><button class="btn btn-xs btn-outline-secondary auto-remove-effect" data-index="${i}" type="button">×</button></div>`).join(''):'<div class="small text-muted">No action sequence added; current action will be used on save.</div>'; }
|
function renderAutomationBuilder(){ const cbox=$('autoConditionsList'), ebox=$('autoEffectsList'); if(cbox) cbox.innerHTML=automationConditionQueue.length?automationConditionQueue.map((c,i)=>`<div class="automation-chip"><span>${esc(i+1)}. ${esc(conditionSummary(c))}</span><button class="btn btn-xs btn-outline-secondary auto-remove-condition" data-index="${i}" type="button">×</button></div>`).join(''):'<div class="small text-muted">No extra conditions added; current condition will be used on save.</div>'; if(ebox) ebox.innerHTML=automationEffectQueue.length?automationEffectQueue.map((e,i)=>`<div class="automation-chip"><span>${esc(i+1)}. ${esc(effectSummary(e))}</span><button class="btn btn-xs btn-outline-secondary auto-remove-effect" data-index="${i}" type="button">×</button></div>`).join(''):'<div class="small text-muted">No action sequence added; current action will be used on save.</div>'; }
|
||||||
function addAutomationCondition(){ automationConditionQueue.push(automationCondition()); renderAutomationBuilder(); }
|
function addAutomationCondition(){ automationConditionQueue.push(automationCondition()); renderAutomationBuilder(); }
|
||||||
|
|||||||
@@ -1245,7 +1245,8 @@ body.mobile-mode .mobile-card {
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.auto-move-option {
|
.auto-move-option,
|
||||||
|
.auto-condition-option {
|
||||||
gap: 0.45rem;
|
gap: 0.45rem;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user