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
Moving values to static
  • Loading branch information
Jason Sadler committed Dec 3, 2018
1 parent 16bfe91 commit 6b083e8
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
.vscode
build
5 changes: 5 additions & 0 deletions include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ class LEDGER_EXPORT Ledger {
const uint32_t date) = 0;
virtual void RemoveRecurring(const std::string& publisher_key) = 0;
virtual double GetDefaultContributionAmount() = 0;
virtual void OnTwitchInfo(
const std::string& page_blob,
ledger::PUBLISHER_MONTH month,
int year,
const std::string& url) = 0;
};

} // namespace ledger
Expand Down
135 changes: 123 additions & 12 deletions src/bat_get_media.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,6 @@ void BatGetMedia::getPublisherInfoDataCallback(const std::string& mediaId,
uint64_t realDuration = getTwitchDuration(oldEvent, newEvent);
twitchEvents[media_key] = newEvent;

if (realDuration == 0) {
return;
}

std::string twitchMediaID = mediaId;
std::string mediaUrl = getMediaURL(twitchMediaID, providerName);

Expand All @@ -163,7 +159,7 @@ void BatGetMedia::getPublisherInfoDataCallback(const std::string& mediaId,
}

twitchMediaID = media_props[0];
mediaUrl = getMediaURL(twitchMediaID, providerName);
mediaUrl = TWITCH_VOD_URL + media_props[2];
std::string oembed_url = (std::string)TWITCH_VOD_URL + media_props[media_props.size() - 1];
updated_visit_data.name = twitchMediaID;
updated_visit_data.url = mediaUrl + "/videos";
Expand Down Expand Up @@ -312,7 +308,8 @@ uint64_t BatGetMedia::getTwitchDuration(const ledger::TwitchEventInfo& oldEventI

void BatGetMedia::onFetchFavIcon(const std::string& publisher_key,
bool success,
const std::string& favicon_url) {
const std::string& favicon_url,
const std::string& name) {
uint64_t currentReconcileStamp = ledger_->GetReconcileStamp();
auto filter = ledger_->CreatePublisherFilter(publisher_key,
ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE,
Expand All @@ -323,13 +320,15 @@ void BatGetMedia::onFetchFavIcon(const std::string& publisher_key,
currentReconcileStamp);
ledger_->GetPublisherInfo(filter,
std::bind(&BatGetMedia::onFetchFavIconDBResponse,
this, _1, _2, favicon_url));
this, _1, _2, favicon_url, name));
}

void BatGetMedia::onFetchFavIconDBResponse(ledger::Result result,
std::unique_ptr<ledger::PublisherInfo> info,
const std::string& favicon_url) {
const std::string& favicon_url,
const std::string& name) {
if (result == ledger::Result::LEDGER_OK && !favicon_url.empty()) {
info->name = name;
info->favicon_url = favicon_url;

ledger_->SetPublisherInfo(std::move(info),
Expand Down Expand Up @@ -384,20 +383,28 @@ void BatGetMedia::getPublisherFromMediaPropsCallback(const uint64_t& duration,
if (providerName == TWITCH_MEDIA_TYPE) {
std::string fav_icon;
braveledger_bat_helper::getJSONValue("author_thumbnail_url", response, fav_icon);
std::string author_url;
braveledger_bat_helper::getJSONValue("author_url", response, author_url);
std::string author_name;
braveledger_bat_helper::getJSONValue("author_name", response, author_name);

std::string media_url(mediaURL);
std::string twitchMediaID = visit_data.name;
if (twitchMediaID == "twitch") {
twitchMediaID = extractData(author_url, TWITCH_FULL_URL, "/");
media_url = getMediaURL(twitchMediaID, providerName);
}
std::string id = providerName + "#author:" + twitchMediaID;

ledger::VisitData updated_visit_data(visit_data);
updated_visit_data.favicon_url = "https://" + ledger_->GenerateGUID() + ".invalid";
updated_visit_data.name = author_name;
updated_visit_data.url = media_url;

if (fav_icon.length() > 0) {
ledger_->FetchFavIcon(fav_icon,
updated_visit_data.favicon_url,
std::bind(&BatGetMedia::onFetchFavIcon, this, id, _1, _2));
std::bind(&BatGetMedia::onFetchFavIcon, this, id,
_1, _2, updated_visit_data.name));
}

ledger_->SaveMediaVisit(id, updated_visit_data, duration, window_id);
Expand Down Expand Up @@ -467,7 +474,8 @@ void BatGetMedia::savePublisherInfo(const uint64_t& duration,
".invalid";
ledger_->FetchFavIcon(favIconURL,
favicon_key,
std::bind(&BatGetMedia::onFetchFavIcon, this, publisher_id, _1, _2));
std::bind(&BatGetMedia::onFetchFavIcon, this,
publisher_id, _1, _2, publisherName));
}

ledger::VisitData updated_visit_data(visit_data);
Expand Down Expand Up @@ -567,7 +575,17 @@ 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,
visit_data.url);
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 @@ -809,6 +827,17 @@ 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/", "/");
if (mediaId == "videos") {
mediaId = "twitch_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 @@ -819,6 +848,20 @@ std::string BatGetMedia::getYoutubeMediaKeyFromUrl(
return std::string();
}

std::string BatGetMedia::getTwitchMediaKeyFromUrl(
const std::string& provider_type,
const std::string& id,
const std::string& url) {
if (id == "twitch") {
return std::string();
}
if (url.find("twitch.tv/videos") != std::string::npos) {
std::string vodId = extractData(url, "twitch.tv/videos/", "/");
return provider_type + "_vod_" + vodId;
}
return provider_type + "_" + id;
}

std::string BatGetMedia::getYoutubePublisherKeyFromUrl(const ledger::VisitData& visit_data) {
return extractData(visit_data.path + "/", "/channel/", "/");
}
Expand All @@ -839,6 +882,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 @@ -848,4 +893,70 @@ 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;
if (url.find("twitch.tv/videos") != std::string::npos) {
std::string vodId = extractData(url, "twitch.tv/videos/", "/");
mediaKey = "twitch_vod_" + vodId;
} else {
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) {
std::string favicon_key = "https://" + ledger_->GenerateGUID() +
".invalid";
ledger_->FetchFavIcon(faviconUrl,
favicon_key,
std::bind(
&BatGetMedia::onFetchFavIcon, this,
publisher_info->id, _1, _2, name));
}
}

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
28 changes: 26 additions & 2 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 @@ -83,11 +88,13 @@ class BatGetMedia {

void onFetchFavIcon(const std::string& publisher_key,
bool success,
const std::string& favicon_url);
const std::string& favicon_url,
const std::string& name);

void onFetchFavIconDBResponse(ledger::Result result,
std::unique_ptr<ledger::PublisherInfo> info,
const std::string& favicon_url);
const std::string& favicon_url,
const std::string& name);

std::string getTwitchStatus(const ledger::TwitchEventInfo& oldEventInfo,
const ledger::TwitchEventInfo& newEventInfo);
Expand Down Expand Up @@ -180,8 +187,13 @@ 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,
const std::string& url);

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

