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

Commit 283fed2

Browse files
authored
Merge pull request #114 from brave-intl/feature/fine_tune_time
Youtube and Twitch videos now provide more accurate times than before
2 parents 695193e + 22b0856 commit 283fed2

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

src/bat_get_media.cc

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "bat_get_media.h"
77

88
#include <sstream>
9+
#include <cmath>
910

1011
#include "bat_get_media.h"
1112
#include "bat_helper.h"
@@ -95,14 +96,8 @@ void BatGetMedia::getPublisherInfoDataCallback(const std::string& mediaId, const
9596
return;
9697
}
9798

98-
std::vector<std::string> split = braveledger_bat_helper::split(mediaId, '_');
99-
std::string new_media_id = mediaId;
100-
if (!split.empty()) {
101-
new_media_id = split[0];
102-
}
103-
10499
if (!publisher_info.get()) {
105-
std::string mediaURL = getMediaURL(new_media_id, providerName);
100+
std::string mediaURL = getMediaURL(mediaId, providerName);
106101
if (YOUTUBE_MEDIA_TYPE == providerName) {
107102
auto request = ledger_->LoadURL((std::string)YOUTUBE_PROVIDER_URL + "?format=json&url=" + ledger_->URIEncode(mediaURL),
108103
std::vector<std::string>(), "", "", ledger::URL_METHOD::GET, &handler_);
@@ -118,12 +113,16 @@ void BatGetMedia::getPublisherInfoDataCallback(const std::string& mediaId, const
118113
_2,
119114
_3));
120115
} else if (TWITCH_MEDIA_TYPE == providerName) {
121-
const std::string mediaUrl = getMediaURL(new_media_id, providerName);
116+
const std::string twitchMediaID =
117+
mediaId.find(MEDIA_DELIMITER) != std::string::npos ?
118+
mediaId :
119+
braveledger_bat_helper::split(mediaId, MEDIA_DELIMITER)[0];
120+
const std::string mediaUrl = getMediaURL(twitchMediaID, providerName);
122121
std::unique_ptr<ledger::PublisherInfo> new_publisher_info(new ledger::PublisherInfo());
123122
new_publisher_info->favicon_url = "";
124123
new_publisher_info->url = mediaUrl + "/videos";
125-
std::string id = providerName + "#author:" + new_media_id;
126-
new_publisher_info->name = new_media_id;
124+
std::string id = providerName + "#author:" + twitchMediaID;
125+
new_publisher_info->name = twitchMediaID;
127126
new_publisher_info->id = id;
128127

129128
ledger::TwitchEventInfo oldEvent;
@@ -259,11 +258,15 @@ uint64_t BatGetMedia::getTwitchDuration(const ledger::TwitchEventInfo& oldEventI
259258
return 0;
260259
}
261260

261+
if (oldEventInfo.status_.empty()) { // if autoplay is off and play is pressed
262+
return 0;
263+
}
264+
262265
if (time > TWITCH_MAXIMUM_SECONDS_CHUNK) {
263266
time = TWITCH_MAXIMUM_SECONDS_CHUNK;
264267
}
265268

266-
return (uint64_t)time;
269+
return (uint64_t)std::round(time);
267270
}
268271

269272
void BatGetMedia::getPublisherFromMediaPropsCallback(const uint64_t& duration, const std::string& media_key,

src/bat_helper.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,17 +1864,21 @@ static bool ignore_ = false;
18641864
if (startTime.size() != endTime.size()) {
18651865
return 0;
18661866
}
1867-
double tempTime = 0;
1867+
// get all the intervals and combine them.
1868+
// (Should only be one set if there were no seeks)
18681869
for (size_t i = 0; i < startTime.size(); i++) {
18691870
std::stringstream tempET(endTime[i]);
18701871
std::stringstream tempST(startTime[i]);
18711872
double st = 0;
18721873
double et = 0;
18731874
tempET >> et;
18741875
tempST >> st;
1875-
tempTime = et - st;
1876+
1877+
// round instead of truncate
1878+
// also make sure we include previous iterations
1879+
// if more than one set exists
1880+
duration += (uint64_t)std::round(et - st);
18761881
}
1877-
duration = (uint64_t)tempTime;
18781882
}
18791883
} else if (TWITCH_MEDIA_TYPE == type) {
18801884
// We set the correct duration for twitch in BatGetMedia class

src/static_values.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#define YOUTUBE_PROVIDER_URL "https://www.youtube.com/oembed"
5454
#define YOUTUBE_TLD "youtube.com"
5555
#define TWITCH_TLD "twitch.com"
56+
#define MEDIA_DELIMITER '_'
5657

5758
#define SEED_LENGTH 32
5859
#define SALT_LENGTH 64

0 commit comments

Comments
 (0)