Return number of new (unwatched) videos when fetching updates

This commit is contained in:
Daniel Schulte 2020-12-01 20:37:08 +01:00
parent da84ddce7b
commit 0f777ef139
3 changed files with 10 additions and 6 deletions

View File

@ -143,7 +143,7 @@ void load_videos_for_channel(const Channel &channel, bool force=false)
selected_video = 0; selected_video = 0;
} }
void fetch_videos_for_channel(Channel &channel, bool name_in_title=false) int fetch_videos_for_channel(Channel &channel, bool name_in_title=false)
{ {
if(channel.is_virtual) { if(channel.is_virtual) {
std::vector<Video> &channelVideos = videos[channel.id]; std::vector<Video> &channelVideos = videos[channel.id];
@ -151,7 +151,7 @@ void fetch_videos_for_channel(Channel &channel, bool name_in_title=false)
for(Video &video: channelVideos) { for(Video &video: channelVideos) {
video.tui_title_width = string_width(video.title); video.tui_title_width = string_width(video.title);
} }
return; return channelVideos.size();
} }
std::string title("Refreshing"); std::string title("Refreshing");
@ -159,10 +159,12 @@ void fetch_videos_for_channel(Channel &channel, bool name_in_title=false)
title.append(" ").append(channel.name); title.append(" ").append(channel.name);
title.append(""); title.append("");
progress_info *info = begin_progress(title, 30); progress_info *info = begin_progress(title, 30);
channel.fetch_new_videos(db, info); const int new_video_count = channel.fetch_new_videos(db, info);
end_progress(info); end_progress(info);
load_videos_for_channel(channels[selected_channel], true); load_videos_for_channel(channels[selected_channel], true);
channel.load_info(db); channel.load_info(db);
return new_video_count;
} }
bool startswith(const std::string &str, const std::string &with) bool startswith(const std::string &str, const std::string &with)

6
yt.cpp
View File

@ -174,7 +174,7 @@ void add_video(sqlite3 *db, const json &snippet, const std::string &channel_id)
} }
void Channel::fetch_new_videos(sqlite3 *db, progress_info *info, std::optional<std::string> after, std::optional<int> max_count) int Channel::fetch_new_videos(sqlite3 *db, progress_info *info, std::optional<std::string> after, std::optional<int> max_count)
{ {
const std::string playlist_id = upload_playlist(); const std::string playlist_id = upload_playlist();
std::map<std::string, std::string> params = { std::map<std::string, std::string> params = {
@ -217,7 +217,7 @@ void Channel::fetch_new_videos(sqlite3 *db, progress_info *info, std::optional<s
add_video(db, snippet, channel_id); add_video(db, snippet, channel_id);
//fprintf(stderr, "New video: '%s': %s.\r\n", title.c_str(), video_id.c_str()); //fprintf(stderr, "New video: '%s': %s.\r\n", title.c_str(), video_id.c_str());
processed += 1; processed++;
if(max_count && processed >= *max_count) { if(max_count && processed >= *max_count) {
abort = true; abort = true;
break; break;
@ -236,6 +236,8 @@ void Channel::fetch_new_videos(sqlite3 *db, progress_info *info, std::optional<s
break; break;
} }
} }
return processed;
} }
void Channel::load_info(sqlite3 *db) void Channel::load_info(sqlite3 *db)

2
yt.h
View File

@ -36,7 +36,7 @@ public:
static std::vector<Channel> get_all(sqlite3 *db); static std::vector<Channel> get_all(sqlite3 *db);
std::string upload_playlist() const; std::string upload_playlist() const;
void fetch_new_videos(sqlite3 *db, progress_info *info=nullptr, std::optional<std::string> after={}, std::optional<int> max_count={}); int fetch_new_videos(sqlite3 *db, progress_info *info=nullptr, std::optional<std::string> after={}, std::optional<int> max_count={});
void load_info(sqlite3 *db); void load_info(sqlite3 *db);
bool is_valid() const; bool is_valid() const;