/* For strcasestr */ #define _GNU_SOURCE #include "buffer.h" #include "fileutils.h" #include "transmission.h" #include "fileutils.h" #include "stringutils.h" #include "program.h" #include "rss.h" #include "rss_html_common.h" #include "html.h" #include "track_remove_parser.h" #include "fallback.h" #include "../depends/cJSON.h" #include "alloc.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #define NAME_MAX_LEN 250 /* 30 minutes */ #define IN_PROGRESS_TIMEOUT_SEC 60L*30L static void usage(void) { fprintf(stderr, "usage: automedia COMMAND\n"); fprintf(stderr, "\n"); fprintf(stderr, "COMMANDS\n"); fprintf(stderr, " add Add media to track\n"); fprintf(stderr, " sync Start syncing tracked media\n"); fprintf(stderr, " downloaded List downloaded media\n"); fprintf(stderr, " cleanup Stop tracking media that has stopped getting updates (either finished or dropped media)\n"); exit(1); } static void usage_add(void) { fprintf(stderr, "usage: automedia add [--name name] [--start-after start_after]\n"); fprintf(stderr, "OPTIONS\n"); fprintf(stderr, " type The type should be either rss or html\n"); fprintf(stderr, " url The url to the rss or html\n"); fprintf(stderr, " filename The filename of an episode of an existing serie to start track. Currently only works with rss on https://nyaa.si\n"); fprintf(stderr, " --name The display name to be used for the media. Optional for rss, in which case the name will be the rss TITLE, required for html. The name can't be longer than 250 characters\n"); fprintf(stderr, " --start-after The sync should start downloading media after this item. This --start-after value should be the title of the episode/chapter (Optional, default is to start from the first item)\n"); fprintf(stderr, "EXAMPLES\n"); fprintf(stderr, " automedia add rss 'https://nyaa.si/?page=rss&q=Tejina-senpai+1080p&c=0_0&f=0&u=HorribleSubs'\n"); fprintf(stderr, " automedia add html 'https://manganelo.com/manga/read_naruto_manga_online_free3' --name Naruto\n"); fprintf(stderr, " automedia add rss '[Erai-raws] Saiki Kusuo no Psi Nan - Kanketsu-hen - 01 [1080p][Multiple Subtitle].mkv'\n"); exit(1); } static void usage_sync(void) { fprintf(stderr, "usage: automedia sync \n"); fprintf(stderr, "OPTIONS\n"); fprintf(stderr, " download_dir The path where media should be downloaded to\n"); fprintf(stderr, "EXAMPLES\n"); fprintf(stderr, " automedia sync /home/user/Downloads/automedia\n"); exit(1); } static void usage_downloaded(void) { fprintf(stderr, "usage: automedia downloaded [--include-tracked]\n"); fprintf(stderr, "OPTIONS\n"); fprintf(stderr, " --include-tracked Include tracked only episodes that have already been marked as watched and will not be downloaded (Optional, default is to not include these)\n"); fprintf(stderr, "EXAMPLES\n"); fprintf(stderr, " automedia downloaded\n"); fprintf(stderr, " automedia downloaded --include-tracked\n"); exit(1); } static void usage_cleanup(void) { fprintf(stderr, "usage: automedia cleanup [-d ] [search_term]\n"); fprintf(stderr, "OPTIONS\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 -d 100\n"); fprintf(stderr, " automedia cleanup baki\n"); exit(1); } typedef void (*DownloadedListCallback)(const char *title, double timestamp, void *userdata); static void data_file_get_downloaded(const char *dir_name, const char *data_filepath, int is_html, int include_tracked, DownloadedListCallback callback, void *userdata) { char *file_data; long file_size; if(file_get_content(data_filepath, &file_data, &file_size) != 0) { fprintf(stderr, "Failed to read the content of file %s\n", data_filepath); return; } cJSON *json_root = cJSON_ParseWithLength(file_data, file_size); if(!json_root) { fprintf(stderr, "Failed to parse file %s as json\n", data_filepath); goto cleanup; } free(file_data); file_data = NULL; if(!cJSON_IsObject(json_root)) { fprintf(stderr, "File %s contains malformed json. Expected json root element to be an object\n", data_filepath); goto cleanup; } const cJSON *downloaded_json = cJSON_GetObjectItemCaseSensitive(json_root, "downloaded"); if(!downloaded_json) { fprintf(stderr, "File %s contains malformed json. Expected json to contain \"downloaded\"\n", data_filepath); goto cleanup; } if(!cJSON_IsArray(downloaded_json)) { fprintf(stderr, "File %s contains malformed json. Expected \"downloaded\" to be an array\n", data_filepath); goto cleanup; } const cJSON *downloaded_item_json = NULL; cJSON_ArrayForEach(downloaded_item_json, downloaded_json) { if(!cJSON_IsObject(downloaded_item_json)) continue; const cJSON *filler_json = cJSON_GetObjectItemCaseSensitive(downloaded_item_json, "filler"); if(!include_tracked && cJSON_IsTrue(filler_json)) continue; const cJSON *time_json = cJSON_GetObjectItemCaseSensitive(downloaded_item_json, "time"); if(!time_json || !cJSON_IsString(time_json)) continue; const cJSON *title_json = cJSON_GetObjectItemCaseSensitive(downloaded_item_json, "title"); const cJSON *filename_json = cJSON_GetObjectItemCaseSensitive(downloaded_item_json, "filename"); /* Filename limit is 256, so this should be safe... */ char title[256]; if(filename_json && cJSON_IsString(filename_json)) { strcpy(title, filename_json->valuestring); } else if(title_json && cJSON_IsString(title_json)) { if(is_html) { snprintf(title, sizeof(title), "%s/%s", dir_name, title_json->valuestring); } else { strcpy(title, title_json->valuestring); } } else { continue; } callback(title, atof(time_json->valuestring), userdata); } cleanup: cJSON_Delete(json_root); free(file_data); } typedef struct { char *title; double timestamp; } DownloadedListData; static void downloaded_list_callback(const char *title, double timestamp, void *userdata) { DownloadedListData list_data; list_data.title = strdup(title); list_data.timestamp = timestamp; Buffer *buffer = userdata; buffer_append(buffer, &list_data, sizeof(list_data)); } static void get_downloaded_items(const char *tracked_dir, int is_html, void *userdata, int include_tracked) { struct dirent *dir; DIR *d = opendir(tracked_dir); if(!d) { fprintf(stderr, "Failed to open directory: %s\n", tracked_dir); return; } char data_filepath[PATH_MAX]; strcpy(data_filepath, tracked_dir); strcat(data_filepath, "/"); int data_filepath_length = strlen(data_filepath); 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); strcpy(data_filepath + data_filepath_length + filename_len, "/data"); data_file_get_downloaded(dir->d_name, data_filepath, is_html, include_tracked, downloaded_list_callback, userdata); } closedir(d); } static int compare_downloaded_item(const void *a, const void *b) { const DownloadedListData *list_data_a = a; const DownloadedListData *list_data_b = b; return list_data_a->timestamp - list_data_b->timestamp; } typedef struct { char *item_tracked_path; time_t updated_time; } TrackedItemData; 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) { fprintf(stderr, "Failed to open directory: %s\n", tracked_dir); return; } char data_filepath[PATH_MAX]; strcpy(data_filepath, tracked_dir); strcat(data_filepath, "/"); int data_filepath_length = strlen(data_filepath); time_t time_now = time(NULL); 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; 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); continue; } time_t updated_time = 0; sscanf(file_data, "%ld", &updated_time); free(file_data); if(time_now - updated_time <= age_sec) continue; char *p = alloc_or_crash(data_filepath_length + filename_len + 1); memcpy(p, data_filepath, data_filepath_length); memcpy(p + data_filepath_length, dir->d_name, filename_len); p[data_filepath_length + filename_len] = '\0'; TrackedItemData tracked_item_data; tracked_item_data.item_tracked_path = p; tracked_item_data.updated_time = updated_time; buffer_append(tracked_items_buffer, &tracked_item_data, sizeof(tracked_item_data)); } closedir(d); } static void command_add(int argc, char **argv, char *rss_config_dir, char *html_config_dir, char *program_dir) { if(argc < 2) usage_add(); char *media_type = argv[0]; char *media_url = argv[1]; char *media_name = NULL; char *start_after = NULL; const char *option = NULL; for(int i = 2; i < argc; ++i) { char *arg = argv[i]; if(strcmp(arg, "--name") == 0 || strcmp(arg, "--start-after") == 0) { if(option) usage_add(); option = arg; } else { if(!option) usage_add(); if(strcmp(option, "--name") == 0) media_name = arg; else if(strcmp(option, "--start-after") == 0) start_after = arg; else { fprintf(stderr, "Invalid option %s\n", option); usage_add(); } option = NULL; } } if(media_name) { string_replace(media_name, '/', '_'); media_name = strip(media_name); int media_name_len = strlen(media_name); if(media_name_len > NAME_MAX_LEN) { fprintf(stderr, "--name value can't be longer than %d characters\n", NAME_MAX_LEN); exit(1); } if(strcmp(media_name, ".") == 0 || strcmp(media_name, "..") == 0) { fprintf(stderr, "--name can't be . or ..\n"); exit(1); } } if(start_after) { string_replace(start_after, '/', '_'); start_after = strip(start_after); } media_url = strip(media_url); fallback fall; if(!fallback_load_from_file(&fall, "/usr/share/automedia/fallback.json")) { fprintf(stderr, "Error: command_add: failed to load fallbacks file (/usr/share/automedia/fallback.json)\n"); exit(1); } if(strcmp(media_type, "rss") == 0) { int res = create_directory_recursive(rss_config_dir); if(res != 0) { fprintf(stderr, "Failed to create %s, error: %s\n", rss_config_dir, strerror(res)); exit(1); } if(add_rss(media_name, media_url, rss_config_dir, start_after, &fall) != 0) exit(1); } else if(strcmp(media_type, "html") == 0) { int res = create_directory_recursive(html_config_dir); if(res != 0) { fprintf(stderr, "Failed to create %s, error: %s\n", html_config_dir, strerror(res)); exit(1); } if(add_html(media_name, media_url, html_config_dir, program_dir, start_after) != 0) exit(1); } else { fprintf(stderr, "type should be either rss or html\n"); usage_add(); } fallback_deinit(&fall); } sig_atomic_t automedia_running = 0; static void automedia_pid_signal_handler(int signum) { (void)signum; automedia_running = 0; } int is_program_running() { return automedia_running; } /* plugin is NULL for rss */ typedef int (*IterateTrackedItemCallback)(char *title, char *link, char *plugin, char *config_dir, cJSON *json_data, fallback *fall, void *userdata); static void iterate_tracked_items(char *config_dir, IterateTrackedItemCallback iterate_callback, fallback *fall, void *userdata) { /* TODO: Only iterate updated items. To make that work, sync_rss and sync_html need to be updated to update the json_object with new downloaded items (and not only temporary downloaded items) and then also only clean them up! */ char tracked_dir[PATH_MAX]; strcpy(tracked_dir, config_dir); strcat(tracked_dir, "/tracked"); struct dirent *dir; DIR *d = opendir(tracked_dir); if(!d) { fprintf(stderr, "Failed to open directory: %s\n", tracked_dir); return; } char *item_filepath = tracked_dir; strcat(item_filepath, "/"); int item_filepath_len = strlen(item_filepath); time_t time_now = time(NULL); while((dir = readdir(d)) != NULL && automedia_running) { int title_len = strlen(dir->d_name); if((title_len == 1 && dir->d_name[0] == '.') || (title_len == 2 && dir->d_name[0] == '.' && dir->d_name[1] == '.')) continue; strcpy(item_filepath + item_filepath_len, dir->d_name); strcpy(item_filepath + item_filepath_len + title_len, "/.in_progress"); time_t last_modified = 0; if(file_get_last_modified_time(item_filepath, &last_modified) == 0) { if(time_now - last_modified > IN_PROGRESS_TIMEOUT_SEC) { fprintf(stderr, "Removing in-progress item: %s\n", dir->d_name); remove(item_filepath); } else { fprintf(stderr, "Skipping in-progress item: %s\n", dir->d_name); continue; } } char *link_file_content = NULL; char *data_file_content = NULL; char *plugin_file_content = NULL; cJSON *json_data = NULL; strcpy(item_filepath + item_filepath_len + title_len, "/link"); long link_file_size = 0; int has_link = file_get_content(item_filepath, &link_file_content, &link_file_size); strcpy(item_filepath + item_filepath_len + title_len, "/data"); long data_file_size = 0; int has_data = file_get_content(item_filepath, &data_file_content, &data_file_size); strcpy(item_filepath + item_filepath_len + title_len, "/plugin"); long plugin_file_size = 0; file_get_content(item_filepath, &plugin_file_content, &plugin_file_size); if(has_link != 0 || has_data != 0) { fprintf(stderr, "Rss corrupt, link or data missing for rss %s\n", dir->d_name); goto cleanup_item; } json_data = cJSON_ParseWithLength(data_file_content, data_file_size); if(!json_data || !cJSON_IsObject(json_data)) { fprintf(stderr, "Rss corrupt for %s\n", dir->d_name); goto cleanup_item; } free(data_file_content); data_file_content = NULL; if(iterate_callback(dir->d_name, link_file_content, plugin_file_content, config_dir, json_data, fall, userdata) != 0) fprintf(stderr, "Failed to sync %s\n", dir->d_name); cleanup_item: cJSON_Delete(json_data); free(plugin_file_content); free(data_file_content); free(link_file_content); } closedir(d); } static int iterate_tracked_item_rss_callback(char *title, char *link, char *plugin, char *config_dir, cJSON *json_data, fallback *fall, void *userdata) { (void)plugin; TransmissionSession *transmission_session = userdata; TrackedRss tracked_rss; tracked_rss.title = title; tracked_rss.link = link; tracked_rss.json_data = json_data; return sync_rss(&tracked_rss, transmission_session, config_dir, fall); } typedef struct { char *program_dir; const char *download_dir; } IterateHtmlItemUserdata; static int iterate_tracked_item_html_callback(char *title, char *link, char *plugin, char *config_dir, cJSON *json_data, fallback *fall, void *userdata) { (void)fall; if(!plugin) { fprintf(stderr, "Missing plugin name for html item: %s\n", title); return -1; } IterateHtmlItemUserdata *iterate_html_item_userdata = userdata; TrackedHtml tracked_html; tracked_html.plugin = plugin; tracked_html.title = title; tracked_html.link = link; tracked_html.json_data = json_data; return sync_html(&tracked_html, iterate_html_item_userdata->program_dir, iterate_html_item_userdata->download_dir, config_dir); } static void sync_tracked_rss(TransmissionSession *transmission_session, char *rss_config_dir, fallback *fall) { iterate_tracked_items(rss_config_dir, iterate_tracked_item_rss_callback, fall, transmission_session); } static void sync_tracked_html(char *html_config_dir, char *program_dir, const char *download_dir) { IterateHtmlItemUserdata iterate_html_item_userdata; iterate_html_item_userdata.program_dir = program_dir; iterate_html_item_userdata.download_dir = download_dir; iterate_tracked_items(html_config_dir, iterate_tracked_item_html_callback, NULL, &iterate_html_item_userdata); } typedef struct { char *items; int size; const char *download_dir; } UnfinishedTorrents; static void torrent_list_check_new_downloads_callback(int id, const char *name, double percentage_done, void *userdata) { /* Sanity check, random high number */ if(id <= 0 || id >= 650000) { fprintf(stderr, "Invalid torrent id: %d\n", id); return; } id--; UnfinishedTorrents *unfinished_torrents = userdata; const char *home_dir = get_home_dir(); char download_finished_script[PATH_MAX]; strcpy(download_finished_script, home_dir); strcat(download_finished_script, "/.config/automedia/download_finished.sh"); int is_finished = (percentage_done >= 0.9999); if(is_finished) { if(id < unfinished_torrents->size && unfinished_torrents->items[id] == 1) { unfinished_torrents->items[id] = 0; const char *notify_args[] = { "notify-send", "-u", "low", "-a", "automedia", "-t", "5000", "--", "Download finished", name, NULL }; program_exec(notify_args, NULL, NULL); char item_path[PATH_MAX]; const char *components[] = { unfinished_torrents->download_dir, name }; path_join(item_path, components, 2); const char *download_finished_args[] = { download_finished_script, "rss", item_path, NULL }; if(file_exists(download_finished_script) == 0) program_exec(download_finished_args, NULL, NULL); } } else { if(id >= unfinished_torrents->size) { int prev_size = unfinished_torrents->size; unfinished_torrents->size = id + 128; unfinished_torrents->items = realloc_or_crash(unfinished_torrents->items, unfinished_torrents->size); memset(unfinished_torrents->items + prev_size, 0, unfinished_torrents->size - prev_size); } unfinished_torrents->items[id] = 1; } } static void sync_rss_html(char *rss_config_dir, char *html_config_dir, char *program_dir, const char *download_dir, int sync_rate_sec, fallback *fall) { if(transmission_is_daemon_running() != 0) { if(transmission_start_daemon(download_dir) != 0) { fprintf(stderr, "Failed to start torrent daemon\n"); exit(2); } } TransmissionSession transmission_session; if(transmission_connect(&transmission_session) != 0) { fprintf(stderr, "Failed to connect to the transmission daemon!\n"); exit(2); } /* Check for torrent progress every 15 seconds */ int check_torrent_status_rate_sec = 15; UnfinishedTorrents unfinished_torrents; unfinished_torrents.size = 1024; unfinished_torrents.items = alloc_or_crash(unfinished_torrents.size); unfinished_torrents.download_dir = download_dir; memset(unfinished_torrents.items, 0, unfinished_torrents.size); automedia_running = 1; transmission_list_torrents(&transmission_session, torrent_list_check_new_downloads_callback, &unfinished_torrents); /* running is set to 0 in SIGINT signal handler (ctrl+c) */ while(automedia_running) { sync_tracked_rss(&transmission_session, rss_config_dir, fall); sync_tracked_html(html_config_dir, program_dir, download_dir); fprintf(stderr, "Finished syncing rss and html. Syncing again in %d minutes\n", sync_rate_sec / 60); int check_count = 0; while(automedia_running && check_count < sync_rate_sec/check_torrent_status_rate_sec) { transmission_list_torrents(&transmission_session, torrent_list_check_new_downloads_callback, &unfinished_torrents); sleep(check_torrent_status_rate_sec); ++check_count; } fallback_clear_sources_to_use(fall); } free(unfinished_torrents.items); } static int cmdline_contains_str(const char *cmdline, int cmdline_len, const char *str) { const char *start = cmdline; while(cmdline - start < cmdline_len) { int arg_len = strlen(cmdline); if(arg_len == 0) return -1; if(strstr(cmdline, str)) return 0; cmdline += arg_len + 1; } return -1; } /* We can't use file_get_content because reading the size of /proc//cmdline fails (returns 0) because its not a regular file */ static int proc_read_cmdline(const char *pid_str, char *cmdline_data, int cmdline_data_size, int *cmdline_data_size_output) { assert(cmdline_data_size > 0); char cmdline_file_path[256]; snprintf(cmdline_file_path, sizeof(cmdline_file_path), "/proc/%s/cmdline", pid_str); int cmdline_fd = open(cmdline_file_path, O_RDONLY); if(cmdline_fd == -1) { fprintf(stderr, "Process %s doesn't exist\n", pid_str); return -1; } ssize_t bytes_read = read(cmdline_fd, cmdline_data, cmdline_data_size - 1); if(bytes_read == -1) { fprintf(stderr, "Failed to read %s\n", cmdline_file_path); close(cmdline_fd); return -1; } cmdline_data[bytes_read] = '\0'; *cmdline_data_size_output = bytes_read; close(cmdline_fd); return 0; } static void command_sync(int argc, char **argv, char *rss_config_dir, char *html_config_dir, char *program_dir) { if(argc < 1) usage_sync(); char *download_dir = argv[0]; const char automedia_pid_path[] = "/tmp/automedia.pid"; if(create_directory_recursive(download_dir) != 0) { fprintf(stderr, "Failed to create download directory %s\n", download_dir); exit(1); } fallback fall; if(!fallback_load_from_file(&fall, "/usr/share/automedia/fallback.json")) { fprintf(stderr, "Error: command_sync: failed to load fallbacks file (/usr/share/automedia/fallback.json)\n"); exit(1); } /* Create a pid file because we only want to allow one instance of automedia to run at once */ int pid_file = open(automedia_pid_path, O_CREAT | O_EXCL | O_SYNC | O_RDWR, 0666); if(pid_file == -1 && errno == EEXIST) { char *running_automedia_pid; long running_automedia_pid_size; if(file_get_content(automedia_pid_path, &running_automedia_pid, &running_automedia_pid_size) == 0) { /* We have to check the cmdline because another process could theoretically receive the pid that an old automedia process had */ char cmdline[1024]; int cmdline_size; if(proc_read_cmdline(running_automedia_pid, cmdline, sizeof(cmdline), &cmdline_size) == 0 && cmdline_contains_str(cmdline, cmdline_size, "automedia") == 0 && cmdline_contains_str(cmdline, cmdline_size, "sync") == 0) { fprintf(stderr, "Automedia is already running with sync\n"); free(running_automedia_pid); exit(1); } free(running_automedia_pid); } else { fprintf(stderr, "Failed to get content of %s\n", automedia_pid_path); exit(1); } fprintf(stderr, "Overwriting existing %s\n", automedia_pid_path); unlink(automedia_pid_path); pid_file = open(automedia_pid_path, O_CREAT | O_EXCL | O_SYNC | O_RDWR, 0666); } if(pid_file == -1 && errno != EEXIST) { fprintf(stderr, "Failed to create %s\n", automedia_pid_path); exit(1); } signal(SIGINT, automedia_pid_signal_handler); signal(SIGTERM, automedia_pid_signal_handler); char process_pid_str[32]; snprintf(process_pid_str, sizeof(process_pid_str), "%d", getpid()); int process_pid_str_len = strlen(process_pid_str); if(write(pid_file, process_pid_str, process_pid_str_len) != process_pid_str_len) { fprintf(stderr, "Failed to write pid to %s\n", automedia_pid_path); unlink(automedia_pid_path); exit(1); } close(pid_file); const int sync_rate_sec = 15 * 60; /* every 15 min */ sync_rss_html(rss_config_dir, html_config_dir, program_dir, download_dir, sync_rate_sec, &fall); unlink(automedia_pid_path); fallback_deinit(&fall); } static void command_downloaded(int argc, char **argv, const char *rss_config_dir, const char *html_config_dir) { int include_tracked = 0; if(argc >= 1) { include_tracked = strcmp(argv[0], "--include-tracked") == 0; if(!include_tracked) usage_downloaded(); } char rss_tracked_dir[PATH_MAX]; strcpy(rss_tracked_dir, rss_config_dir); strcat(rss_tracked_dir, "/tracked"); char html_tracked_dir[PATH_MAX]; strcpy(html_tracked_dir, html_config_dir); strcat(html_tracked_dir, "/tracked"); Buffer downloaded_items; buffer_init(&downloaded_items); get_downloaded_items(rss_tracked_dir, 0, &downloaded_items, include_tracked); get_downloaded_items(html_tracked_dir, 1, &downloaded_items, include_tracked); qsort(downloaded_items.data, buffer_get_size(&downloaded_items, sizeof(DownloadedListData)), sizeof(DownloadedListData), compare_downloaded_item); DownloadedListData *list_it = buffer_begin(&downloaded_items); DownloadedListData *list_end = buffer_end(&downloaded_items); for(; list_it != list_end; ++list_it) { puts(list_it->title); free(list_it->title); } buffer_deinit(&downloaded_items); } const char* path_get_filename(const char *filepath) { int len = strlen(filepath); for(int i = len - 1; i >= 0; --i) { if(filepath[i] == '/') return filepath + i + 1; } return filepath; } static int compare_tracked_item(const void *a, const void *b) { const TrackedItemData *list_data_a = a; const TrackedItemData *list_data_b = b; return list_data_a->updated_time - list_data_b->updated_time; } static int time_diff_to_days(time_t start, time_t end) { return (end - start) / 60LL / 60LL / 24LL; } static void command_cleanup(int argc, char **argv, const char *rss_config_dir, const char *html_config_dir) { time_t days = 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]; } } char rss_tracked_dir[PATH_MAX]; strcpy(rss_tracked_dir, rss_config_dir); strcat(rss_tracked_dir, "/tracked"); char html_tracked_dir[PATH_MAX]; strcpy(html_tracked_dir, html_config_dir); strcat(html_tracked_dir, "/tracked"); Buffer tracked_items; buffer_init(&tracked_items); int num_rss_items = 0; const time_t age_sec = 60 * 60 * 24 * days; 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, 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); qsort((TrackedItemData*)tracked_items.data + num_rss_items, num_tracked_items - num_rss_items, sizeof(TrackedItemData), compare_tracked_item); time_t time_now = time(NULL); TrackedItemData *list_begin = buffer_begin(&tracked_items); TrackedItemData *list_it = list_begin; TrackedItemData *list_end = buffer_end(&tracked_items); int index = 0; for(; list_it != list_end; ++list_it) { const char *name = path_get_filename(list_it->item_tracked_path); printf("%d (last updated: %d day(s) ago): %s/%s\n", 1 + index, (int)time_diff_to_days(list_it->updated_time, time_now), index < num_rss_items ? "rss" : "html", name); ++index; } char *track_items_selected = alloc_or_crash(num_tracked_items); Buffer /*size_t*/ tracks_to_remove; buffer_init(&tracks_to_remove); Buffer /*Range*/ track_remove_ranges; buffer_init(&track_remove_ranges); if(tracked_items.size == 0) { 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; } for(;;) { fprintf(stderr, "==> Media to stop tracking: (eg: 10, 1-3)\n"); fprintf(stderr, "==> "); fflush(stderr); char resp[512]; if(!fgets(resp, sizeof(resp), stdin)) { fprintf(stderr, "Failed to read response from stdin\n"); goto cleanup; } char *response_str = strip(resp); int response_len = strlen(response_str); if(response_len == 0) { fprintf(stderr, "Invalid input. Expected numbers and/or number ranges\n"); continue; } memset(track_items_selected, 0, num_tracked_items); buffer_clear(&tracks_to_remove); buffer_clear(&track_remove_ranges); if(track_remove_parser_parse(response_str, response_len, &track_remove_ranges) != 0) continue; Range *track_remove_it = buffer_begin(&track_remove_ranges); Range *track_remove_end = buffer_end(&track_remove_ranges); int valid_ranges = 1; for(; track_remove_it != track_remove_end; ++track_remove_it) { int start_index_f = track_remove_it->start - 1; if(start_index_f < 0 || start_index_f >= num_tracked_items) { fprintf(stderr, "Invalid item start range: %d, expected a value between 1 and %d\n", track_remove_it->start, num_tracked_items); valid_ranges = 0; break; } int end_index_f = track_remove_it->end - 1; if(end_index_f < start_index_f || end_index_f >= num_tracked_items) { fprintf(stderr, "Invalid item end range: %d, expected a value between %d and %d\n", end_index_f, track_remove_it->start, num_tracked_items); valid_ranges = 0; break; } for(size_t i = start_index_f; i <= (size_t)end_index_f; ++i) { if(track_items_selected[i]) continue; track_items_selected[i] = 1; buffer_append(&tracks_to_remove, &i, sizeof(i)); } } if(!valid_ranges) continue; for(;;) { time_now = time(NULL); size_t *tracks_to_remove_it = buffer_begin(&tracks_to_remove); size_t *tracks_to_remove_end = buffer_end(&tracks_to_remove); fprintf(stderr, "==> Are you sure you want to stop tracking:\n"); for(; tracks_to_remove_it != tracks_to_remove_end; ++tracks_to_remove_it) { const size_t track_index = *tracks_to_remove_it; TrackedItemData *track_item = &list_begin[track_index]; const char *name = path_get_filename(track_item->item_tracked_path); printf(" (last updated: %d day(s) ago): %s/%s\n", (int)time_diff_to_days(track_item->updated_time, time_now), track_index < (size_t)num_rss_items ? "rss" : "html", name); } fprintf(stderr, "==> Yes/No: "); fflush(stderr); if(!fgets(resp, sizeof(resp), stdin)) { fprintf(stderr, "Failed to read response from stdin\n"); goto cleanup; } response_str = strip(resp); response_len = strlen(response_str); if(response_len == 0) continue; if(response_str[0] == 'n' || response_str[0] == 'N') { goto cleanup; } else if(response_str[0] == 'y' || response_str[0] == 'Y') { break; } } size_t *tracks_to_remove_it = buffer_begin(&tracks_to_remove); size_t *tracks_to_remove_end = buffer_end(&tracks_to_remove); for(; tracks_to_remove_it != tracks_to_remove_end; ++tracks_to_remove_it) { const size_t track_index = *tracks_to_remove_it; TrackedItemData *track_item = &list_begin[track_index]; const char *name = path_get_filename(track_item->item_tracked_path); if(remove_recursive(track_item->item_tracked_path) != 0) { fprintf(stderr, "Failed to remove %s/%s\n", track_index < (size_t)num_rss_items ? "rss" : "html", name); goto cleanup; } fprintf(stderr, "Removed %s/%s\n", track_index < (size_t)num_rss_items ? "rss" : "html", name); } break; } cleanup: buffer_deinit(&track_remove_ranges); buffer_deinit(&tracks_to_remove); free(track_items_selected); list_it = buffer_begin(&tracked_items); list_end = buffer_end(&tracked_items); for(; list_it != list_end; ++list_it) { free(list_it->item_tracked_path); } buffer_deinit(&tracked_items); } int main(int argc, char **argv) { if(argc < 2) usage(); setlocale(LC_ALL, "C"); const char *home_dir = get_home_dir(); char rss_config_dir[PATH_MAX]; strcpy(rss_config_dir, home_dir); strcat(rss_config_dir, "/.config/automedia/rss"); char html_config_dir[PATH_MAX]; strcpy(html_config_dir, home_dir); strcat(html_config_dir, "/.config/automedia/html"); const char *command = argv[1]; if(strcmp(command, "add") == 0) { command_add(argc - 2, argv + 2, rss_config_dir, html_config_dir, dirname(argv[0])); } else if(strcmp(command, "sync") == 0) { command_sync(argc - 2, argv + 2, rss_config_dir, html_config_dir, dirname(argv[0])); } else if(strcmp(command, "downloaded") == 0) { command_downloaded(argc - 2, argv + 2, rss_config_dir, html_config_dir); } else if(strcmp(command, "cleanup") == 0) { command_cleanup(argc - 2, argv + 2, rss_config_dir, html_config_dir); } else { fprintf(stderr, "Error: Invalid command %s\n", command); usage(); } return 0; }