Skip to content

Commit

Permalink
Allow inverting filter text. Fix #266
Browse files Browse the repository at this point in the history
  • Loading branch information
timwhite committed Jul 12, 2024
1 parent e4f060d commit 4749193
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 11 deletions.
31 changes: 23 additions & 8 deletions tubesync/sync/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 9 additions & 0 deletions tubesync/sync/migrations/0023_media_duration_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
),
]
5 changes: 5 additions & 0 deletions tubesync/sync/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ class IndexSchedule(models.IntegerChoices):
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,
Expand Down
5 changes: 3 additions & 2 deletions tubesync/sync/templates/sync/source.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ <h1 class="truncate">Source <strong>{{ source.name }}</strong></h1>
<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">
Expand Down
24 changes: 24 additions & 0 deletions tubesync/sync/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,30 @@ def test_filter_filter_text_match(self):
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
Expand Down
2 changes: 1 addition & 1 deletion tubesync/sync/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def get_success_url(self):

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',
Expand Down

0 comments on commit 4749193

Please sign in to comment.