Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for different watch youtube urls #1328

Merged
merged 1 commit into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/components/brave_sync/extension/brave-sync/
/dist/
/out/
/vendor
/vendor/*
!/vendor/bat-native-ledger
node_modules/
*.xcodeproj
Expand Down
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ test("brave_unit_tests") {

if (brave_rewards_enabled) {
sources += [
"//brave/vendor/bat-native-ledger/src/bat_get_media_unittest.cc",
"//brave/vendor/bat-native-ledger/src/test/niceware_partial_unittest.cc",
"//brave/vendor/bat-native-usermodel/test/usermodel_unittest.cc",
"//brave/components/brave_rewards/browser/rewards_service_impl_unittest.cc",
Expand Down
26 changes: 22 additions & 4 deletions vendor/bat-native-ledger/src/bat_get_media.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <sstream>
#include <cmath>
#include <vector>

#include "bat_get_media.h"
#include "bat_helper.h"
Expand Down Expand Up @@ -758,11 +759,28 @@ std::string BatGetMedia::parseChannelId(const std::string& data) {
return id;
}

// static
std::string BatGetMedia::getYoutubeMediaIdFromUrl(const ledger::VisitData& visit_data) {
std::vector<std::string> m_url =
braveledger_bat_helper::split(visit_data.url, '=');
if (m_url.size() > 1) {
return m_url[1];
std::vector<std::string> first_split =
braveledger_bat_helper::split(visit_data.url, '?');

if (first_split.size() < 2) {
return std::string();
}

std::vector<std::string> and_split =
braveledger_bat_helper::split(first_split[1], '&');

for (const auto& item : and_split) {
std::vector<std::string> m_url = braveledger_bat_helper::split(item, '=');

if (m_url.size() < 2) {
continue;
}

if (m_url[0] == "v") {
return m_url[1];
}
}

return std::string();
Expand Down
4 changes: 2 additions & 2 deletions vendor/bat-native-ledger/src/bat_get_media.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class BatGetMedia {
const ledger::VisitData& visit_data,
const std::string& providerType);

static std::string getYoutubeMediaIdFromUrl(const ledger::VisitData& visit_data);

private:
std::string getMediaURL(const std::string& mediaId, const std::string& providerName);
void getPublisherFromMediaPropsCallback(const uint64_t& duration,
Expand Down Expand Up @@ -171,8 +173,6 @@ class BatGetMedia {

std::string parseChannelId(const std::string& data);

std::string getYoutubeMediaIdFromUrl(const ledger::VisitData& visit_data);

std::string getYoutubeMediaKeyFromUrl(const std::string& provider_type, const std::string& media_id);

std::string getYoutubePublisherKeyFromUrl(const ledger::VisitData& visit_data);
Expand Down
52 changes: 52 additions & 0 deletions vendor/bat-native-ledger/src/bat_get_media_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/vendor/bat-native-ledger/include/bat/ledger/ledger.h"
#include "brave/vendor/bat-native-ledger/src/bat_get_media.h"
#include "testing/gtest/include/gtest/gtest.h"

TEST(BatGetMediaTest, GetYoutubeMediaIdFromUrl) {
//BatGetMedia media = new BatGetMedia(nullptr);

// missing video id
ledger::VisitData data;
data.url = "https://www.youtube.com/watch";

std::string media =
braveledger_bat_get_media::BatGetMedia::getYoutubeMediaIdFromUrl(data);

ASSERT_EQ(media, "");

// single element in the url
data.url = "https://www.youtube.com/watch?v=44444444";

media =
braveledger_bat_get_media::BatGetMedia::getYoutubeMediaIdFromUrl(data);

ASSERT_EQ(media, "44444444");

// single element in the url with & appended
data.url = "https://www.youtube.com/watch?v=44444444&";

media =
braveledger_bat_get_media::BatGetMedia::getYoutubeMediaIdFromUrl(data);

ASSERT_EQ(media, "44444444");

// multiple elements in the url (id first)
data.url = "https://www.youtube.com/watch?v=44444444&time_continue=580";

media =
braveledger_bat_get_media::BatGetMedia::getYoutubeMediaIdFromUrl(data);

ASSERT_EQ(media, "44444444");

// multiple elements in the url
data.url = "https://www.youtube.com/watch?time_continue=580&v=44444444";

media =
braveledger_bat_get_media::BatGetMedia::getYoutubeMediaIdFromUrl(data);

ASSERT_EQ(media, "44444444");
}