Expand All @@ -199,6 +211,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
8 changes: 6 additions & 2 deletions src/bat_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2259,7 +2259,7 @@ static bool ignore_ = false;
if (iter != data.end()) {
std::string idAddition(iter->second);
if (idAddition.find('v') != std::string::npos) {
id += "_vod_" + braveledger_bat_helper::split(idAddition, 'v')[1];
id = "_vod_" + braveledger_bat_helper::split(idAddition, 'v')[1];
}
}

Expand All @@ -2273,7 +2273,11 @@ static bool ignore_ = false;
}

std::string getMediaKey(const std::string& mediaId, const std::string& type) {
return type + "_" + mediaId;
if (mediaId.find("_vod_") != std::string::npos) {
return type + mediaId;
} else {
return type + "_" + mediaId;
}
}

uint64_t getMediaDuration(const std::map<std::string, std::string>& data, const std::string& media_key, const std::string& type) {
Expand Down
15 changes: 15 additions & 0 deletions src/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,14 @@ 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);
}

std::unique_ptr<ledger::LogStream> LedgerImpl::Log(
const char* file,
Expand Down Expand Up @@ -1052,4 +1060,11 @@ double LedgerImpl::GetDefaultContributionAmount() {
return bat_state_->GetDefaultContributionAmount();
}

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 @@ -201,6 +201,11 @@ class LedgerImpl : public ledger::Ledger,
const char* file,
int line,
const ledger::LogLevel log_level) const;
ledger::PublisherInfoFilter CreatePublisherFilter(
const std::string& publisher_id,
ledger::PUBLISHER_CATEGORY category,
ledger::PUBLISHER_MONTH month,
int year);
void LogResponse(const std::string& func_name,
bool result,
const std::string& response,
Expand Down Expand Up @@ -276,6 +281,11 @@ class LedgerImpl : public ledger::Ledger,

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,
int level = -1);
Expand Down
1 change: 1 addition & 0 deletions src/static_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#define YOUTUBE_TLD "youtube.com"
#define TWITCH_TLD "twitch.tv"
#define TWITCH_VOD_URL "https://www.twitch.tv/videos/"
#define TWITCH_FULL_URL "https://www.twitch.tv/"
#define MEDIA_DELIMITER '_'
#define WALLET_PASSPHRASE_DELIM ' '
#define DICTIONARY_DELIMITER '\n'
Expand Down

0 comments on commit 6b083e8

Please sign in to comment.