Skip to content
This repository has been archived by the owner on Dec 12, 2018. It is now read-only.

Commit

Permalink
Now able to retrieve Twitch favicon and panel
Browse files Browse the repository at this point in the history
Panel and FavIcon for Twitch now show.
  • Loading branch information
Jason Sadler committed Nov 20, 2018
1 parent f72aad5 commit 42a0c5d
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 1 deletion.
5 changes: 5 additions & 0 deletions include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ class LEDGER_EXPORT Ledger {
const int year,
const uint32_t date) = 0;
virtual void RemoveRecurring(const std::string& publisher_key) = 0;
virtual void OnTwitchInfo(
const std::string& page_blob,
ledger::PUBLISHER_MONTH month,
int year,
const std::string& url) = 0;
};

} // namespace ledger
Expand Down
94 changes: 93 additions & 1 deletion src/bat_get_media.cc
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,16 @@ void BatGetMedia::processYoutubeMediaPanel(uint64_t windowId,

void BatGetMedia::processTwitchMediaPanel(uint64_t windowId,
const ledger::VisitData& visit_data, const std::string& providerType) {
// TODO add support for twitch
std::string media_id = getTwitchMediaIdFromUrl(visit_data);
std::string media_key = getTwitchMediaKeyFromUrl(providerType, media_id);
if (!media_key.empty() || !media_id.empty()) {
ledger_->GetMediaPublisherInfo(media_key,
std::bind(&BatGetMedia::onMediaPublisherActivity,
this, _1, _2, windowId, visit_data,
providerType, media_key, media_id));
} else {
onMediaActivityError(visit_data, providerType, windowId);
}
}

void BatGetMedia::processYoutubeWatchPath(uint64_t windowId,
Expand Down Expand Up @@ -794,6 +803,19 @@ std::string BatGetMedia::getYoutubeMediaIdFromUrl(const ledger::VisitData& visit
return std::string();
}

std::string BatGetMedia::getTwitchMediaIdFromUrl(
const ledger::VisitData& visit_data) {
std::string mediaId =
extractData(visit_data.url, "twitch.tv/", std::string());

// this now needs rework
if (mediaId == "videos") {
mediaId = "twitch_" + visit_data.name + "_vod_" +
extractData(visit_data.url, "twitch.tv/videos/", "/");
}
return mediaId;
}

std::string BatGetMedia::getYoutubeMediaKeyFromUrl(
const std::string& provider_type,
const std::string& id) {
Expand All @@ -804,6 +826,12 @@ std::string BatGetMedia::getYoutubeMediaKeyFromUrl(
return std::string();
}

std::string BatGetMedia::getTwitchMediaKeyFromUrl(
const std::string& provider_type,
const std::string& id) {
return provider_type + "_" + id;
}

std::string BatGetMedia::getYoutubePublisherKeyFromUrl(const ledger::VisitData& visit_data) {
return extractData(visit_data.path + "/", "/channel/", "/");
}
Expand All @@ -824,6 +852,8 @@ std::string BatGetMedia::extractData(const std::string& data,
match = data.substr(startPos, endPos - startPos);
} else if (endPos != std::string::npos) {
match = data.substr(startPos, endPos);
} else {
match = data.substr(startPos, std::string::npos);
}
}
return match;
Expand All @@ -833,4 +863,66 @@ std::string BatGetMedia::getNameFromChannel(const std::string& data) {
return extractData(data, "channelMetadataRenderer\":{\"title\":\"", "\"");
}

void BatGetMedia::checkTwitchInfo(
const std::string& page_blob,
ledger::PUBLISHER_MONTH month,
int year, const std::string& url) {
std::string userId;
std::string faviconUrl = parseTwitchData(userId, page_blob);
std::string name(userId);
if (!userId.empty()) {
std::transform(userId.begin(), userId.end(), userId.begin(), ::tolower);
std::string mediaKey = "twitch_" + userId;
ledger_->GetMediaPublisherInfo(mediaKey,
std::bind(&BatGetMedia::getTwitchPublisherInfoDataCallback,
this,
userId,
faviconUrl,
month,
year,
name,
url,
_1,
_2));
}
}

void BatGetMedia::getTwitchPublisherInfoDataCallback(
std::string& userId,
const std::string& faviconUrl,
ledger::PUBLISHER_MONTH month,
int year,
const std::string& name,
const std::string& url,
ledger::Result result,
std::unique_ptr<ledger::PublisherInfo> publisher_info) {
if (result == ledger::Result::NOT_FOUND) {
return;
}
if (publisher_info) {
publisher_info->name = name;
std::string favicon_key = "https://" + ledger_->GenerateGUID() +
".invalid";
ledger_->FetchFavIcon(faviconUrl,
favicon_key,
std::bind(
&BatGetMedia::onFetchFavIcon, this,
publisher_info->id, _1, _2));
ledger::TwitchEventInfo twitchEventInfo;
}
}

std::string BatGetMedia::parseTwitchData(
std::string& userId, const std::string& page_blob) {
userId = extractData(page_blob,
"<figure class=\"tw-avatar tw-avatar--size-36\">"
"<div class=\"tw-border-radius-medium tw-overflow-hidden\">"
"<img class=\"tw-avatar__img tw-image\" alt=\"", "\"");
return extractData(page_blob,
"<figure class=\"tw-avatar tw-avatar--size-36\">"
"<div class=\"tw-border-radius-medium tw-overflow-hidden\">"
"<img class=\"tw-avatar__img tw-image\" alt=\"" + userId + "\" src=\"",
"\"");
}

} // namespace braveledger_bat_get_media
21 changes: 21 additions & 0 deletions src/bat_get_media.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class BatGetMedia {
void getMediaActivityFromUrl(uint64_t windowId,
const ledger::VisitData& visit_data,
const std::string& providerType);
void checkTwitchInfo(
const std::string& page_blob,
ledger::PUBLISHER_MONTH month,
int year,
const std::string& url);

private:
std::string getMediaURL(const std::string& mediaId, const std::string& providerName);
Expand Down Expand Up @@ -180,8 +185,12 @@ class BatGetMedia {
std::string parseChannelId(const std::string& data);

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

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

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

Expand All @@ -199,6 +208,18 @@ class BatGetMedia {
const std::string& publisher_key);

void fetchDataFromUrl(const std::string& url, FetchDataFromUrlCallback callback);
std::string parseTwitchData(
std::string& userId,
const std::string& page_blob);
void getTwitchPublisherInfoDataCallback(
std::string& userId,
const std::string& faviconUrl,
ledger::PUBLISHER_MONTH,
int year,
const std::string& name,
const std::string& url,
ledger::Result result,
std::unique_ptr<ledger::PublisherInfo> publisher_info);

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

Expand Down
17 changes: 17 additions & 0 deletions src/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,16 @@ ledger::PublisherInfoFilter LedgerImpl::CreatePublisherFilter(const std::string&
currentReconcileStamp);
}

ledger::PublisherInfoFilter LedgerImpl::CreatePublisherFilter(
const std::string& publisher_id,
ledger::PUBLISHER_CATEGORY category,
ledger::PUBLISHER_MONTH month,
int year) {
return bat_publishers_->CreatePublisherFilter(publisher_id, category, month,
year);
}


void LedgerImpl::Log(const std::string& func_name, const ledger::LogLevel log_level, std::vector<std::string> data) {
const char* delimiter = " ";
std::stringstream imploded;
Expand Down Expand Up @@ -1003,4 +1013,11 @@ LedgerImpl::GetCurrentReconciles() const {
return bat_state_->GetCurrentReconciles();
}

void LedgerImpl::OnTwitchInfo(
const std::string& page_blob,
ledger::PUBLISHER_MONTH month,
int year, const std::string& url) {
bat_get_media_->checkTwitchInfo(page_blob, month, year, url);
}

} // namespace bat_ledger
10 changes: 10 additions & 0 deletions src/ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ class LedgerImpl : public ledger::Ledger,
ledger::PUBLISHER_EXCLUDE_FILTER excluded,
bool min_duration,
const uint64_t& currentReconcileStamp);
ledger::PublisherInfoFilter CreatePublisherFilter(
const std::string& publisher_id,
ledger::PUBLISHER_CATEGORY category,
ledger::PUBLISHER_MONTH month,
int year);
void Log(const std::string& func_name,
const ledger::LogLevel log_level,
std::vector<std::string> data);
Expand Down Expand Up @@ -271,6 +276,11 @@ class LedgerImpl : public ledger::Ledger,
uint32_t /* next_record */);

void SetTimer(uint64_t time_offset, uint32_t& timer_id) const;
void OnTwitchInfo(
const std::string& page_blob,
ledger::PUBLISHER_MONTH month,
int year,
const std::string& url) override;

bool AddReconcileStep(const std::string& viewing_id,
braveledger_bat_helper::ContributionRetry step,
Expand Down

0 comments on commit 42a0c5d

Please sign in to comment.