Allow inverting filter text. Fix #266

pull/515/head
Tim 2024-07-13 07:37:10 +08:00
parent e4f060d6d4
commit 4749193edd
6 changed files with 65 additions and 11 deletions

View File

@ -62,20 +62,35 @@ def filter_filter_text(instance: Media):
if not filter_text:
return False
# We match the filter text, so don't skip downloading this
if not instance.source.filter_text_invert:
# We match the filter text, so don't skip downloading this
if instance.source.is_regex_match(instance.title):
log.info(
f"Media: {instance.source} / {instance} has a valid "
f"title filter, not marking to be skipped"
)
return False
log.info(
f"Media: {instance.source} / {instance} doesn't match "
f"title filter, marking to be skipped"
)
return True
if instance.source.is_regex_match(instance.title):
log.info(
f"Media: {instance.source} / {instance} has a valid "
f"title filter, not marking to be skipped"
f"Media: {instance.source} / {instance} matches inverted "
f"title filter, marking to be skipped"
)
return False
return True
log.info(
f"Media: {instance.source} / {instance} doesn't match "
f"title filter, marking to be skipped"
f"Media: {instance.source} / {instance} does not match the inverted "
f"title filter, not marking to be skipped"
)
return True
return False
def filter_max_cap(instance: Media):

View File

@ -50,4 +50,13 @@ class Migration(migrations.Migration):
"video greater than maximum) video duration",
),
),
migrations.AddField(
model_name="source",
name="filter_text_invert",
field=models.BooleanField(
verbose_name="invert filter text matching",
default=False,
help_text="Invert filter string regex match, skip any matching titles when selected",
),
),
]

View File

@ -298,6 +298,11 @@ class Source(models.Model):
blank=True,
help_text=_('Regex compatible filter string for video titles')
)
filter_text_invert = models.BooleanField(
_("invert filter text matching"),
default=False,
help_text="Invert filter string regex match, skip any matching titles when selected",
)
filter_seconds = models.PositiveIntegerField(
_('filter seconds'),
blank=True,

View File

@ -44,8 +44,9 @@
<td><span class="hide-on-med-and-up">Directory<br></span><strong>{{ source.directory }}</strong></td>
</tr>
<tr title="Filter text">
<td class="hide-on-small-only">Filter text</td>
<td><span class="hide-on-med-and-up">Filter text<br></span><strong>{{ source.filter_text }}</strong></td>
<td class="hide-on-small-only">Filter text{% if source.filter_text_invert %} <em>Inverted</em>{% endif %}</td>
<td><span class="hide-on-med-and-up">Filter text{% if source.filter_text_invert %} <em>Inverted</em>{% endif %}<br></span>
<strong>{{ source.filter_text }}</strong></td>
</tr>
{% if source.filter_seconds %}
<tr title="Do not download videos shorter/longer than this limit seconds">

View File

@ -777,6 +777,30 @@ class MediaFilterTestCase(TestCase):
self.assertTrue(changed)
self.assertFalse(self.media.skip)
def test_filter_filter_text_invert_nomatch(self):
# Check that if we don't match the filter text, we don't skip
self.media.source.filter_text = "No fancy stuff"
self.media.source.filter_text_invert = True
self.media.skip = True
self.media.published = timezone.make_aware(
datetime(year=2020, month=1, day=1, hour=1, minute=1, second=1)
)
changed = filter_media(self.media)
self.assertTrue(changed)
self.assertFalse(self.media.skip)
def test_filter_filter_text_invert_match(self):
# Check that if we match the filter text and do skip
self.media.source.filter_text = "(?i)No fancy stuff"
self.media.source.filter_text_invert = True
self.media.skip = False
self.media.published = timezone.make_aware(
datetime(year=2020, month=1, day=1, hour=1, minute=1, second=1)
)
changed = filter_media(self.media)
self.assertTrue(changed)
self.assertTrue(self.media.skip)
def test_filter_max_cap_skip(self):
# Check if it's older than the max_cap, we don't download it (1 second so it will always fail)
self.media.source.download_cap = 1

View File

@ -296,7 +296,7 @@ class ValidateSourceView(FormView):
class EditSourceMixin:
model = Source
fields = ('source_type', 'key', 'name', 'directory', 'filter_text', 'filter_seconds', 'filter_seconds_min',
fields = ('source_type', 'key', 'name', 'directory', 'filter_text', 'filter_text_invert', 'filter_seconds', 'filter_seconds_min',
'media_format', 'index_schedule', 'download_media', 'download_cap', 'delete_old_media',
'delete_removed_media', 'days_to_keep', 'source_resolution', 'source_vcodec',
'source_acodec', 'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_channel_images',