#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 "json.h" #include "alloc.h" #include #include #include #include #include #include #include #include #include #include #include #define NAME_MAX_LEN 250 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"); 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/adam/Downloads/automedia\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, 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; } struct json_value_s *json_root = json_parse(file_data, file_size); if(!json_root) { fprintf(stderr, "Failed to parse file %s as json\n", data_filepath); goto cleanup; } struct json_object_s *json_root_obj = json_value_as_object(json_root); if(!json_root_obj) { fprintf(stderr, "File %s contains malformed json. Expected json root element to be an object\n", data_filepath); goto cleanup; } struct json_value_s *downloaded_json = json_object_get_field_by_name(json_root_obj, "downloaded"); if(!downloaded_json) { fprintf(stderr, "File %s contains malformed json. Expected json to contain \"downloaded\"\n", data_filepath); goto cleanup; } struct json_array_s *downloaded_json_arr = json_value_as_array(downloaded_json); if(!downloaded_json_arr) { fprintf(stderr, "File %s contains malformed json. Expected \"downloaded\" to be an array\n", data_filepath); goto cleanup; } size_t dir_name_len = strlen(dir_name); struct json_array_element_s *downloaded_item = downloaded_json_arr->start; for(; downloaded_item; downloaded_item = downloaded_item->next) { struct json_object_s *downloaded_item_obj = json_value_as_object(downloaded_item->value); if(!downloaded_item_obj) continue; struct json_value_s *time_json = json_object_get_field_by_name(downloaded_item_obj, "time"); if(!time_json || time_json->type != json_type_string) continue; struct json_string_s *time_json_str = json_value_as_string(time_json); struct json_value_s *title_json = json_object_get_field_by_name(downloaded_item_obj, "title"); struct json_string_s *title_str = NULL; if(title_json && title_json->type == json_type_string) title_str = json_value_as_string(title_json); struct json_value_s *filename_json = json_object_get_field_by_name(downloaded_item_obj, "filename"); struct json_string_s *filename_str = NULL; if(filename_json && filename_json->type == json_type_string) filename_str = json_value_as_string(filename_json); /* Filename limit is 256, so this should be safe... */ char title[256]; if(filename_str) { strcpy(title, filename_str->string); } else if(title_str) { if(is_html) { strcpy(title, dir_name); title[dir_name_len] = '/'; strcpy(title + dir_name_len + 1, title_str->string); } else { strcpy(title, title_str->string); } } else { continue; } callback(title, atof(time_json_str->string), userdata); } cleanup: free(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) { 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, 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; } 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(start_after) { string_replace(start_after, '/', '_'); start_after = strip(start_after); } media_url = strip(media_url); 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) != 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(); } } 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, struct json_object_s *json_data, void *userdata); static void iterate_tracked_items(char *config_dir, IterateTrackedItemCallback iterate_callback, 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); 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"); if(file_exists(item_filepath) == 0) { fprintf(stderr, "Skipping in-progress rss %s\n", dir->d_name); continue; } char *link_file_content = NULL; char *data_file_content = NULL; char *plugin_file_content = NULL; struct json_value_s *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 = json_parse(data_file_content, data_file_size); if(!json_data || json_data->type != json_type_object) { 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_value_as_object(json_data), userdata) != 0) fprintf(stderr, "Failed to sync %s\n", dir->d_name); cleanup_item: free(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, struct json_object_s *json_data, 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); } 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, struct json_object_s *json_data, void *userdata) { 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) { iterate_tracked_items(rss_config_dir, iterate_tracked_item_rss_callback, 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, &iterate_html_item_userdata); } typedef struct { char *items; int size; } 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; 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", "normal", "--", "Download finished", name, NULL }; program_exec(notify_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) { 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); 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); sync_tracked_html(html_config_dir, program_dir, download_dir); 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; } } 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[128]; sprintf(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); } /* 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); remove(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); char process_pid_str[32]; sprintf(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); remove(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); remove(automedia_pid_path); } static void command_downloaded(const char *rss_config_dir, const char *html_config_dir) { 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); get_downloaded_items(html_tracked_dir, 1, &downloaded_items); 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); } int main(int argc, char **argv) { if(argc < 2) usage(); 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(rss_config_dir, html_config_dir); } else { fprintf(stderr, "Error: Invalid command %s\n", command); usage(); } return 0; }