From 5224f3f319008e7d39ed262f59b04b5c099ed577 Mon Sep 17 00:00:00 2001 From: John Jetmore Date: Sat, 21 Jan 2023 20:01:27 -0500 Subject: [PATCH] implement --count-disregards-eligibility and --first-only-disregard-eligibility with this change, `--first-only --count-disregards-eligibility` will now cease channel processing after it sees the first item in the channel, regardless of whether that item was actually eligible for processing --first-only-disregard-eligibility is just a convenience for `--first-only --count-disregards-eligibility` fixes #26 --- castget.1.ronn | 8 +++++++- src/castget.c | 20 ++++++++++++++++++++ src/channel.c | 16 +++++++++++----- src/channel.h | 2 ++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/castget.1.ronn b/castget.1.ronn index 0f114e3..2a7cac2 100644 --- a/castget.1.ronn +++ b/castget.1.ronn @@ -47,8 +47,14 @@ options as arguments. * `-s` , `--stop-after`=: Stop after items are processed. This is implemented per-channel. + * `-x`, `--count-disregards-eligibility`: + By default, `--stop-after` only counts items that castget considers eligible for processing (e.g. matches filters and has not been previously downloaded (or marked as downloaded with `--catchup`)). This option modifies `--stop-after` so that the count applies to all items seen, regardless of whether the item was eligible for processing. + * `-1`, `--first-only`: - Restrict operation to the most recent item in each channel only. Equivalent to --stop-after=1. + Restrict operation to the most recent item in each channel only. Equivalent to `--stop-after=1`. + + * `-o`, `--first-only-disregards-eligibility`: + Stop processing when one item has been seen regardless of item's eligibility for processing. Equivalent to `--stop-after=1 --count-disregards-eligibility`. * `-f` , `--filter`=: Restrict operation to enclosures whose download URLs match the regular expression `pattern`. Note that this will override any regular expression filters given in the configuration file. diff --git a/src/castget.c b/src/castget.c index 909c277..89ae78c 100644 --- a/src/castget.c +++ b/src/castget.c @@ -69,7 +69,9 @@ int main(int argc, char **argv) GError *error = NULL; GOptionContext *context; static gboolean first_only = FALSE; + static gboolean first_only_disregard_eligibility = FALSE; static gint stop_after_count = 0; + static gboolean count_disregards_eligibility = FALSE; static gboolean resume = FALSE; static gboolean debug = FALSE; static gboolean reverse = FALSE; @@ -105,8 +107,12 @@ int main(int argc, char **argv) { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, "only print error messages" }, { "stop-after", 's', 0, G_OPTION_ARG_INT, &stop_after_count, "stop after processing ARGUMENT items from each channel" }, + { "count-disregards-eligibility", 'x', 0, G_OPTION_ARG_NONE, &count_disregards_eligibility, + "when processing --stop-after, count considers all items, not just those eligible" }, { "first-only", '1', 0, G_OPTION_ARG_NONE, &first_only, "only process the most recent item from each channel" }, + { "first-only-disregard-eligibility", 'c', 0, G_OPTION_ARG_NONE, &first_only_disregard_eligibility, + "only process the most recent item from each channel" }, { "reverse", 'R', 0, G_OPTION_ARG_NONE, &reverse, "process the channel in reverse order" }, { "filter", 'f', 0, G_OPTION_ARG_STRING, &filter_regex, @@ -145,8 +151,21 @@ int main(int argc, char **argv) g_print("--stop-after and --first-only are incompatible\n"); exit(1); } + if (first_only_disregard_eligibility && stop_after_count) { + g_print("--stop-after and --first-only-disregard-eligibility are incompatible\n"); + exit(1); + } + if (first_only_disregard_eligibility && first_only) { + g_print("--first-only and --first-only-disregard-eligibility are incompatible\n"); + exit(1); + } + if (first_only) stop_after_count = 1; + if (first_only_disregard_eligibility) { + stop_after_count = 1; + count_disregards_eligibility = TRUE; + } /* Decide on the action to take */ if (show_version) { @@ -158,6 +177,7 @@ int main(int argc, char **argv) opts->no_download = 0; opts->no_mark_read = 0; opts->stop_after_count = stop_after_count; + opts->count_disregards_eligibility = count_disregards_eligibility; opts->resume = resume; opts->debug = debug; opts->reverse = reverse; diff --git a/src/channel.c b/src/channel.c index 9357a53..1b1cfc4 100644 --- a/src/channel.c +++ b/src/channel.c @@ -343,13 +343,17 @@ int channel_update(channel *c, void *user_data, channel_callback cb, _cast_channel_save(c, opts->debug); } - - /* If we have been instructed to deal only with the first - available enclosure, it is time to break out of the loop. */ - if (opts->stop_after_count && eligible_items_seen >= opts->stop_after_count) - break; } } + + /* If we have been instructed to only process a certain number of items + and we have seen that number, exit the loop */ + if (opts->stop_after_count) { + int check_index = (opts->count_disregards_eligibility) ? index.iteration : eligible_items_seen; + if (check_index >= opts->stop_after_count) { + break; +} + } } } @@ -372,6 +376,7 @@ int channel_update(channel *c, void *user_data, channel_callback cb, void _initialize_index(channel_index *index, int num_items, int reverse) { index->ended = 0; index->reverse = reverse; + index->iteration = 0; if (reverse) { index->start = num_items -1; index->stop = 0; @@ -387,6 +392,7 @@ int _next_index(channel_index *index) { if (index->ended) return 0; + index->iteration++; if (index->reverse) { index->current--; if (index->current < index->stop) { diff --git a/src/channel.h b/src/channel.h index 40b692f..ad61928 100644 --- a/src/channel.h +++ b/src/channel.h @@ -60,6 +60,7 @@ typedef struct _option_info { int no_download; int no_mark_read; int stop_after_count; + int count_disregards_eligibility; int reverse; int resume; enclosure_filter *filter; @@ -73,6 +74,7 @@ typedef struct _channel_index { int reverse; int current; int ended; + int iteration; } channel_index; typedef void (*channel_callback)(void *user_data, channel_action action,