Skip to content

Commit

Permalink
Merge pull request #1892 from brave/custom-yt-61
Browse files Browse the repository at this point in the history
Youtube videos with custom paths now show correctly
  • Loading branch information
NejcZdovc authored Mar 8, 2019
2 parents 8048b20 + 732090c commit bea7847
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 21 deletions.
120 changes: 103 additions & 17 deletions vendor/bat-native-ledger/src/bat_get_media.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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) {
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -598,7 +659,7 @@ void BatGetMedia::onMediaUserActivity(

} else {
fetchPublisherDataFromDB(windowId, visit_data,
providerType, info->id, std::string());
providerType, info->id, std::string(), false);
}
}

Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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) {
Expand Down Expand Up @@ -726,7 +797,8 @@ void BatGetMedia::onGetChannelHeadlineVideo(
const std::string& providerType,
bool success,
const std::string& response,
const std::map<std::string, std::string>& headers) {
const std::map<std::string, std::string>& headers,
const bool is_custom_path) {
if (!success) {
onMediaActivityError(visit_data, providerType, windowId);
return;
Expand All @@ -738,7 +810,7 @@ void BatGetMedia::onGetChannelHeadlineVideo(
std::string channelId = getYoutubePublisherKeyFromUrl(visit_data.path);

savePublisherInfo(0,
"",
std::string(),
providerType,
visit_data.url,
title,
Expand All @@ -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);
}
Expand Down Expand Up @@ -861,7 +941,8 @@ void BatGetMedia::onMediaPublisherActivity(ledger::Result result,
visit_data,
providerType,
info->id,
publisher_blob);
publisher_blob,
false);
}
}
}
Expand Down Expand Up @@ -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
22 changes: 19 additions & 3 deletions vendor/bat-native-ledger/src/bat_get_media.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string>& headers);
const std::map<std::string, std::string>& headers,
const bool is_custom_path);

void onFetchPublisherFromDBResponse(
ledger::Result result,
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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<std::string, ledger::TwitchEventInfo> twitchEvents;
Expand All @@ -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
Expand Down
84 changes: 83 additions & 1 deletion vendor/bat-native-ledger/src/bat_get_media_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bea7847

Please sign in to comment.