From 1d06834848eab86c849b11358cbc8b975296f54a Mon Sep 17 00:00:00 2001 From: trilader Date: Tue, 1 Dec 2020 23:20:10 +0100 Subject: [PATCH] Allow the user to get notified on new videos --- README.md | 10 ++++++++++ main.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- yttui.conf.example | 7 ++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1bbaa60..f68da1b 100644 --- a/README.md +++ b/README.md @@ -42,3 +42,13 @@ This tool is work in progress and some functions might not work (correctly). | extraHeaders | Extra HTTP headers to send to YouTube. This is a JSON array of objects containing `"key"` and `"value"`. Will be sent with each API requres. | `[]` | ✘ | | database | Path of channel/video database | $HOME/.local/share/yttui.db | ✘ | | watchCommand | Command executed to watch a video. `{{vid}}` will be replaced by the Id of the video to watch. | `["xdg-open", "https://youtube.com/watch?v={{vid}}"]` | ✘ | +| notifications | Object describing notification settings | `{}` | ✘ | + +#### Notifcation options +The `notifications` entry can have the following sub-options: + +|Option | Description | Default value | Required | +|-------|-------------|---------------|--------- | +| channelNewVideoCommand | Gets executed when refreshing a single channel and there is one new videos. `{{channelName}}` will be replaced with the name of updated channel, `{{title}}` with the title of the new video. | `[]` | ✘ | +| channelNewVideosCommand | Gets executed when refreshing a single channel and there are multiple new videos. `{{channelName}}` will be replaced with the name of updated channel, `{{newVideos}}` with the number of new videos. | `[]` | ✘ | +| channelsNewVideosCommand | Gets executed when refreshing multiple channels and there are new videos. `{{updatedChannels}}` will be replaced with the number of updated channels, `{{newVideos}}` with the number of new videos across all refreshed channels. | `[]` | ✘ | diff --git a/main.cpp b/main.cpp index 4de1e2c..7be7c03 100644 --- a/main.cpp +++ b/main.cpp @@ -329,15 +329,45 @@ bool run_command(const std::vector &cmd, const std::vector notify_channel_new_video_command; +std::vector notify_channel_new_videos_command; +std::vector notify_channels_new_videos_command; + void action_refresh_channel() { - fetch_videos_for_channel(channels.at(selected_channel)); + Channel &ch = channels.at(selected_channel); + const int new_videos = fetch_videos_for_channel(channels.at(selected_channel)); + if(new_videos == 0) + return; + if(new_videos == 1 && !notify_channel_new_video_command.empty()) { + run_command(notify_channel_new_video_command, { + {"{{channelName}}", ch.name}, + {"{{videoTitle}}", videos[ch.id].front().title}, + }); + } else if(notify_channel_new_videos_command.size()) { + run_command(notify_channel_new_videos_command, { + {"{{channelName}}", ch.name}, + {"{{newVideos}}", std::to_string(new_videos)} + }); + } + } void action_refresh_all_channels() { if(message_box("Refresh all channels?", ("Do you want to refresh all " + std::to_string(channels.size()) + " channels?").c_str(), Button::Yes | Button::No, Button::No) != Button::Yes) return; + int updated_channels = 0; + int new_videos = 0; for(Channel &channel: channels) { - fetch_videos_for_channel(channel, true); + const int count = fetch_videos_for_channel(channel, true); + new_videos += count; + if(count) + updated_channels++; + } + if(updated_channels && new_videos && !notify_channels_new_videos_command.empty()) { + run_command(notify_channels_new_videos_command, { + {"{{updatedChannels}}", std::to_string(updated_channels)}, + {"{{newVideos}}", std::to_string(new_videos)} + }); } } @@ -523,6 +553,12 @@ int main() database_filename = replace(config["database"], "$HOME", user_home); } config_get_string_list(watch_command, config, "watchCommand"); + if(config.contains("notifications") && config["notifications"].is_object()) { + const json ¬ifications = config["notifications"]; + config_get_string_list(notify_channel_new_video_command, notifications, "channelNewVideoCommand"); + config_get_string_list(notify_channel_new_videos_command, notifications, "channelNewVideosCommand"); + config_get_string_list(notify_channels_new_videos_command, notifications, "channelsNewVideosCommand"); + } db_init(database_filename); make_virtual_unwatched_channel(); diff --git a/yttui.conf.example b/yttui.conf.example index a9df8a3..7f6dd62 100644 --- a/yttui.conf.example +++ b/yttui.conf.example @@ -4,5 +4,10 @@ {"key": "x-extra", "value": "heder"} ], "database": "$HOME/yttui.db", - "watchCommand": ["xdg-open", "https://youtube.com/watch?v={{vid}}"] + "watchCommand": ["xdg-open", "https://youtube.com/watch?v={{vid}}"], + "notifications": { + "channelNewVideoCommand": ["notify-send", "--app-name", "yttui", "New video from {{channelName}}", "{{videoTitle}}"], + "channelNewVideosCommand": ["notify-send", "--app-name", "yttui", "New videos from {{channelName}}", "There are {{newVideos}} new videos."], + "channelsNewVideosCommand": ["notify-send", "--app-name", "yttui", "New videos from multiple channels", "There are {{newVideos}} new videos from {{updatedChannels}} channels."] + } }