From e6e441ab1d7f9f02be8f935bfff4391b2ce215f2 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 23 Oct 2022 14:52:25 +0200 Subject: Add search option to cleanup argument --- .gitignore | 1 + README.md | 12 +++++------- automedia | Bin 124944 -> 0 bytes build.sh | 9 +++++++-- src/main.c | 57 ++++++++++++++++++++++++++++++++++++++++++++------------- 5 files changed, 57 insertions(+), 22 deletions(-) delete mode 100755 automedia diff --git a/.gitignore b/.gitignore index de45a2e..87fef7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode/ __pycache__ .gdb_history +automedia diff --git a/README.md b/README.md index a2ea121..62f66a9 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,10 @@ # AutoMedia -Automatically track new releases of media and download them. Currently works with rss for torrent sites (`nyaa.si`) and for these manga sites: `manganelo.com`, `manganelos.com`, `mangatown.com`, `mangakakalot.com`, `mangakatana.com`, `lhtranslation.net`, `readm.org`, `mangaplus.shueisha.co.jp` and `mangadex.org`. -A notification is shown on the screen when a download finishes (if notify-send is installed). - +Automatically track new releases of media and download them. Currently works with rss for torrent sites (`nyaa.si`) and for these manga sites: `manganelo.com`, `manganelos.com`, `mangatown.com`, `mangakakalot.com`, `mangakatana.com`, `lhtranslation.net`, `readm.org`, `mangaplus.shueisha.co.jp` and `mangadex.org`.\ +A notification is shown on the screen when a download finishes (if notify-send is installed).\ +AutoMedia checks and downloads updates every 15 minutes. Torrents stop seeding after a ratio of 2.0.\ ## Installation -Run `sudo ./install.sh` to install AutoMedia. If you are running Arch Linux, then you can find AutoMedia on aur under the name automedia-git (`yay -S automedia-git`). - -AutoMedia checks and downloads updates every 15 minutes. -Torrents stop seeding after a ratio of 2.0. +Run `sudo ./install.sh` to install AutoMedia. It will build automedia with musl if installed, otherwise it will use gcc/clang (whichever you have configured as the default). If you are running Arch Linux, then you can find AutoMedia on aur under the name automedia-git (`yay -S automedia-git`).\ +There is also a prebuilt binary of the latest commit for x86_64 systems available at https://dec05eba.com/files/automedia. ## Usage Run automedia with `sync` option and keep it running to track media. You can then use `add` option to add new media to track.\ Removing media from being synced can be done by removing the tracked directory in `~/.config/automedia/rss/tracked` or `~/.config/automedia/html/tracked` or by using `automedia cleanup`.\ diff --git a/automedia b/automedia deleted file mode 100755 index 5d30f75..0000000 Binary files a/automedia and /dev/null differ diff --git a/build.sh b/build.sh index 6b88949..d32c9fa 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,10 @@ #!/bin/sh -CFLAGS="-O3 -s -flto -Wall -Wextra -Werror" +CFLAGS="-O3 -s -flto -Wall -Wextra -Werror -DNDEBUG" [ -z "$RELEASE" ] && CFLAGS="-O0 -g3 -Wall -Wextra -Werror"; -musl-gcc -static src/main.c src/program.c src/alloc.c src/buffer.c src/fileutils.c src/transmission.c src/rss.c src/html.c src/rss_html_common.c src/download.c src/stringutils.c src/episode.c src/track_remove_parser.c depends/cJSON.c -o automedia $CFLAGS +CC=cc +if [ $(which musl-gcc 2> /dev/null) ]; then + CC="musl-gcc" + CFLAGS+=" -static" +fi +$CC src/main.c src/program.c src/alloc.c src/buffer.c src/fileutils.c src/transmission.c src/rss.c src/html.c src/rss_html_common.c src/download.c src/stringutils.c src/episode.c src/track_remove_parser.c depends/cJSON.c -o automedia $CFLAGS diff --git a/src/main.c b/src/main.c index ea54701..34895c6 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,5 @@ +/* For strcasestr */ +#define _GNU_SOURCE #include "buffer.h" #include "fileutils.h" #include "transmission.h" @@ -76,11 +78,13 @@ static void usage_downloaded(void) { } static void usage_cleanup(void) { - fprintf(stderr, "usage: automedia cleanup \n"); + fprintf(stderr, "usage: automedia cleanup [-d ] [search_term]\n"); fprintf(stderr, "OPTIONS\n"); - fprintf(stderr, " days Media that haven't received any updates in the specified amount of days are removed\n"); + fprintf(stderr, " -d Media that haven't received any updates in the specified amount of days will be shown. If not specified then all media will be included\n"); + fprintf(stderr, " search_term The name of the media to find. If not inclued then all media (within -d days) will be included. Note this is case insensitive\n"); fprintf(stderr, "EXAMPLES\n"); - fprintf(stderr, " automedia cleanup 100\n"); + fprintf(stderr, " automedia cleanup -d 100\n"); + fprintf(stderr, " automedia cleanup baki\n"); exit(1); } @@ -207,7 +211,7 @@ typedef struct { time_t updated_time; } TrackedItemData; -static void get_tracked_items(const char *tracked_dir, time_t age_sec, Buffer /*TrackedItemData*/ *tracked_items_buffer) { +static void get_tracked_items(const char *tracked_dir, time_t age_sec, const char *search_term, Buffer /*TrackedItemData*/ *tracked_items_buffer) { struct dirent *dir; DIR *d = opendir(tracked_dir); if(!d) { @@ -224,13 +228,18 @@ static void get_tracked_items(const char *tracked_dir, time_t age_sec, Buffer /* char *file_data; long file_size; + if(!search_term) + search_term = ""; + while((dir = readdir(d)) != NULL) { int filename_len = strlen(dir->d_name); if((filename_len == 1 && dir->d_name[0] == '.') || (filename_len == 2 && dir->d_name[0] == '.' && dir->d_name[1] == '.')) continue; - strcpy(data_filepath + data_filepath_length, dir->d_name); + if(search_term[0] != '\0' && !strcasestr(dir->d_name, search_term)) + continue; + strcpy(data_filepath + data_filepath_length, dir->d_name); strcpy(data_filepath + data_filepath_length + filename_len, "/updated"); if(file_get_content(data_filepath, &file_data, &file_size) != 0) { fprintf(stderr, "Failed to read the content of file %s\n", data_filepath); @@ -722,12 +731,31 @@ static int compare_tracked_item(const void *a, const void *b) { static void command_cleanup(int argc, char **argv, const char *rss_config_dir, const char *html_config_dir) { time_t days = 0; - if(argc >= 1) { - errno = 0; - days = strtol(argv[0], NULL, 0); - if(errno || days < 0) { - fprintf(stderr, "Invalid value for days argument %s, expected a positive number\n", argv[0]); + const char *search_term = NULL; + + for(int i = 0; i < argc; ++i) { + if(strcmp(argv[i], "-d") == 0) { + if(i == argc - 1) { + fprintf(stderr, "Error: missing argument after -d\n"); + usage_cleanup(); + } + + errno = 0; + days = strtol(argv[i + 1], NULL, 0); + if(errno || days < 0) { + fprintf(stderr, "Error: invalid value for days argument %s, expected a positive number\n", argv[0]); + usage_cleanup(); + } + + ++i; + } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { usage_cleanup(); + } else { + if(search_term) { + fprintf(stderr, "Error: search term specified multiple times. First as \"%s\" then as \"%s\"\n", search_term, argv[i]); + usage_cleanup(); + } + search_term = argv[i]; } } @@ -744,9 +772,9 @@ static void command_cleanup(int argc, char **argv, const char *rss_config_dir, c int num_rss_items = 0; const time_t age_sec = 60 * 60 * 24 * days; - get_tracked_items(rss_tracked_dir, age_sec, &tracked_items); + get_tracked_items(rss_tracked_dir, age_sec, search_term, &tracked_items); num_rss_items = buffer_get_size(&tracked_items, sizeof(TrackedItemData)); - get_tracked_items(html_tracked_dir, age_sec, &tracked_items); + get_tracked_items(html_tracked_dir, age_sec, search_term, &tracked_items); int num_tracked_items = buffer_get_size(&tracked_items, sizeof(TrackedItemData)); qsort(tracked_items.data, num_rss_items, sizeof(TrackedItemData), compare_tracked_item); @@ -770,7 +798,10 @@ static void command_cleanup(int argc, char **argv, const char *rss_config_dir, c buffer_init(&track_remove_ranges); if(tracked_items.size == 0) { - fprintf(stderr, "There is no media that hasn't been updated in the last %ld %s\n", days, days == 1 ? "day" : "days"); + if(search_term) + fprintf(stderr, "There is no media that hasn't been updated in the last %ld %s and matches the search term \"%s\"\n", days, days == 1 ? "day" : "days", search_term); + else + fprintf(stderr, "There is no media that hasn't been updated in the last %ld %s\n", days, days == 1 ? "day" : "days"); goto cleanup; } -- cgit v1.2.3