From 732090c58089b5932fffedaee6d9a0b81d30b743 Mon Sep 17 00:00:00 2001 From: Nejc Zdovc Date: Wed, 6 Mar 2019 18:52:46 +0100 Subject: [PATCH] Merge pull request #1867 from brave/custom-yt-62 Youtube videos with custom paths now show correctly --- vendor/bat-native-ledger/src/bat_get_media.cc | 120 +++++++++++++++--- vendor/bat-native-ledger/src/bat_get_media.h | 22 +++- .../src/bat_get_media_unittest.cc | 84 +++++++++++- 3 files changed, 205 insertions(+), 21 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat_get_media.cc b/vendor/bat-native-ledger/src/bat_get_media.cc index 0bb6e1e905be..433bc13d4ee7 100644 --- a/vendor/bat-native-ledger/src/bat_get_media.cc +++ b/vendor/bat-native-ledger/src/bat_get_media.cc @@ -533,11 +533,57 @@ void BatGetMedia::processYoutubeMediaPanel(uint64_t windowId, processYoutubeChannelPath(windowId, visit_data, providerType); } else if (visit_data.path.find("/user/") != std::string::npos) { processYoutubeUserPath(windowId, visit_data, providerType); + } else if (!isPredefinedYTPath(visit_data.path)) { + processYoutubeCustomPath(windowId, visit_data, providerType, std::string()); } else { onMediaActivityError(visit_data, providerType, windowId); } } +bool BatGetMedia::isPredefinedYTPath(const std::string& path) const { + std::vector yt_paths({ + "/feed", + "/channel", + "/user", + "/watch", + "/account", + "/gaming", + "/playlist", + "/premium", + "/reporthistory", + "/pair", + "/account_notifications", + "/account_playback", + "/account_privacy", + "/account_sharing", + "/account_billing", + "/account_advanced", + "/subscription_manager", + "/oops" + }); + + // make sure we are ignoring actual YT paths and not + // a custom path that might start with a YT path + std::string yt_path = getRealEnteredYTPath(path); + for (std::string str_path : yt_paths) { + if (yt_path == str_path) { + return true; + } + } + return false; +} + +std::string BatGetMedia::getRealEnteredYTPath(const std::string& path) const { + std::string yt_path = path.substr(0, path.find("/", 1)); + if (yt_path.empty() || yt_path == path) { + yt_path = path.substr(0, path.find("?", 1)); + if (yt_path.empty() || yt_path == path) { + yt_path = path.substr(0); + } + } + return yt_path; +} + void BatGetMedia::processYoutubeWatchPath(uint64_t windowId, const ledger::VisitData& visit_data, const std::string& providerType) { @@ -554,6 +600,20 @@ void BatGetMedia::processYoutubeWatchPath(uint64_t windowId, } } +void BatGetMedia::processYoutubeCustomPath( + uint64_t windowId, + const ledger::VisitData& visit_data, + const std::string& providerType, + const std::string& publisher_key) { + fetchPublisherDataFromDB( + windowId, + visit_data, + providerType, + publisher_key, + std::string(), + true); +} + void BatGetMedia::processYoutubeChannelPath(uint64_t windowId, const ledger::VisitData& visit_data, const std::string& providerType) { @@ -565,7 +625,8 @@ void BatGetMedia::processYoutubeChannelPath(uint64_t windowId, visit_data, providerType, publisher_key, - std::string()); + std::string(), + false); } else { onMediaActivityError(visit_data, providerType, windowId); } @@ -598,7 +659,7 @@ void BatGetMedia::onMediaUserActivity( } else { fetchPublisherDataFromDB(windowId, visit_data, - providerType, info->id, std::string()); + providerType, info->id, std::string(), false); } } @@ -624,7 +685,8 @@ void BatGetMedia::fetchPublisherDataFromDB( const ledger::VisitData& visit_data, const std::string& providerType, const std::string& publisher_key, - const std::string& publisher_blob) { + const std::string& publisher_blob, + const bool is_custom_path) { auto filter = ledger_->CreateActivityFilter( publisher_key, ledger::ACTIVITY_MONTH::ANY, @@ -636,8 +698,15 @@ void BatGetMedia::fetchPublisherDataFromDB( false); ledger_->GetPanelPublisherInfo(filter, std::bind(&BatGetMedia::onFetchPublisherFromDBResponse, - this, _1, _2, windowId, visit_data, providerType, - publisher_key, publisher_blob)); + this, + _1, + _2, + windowId, + visit_data, + providerType, + publisher_key, + publisher_blob, + is_custom_path)); } void BatGetMedia::onFetchPublisherFromDBResponse( @@ -647,18 +716,20 @@ void BatGetMedia::onFetchPublisherFromDBResponse( const ledger::VisitData& visit_data, const std::string& providerType, const std::string& publisher_key, - const std::string& publisher_blob) { + const std::string& publisher_blob, + const bool is_custom_path) { if (!info || (result == ledger::Result::NOT_FOUND && providerType == YOUTUBE_MEDIA_TYPE)) { fetchDataFromUrl(visit_data.url, - std::bind(&BatGetMedia::onGetChannelHeadlineVideo, - this, - windowId, - visit_data, - providerType, - _1, - _2, - _3)); + std::bind(&BatGetMedia::onGetChannelHeadlineVideo, + this, + windowId, + visit_data, + providerType, + _1, + _2, + _3, + is_custom_path)); } else { if (providerType == TWITCH_MEDIA_TYPE) { if (info->name != visit_data.name) { @@ -726,7 +797,8 @@ void BatGetMedia::onGetChannelHeadlineVideo( const std::string& providerType, bool success, const std::string& response, - const std::map& headers) { + const std::map& headers, + const bool is_custom_path) { if (!success) { onMediaActivityError(visit_data, providerType, windowId); return; @@ -738,7 +810,7 @@ void BatGetMedia::onGetChannelHeadlineVideo( std::string channelId = getYoutubePublisherKeyFromUrl(visit_data.path); savePublisherInfo(0, - "", + std::string(), providerType, visit_data.url, title, @@ -747,6 +819,14 @@ void BatGetMedia::onGetChannelHeadlineVideo( favicon, channelId); + } else if (is_custom_path) { + std::string title = getNameFromChannel(response); + std::string favicon = parseFavIconUrl(response); + std::string channelId = parseChannelIdFromCustomPathPage(response); + ledger::VisitData new_visit_data(visit_data); + new_visit_data.path = "/channel/" + channelId; + processYoutubeCustomPath(windowId, new_visit_data, providerType, + "youtube#channel:" + channelId); } else { onMediaActivityError(visit_data, providerType, windowId); } @@ -861,7 +941,8 @@ void BatGetMedia::onMediaPublisherActivity(ledger::Result result, visit_data, providerType, info->id, - publisher_blob); + publisher_blob, + false); } } } @@ -1061,4 +1142,9 @@ std::string BatGetMedia::getNameFromChannel(const std::string& data) { return extractData(data, "channelMetadataRenderer\":{\"title\":\"", "\""); } +std::string BatGetMedia::parseChannelIdFromCustomPathPage( + const std::string& data) { + return extractData(data, "{\"key\":\"browse_id\",\"value\":\"", "\""); +} + } // namespace braveledger_bat_get_media diff --git a/vendor/bat-native-ledger/src/bat_get_media.h b/vendor/bat-native-ledger/src/bat_get_media.h index b81e69914782..9b67dbfa903e 100644 --- a/vendor/bat-native-ledger/src/bat_get_media.h +++ b/vendor/bat-native-ledger/src/bat_get_media.h @@ -194,13 +194,20 @@ class BatGetMedia { const ledger::VisitData& visit_data, const std::string& providerType); + void processYoutubeCustomPath( + uint64_t windowId, + const ledger::VisitData& visit_data, + const std::string& providerType, + const std::string& publisher_key); + void onGetChannelHeadlineVideo( uint64_t windowId, const ledger::VisitData& visit_data, const std::string& providerType, bool success, const std::string& response, - const std::map& headers); + const std::map& headers, + const bool is_custom_path); void onFetchPublisherFromDBResponse( ledger::Result result, @@ -209,7 +216,8 @@ class BatGetMedia { const ledger::VisitData& visit_data, const std::string& providerType, const std::string& publisher_key, - const std::string& publisher_blob); + const std::string& publisher_blob, + const bool is_custom_path); void processYoutubeAsPublisherType(const std::string& data, uint64_t windowId, @@ -231,7 +239,8 @@ class BatGetMedia { const ledger::VisitData& visit_data, const std::string& providerType, const std::string& publisher_key, - const std::string& publisher_blob); + const std::string& publisher_blob, + const bool is_custom_path); void fetchDataFromUrl(const std::string& url, FetchDataFromUrlCallback callback); @@ -249,6 +258,12 @@ class BatGetMedia { std::string getNameFromChannel(const std::string& data); + bool isPredefinedYTPath(const std::string& path) const; + + std::string parseChannelIdFromCustomPathPage( + const std::string& data); + std::string getRealEnteredYTPath(const std::string& path) const; + bat_ledger::LedgerImpl* ledger_; // NOT OWNED std::map twitchEvents; @@ -258,6 +273,7 @@ class BatGetMedia { FRIEND_TEST_ALL_PREFIXES(BatGetMediaTest, GetYoutubeMediaIdFromUrl); FRIEND_TEST_ALL_PREFIXES(BatGetMediaTest, GetYoutubePublisherKeyFromUrl); FRIEND_TEST_ALL_PREFIXES(BatGetMediaTest, GetYoutubeUserFromUrl); + FRIEND_TEST_ALL_PREFIXES(BatGetMediaTest, getRealEnteredYTPath); }; } // namespace braveledger_bat_get_media diff --git a/vendor/bat-native-ledger/src/bat_get_media_unittest.cc b/vendor/bat-native-ledger/src/bat_get_media_unittest.cc index 19f59cdf7138..e75111b2bc4a 100644 --- a/vendor/bat-native-ledger/src/bat_get_media_unittest.cc +++ b/vendor/bat-native-ledger/src/bat_get_media_unittest.cc @@ -180,4 +180,86 @@ TEST(BatGetMediaTest, GetYoutubeUserFromUrl) { ASSERT_EQ(user, "brave"); } -} // namespace braveledger_bat_get_media +TEST(BatGetMediaTest, getRealEnteredYTPath) { + braveledger_bat_get_media::BatGetMedia* bat_get_media_ = + new braveledger_bat_get_media::BatGetMedia(nullptr); + std::string path = "/gaming"; + std::string realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "/gaming"); + + path = "/watch?v=000000000000000"; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "/watch"); + + path = "/playlist?list=0000000000000"; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "/playlist"); + + path = "/bravesoftware"; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "/bravesoftware"); + + path = "/bravesoftware/videos"; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "/bravesoftware"); + + path = "bravesoftware/videos"; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "bravesoftware"); + + path = "/bravesoftware/playlists"; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "/bravesoftware"); + + path = "/bravesoftware/community"; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "/bravesoftware"); + + path = "/bravesoftware/channels"; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "/bravesoftware"); + + path = "/bravesoftware/about"; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "/bravesoftware"); + + path = "/gaminggiant"; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "/gaminggiant"); + + path = "/feed/trending"; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "/feed"); + + path = "/subscription_manager?disable_polymer=1"; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "/subscription_manager"); + + path = ""; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, ""); + + path = "/"; + realPath = + bat_get_media_->getRealEnteredYTPath(path); + ASSERT_EQ(realPath, "/"); + + // cleanup + delete bat_get_media_; +} + +} // braveledger_bat_get_media