Skip to content

Commit

Permalink
implement --count-disregards-eligibility and --first-only-disregard-e…
Browse files Browse the repository at this point in the history
…ligibility

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 mlj#26
  • Loading branch information
jetmore committed Jan 22, 2023
1 parent c8c212c commit 5224f3f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
8 changes: 7 additions & 1 deletion castget.1.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ options as arguments.
* `-s` <count>, `--stop-after`=<count>:
Stop after <count> 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` <pattern>, `--filter`=<pattern>:
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.
Expand Down
20 changes: 20 additions & 0 deletions src/castget.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
16 changes: 11 additions & 5 deletions src/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}

Expand All @@ -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;
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down

0 comments on commit 5224f3f

Please sign in to comment.