aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c47
1 files changed, 33 insertions, 14 deletions
diff --git a/src/main.c b/src/main.c
index 2669545..de7bebe 100644
--- a/src/main.c
+++ b/src/main.c
@@ -10,6 +10,7 @@
#include "rss_html_common.h"
#include "html.h"
#include "track_remove_parser.h"
+#include "fallback.h"
#include "../depends/cJSON.h"
#include "alloc.h"
@@ -322,6 +323,12 @@ static void command_add(int argc, char **argv, char *rss_config_dir, char *html_
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) {
@@ -329,7 +336,7 @@ static void command_add(int argc, char **argv, char *rss_config_dir, char *html_
exit(1);
}
- if(add_rss(media_name, media_url, rss_config_dir, start_after) != 0)
+ 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);
@@ -344,6 +351,8 @@ static void command_add(int argc, char **argv, char *rss_config_dir, char *html_
fprintf(stderr, "type should be either rss or html\n");
usage_add();
}
+
+ fallback_deinit(&fall);
}
sig_atomic_t automedia_running = 0;
@@ -357,8 +366,8 @@ int is_program_running() {
}
/* plugin is NULL for rss */
-typedef int (*IterateTrackedItemCallback)(char *title, char *link, char *plugin, char *config_dir, cJSON *json_data, void *userdata);
-static void iterate_tracked_items(char *config_dir, IterateTrackedItemCallback iterate_callback, void *userdata) {
+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
@@ -431,7 +440,7 @@ static void iterate_tracked_items(char *config_dir, IterateTrackedItemCallback i
free(data_file_content);
data_file_content = NULL;
- if(iterate_callback(dir->d_name, link_file_content, plugin_file_content, config_dir, json_data, userdata) != 0)
+ 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:
@@ -444,14 +453,14 @@ static void iterate_tracked_items(char *config_dir, IterateTrackedItemCallback i
closedir(d);
}
-static int iterate_tracked_item_rss_callback(char *title, char *link, char *plugin, char *config_dir, cJSON *json_data, void *userdata) {
+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);
+ return sync_rss(&tracked_rss, transmission_session, config_dir, fall);
}
typedef struct {
@@ -459,7 +468,8 @@ typedef struct {
const char *download_dir;
} IterateHtmlItemUserdata;
-static int iterate_tracked_item_html_callback(char *title, char *link, char *plugin, char *config_dir, cJSON *json_data, void *userdata) {
+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;
@@ -474,15 +484,15 @@ static int iterate_tracked_item_html_callback(char *title, char *link, char *plu
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_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, &iterate_html_item_userdata);
+ iterate_tracked_items(html_config_dir, iterate_tracked_item_html_callback, NULL, &iterate_html_item_userdata);
}
typedef struct {
@@ -511,7 +521,7 @@ static void torrent_list_check_new_downloads_callback(int id, const char *name,
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", "3000", "--", "Download finished", name, NULL };
+ 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];
@@ -533,7 +543,7 @@ static void torrent_list_check_new_downloads_callback(int id, const char *name,
}
}
-static void sync_rss_html(char *rss_config_dir, char *html_config_dir, char *program_dir, const char *download_dir, int sync_rate_sec) {
+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");
@@ -560,7 +570,7 @@ static void sync_rss_html(char *rss_config_dir, char *html_config_dir, char *pro
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_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);
@@ -570,6 +580,8 @@ static void sync_rss_html(char *rss_config_dir, char *html_config_dir, char *pro
sleep(check_torrent_status_rate_sec);
++check_count;
}
+
+ fallback_clear_sources_to_use(fall);
}
free(unfinished_torrents.items);
@@ -627,6 +639,12 @@ static void command_sync(int argc, char **argv, char *rss_config_dir, char *html
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);
@@ -677,8 +695,9 @@ static void command_sync(int argc, char **argv, char *rss_config_dir, char *html
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);
+ 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) {