Skip to content

Commit

Permalink
Adds twitch support
Browse files Browse the repository at this point in the history
Resolves brave#13139

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Feb 21, 2018
1 parent aedf064 commit e47adca
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 131 deletions.
9 changes: 7 additions & 2 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,7 @@ const roundtrip = (params, options, callback) => {
: typeof params.server !== 'undefined' ? params.server
: typeof options.server === 'string' ? urlParse(options.server) : options.server
const binaryP = options.binaryP
const rawP = binaryP || options.rawP
const rawP = binaryP || options.rawP || options.scrapeP

if (!params.method) params.method = 'GET'
parts = underscore.extend(underscore.pick(parts, ['protocol', 'hostname', 'port']),
Expand Down Expand Up @@ -2399,10 +2399,15 @@ const onMediaRequest = (state, xhr, type, tabId) => {

const parsed = ledgerUtil.getMediaData(xhr, type)
const mediaId = ledgerUtil.getMediaId(parsed, type)

if (mediaId == null) {
return state
}

const mediaKey = ledgerUtil.getMediaKey(mediaId, type)
let duration = ledgerUtil.getMediaDuration(parsed, type)

if (mediaId == null || duration == null || mediaKey == null) {
if (duration == null || mediaKey == null) {
return state
}

Expand Down
3 changes: 2 additions & 1 deletion app/common/constants/ledgerMediaProviders.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const providers = {
YOUTUBE: 'youtube'
YOUTUBE: 'youtube',
TWITCH: 'twitch'
}

module.exports = providers
82 changes: 70 additions & 12 deletions app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,21 @@ const getMediaId = (data, type) => {
id = data.docid
break
}
case ledgerMediaProviders.TWITCH:
{
if (
data.event === 'video-play' ||
data.event === 'minute-watched'
) {
id = data.properties.channel
let vod = data.properties.vod

if (vod) {
vod = vod.replace('v', '')
id += `_vod_${vod}`
}
}
}
}

return id
Expand All @@ -241,16 +256,28 @@ const getMediaData = (xhr, type) => {
return result
}

const parsedUrl = urlParse(xhr)
const query = parsedUrl && parsedUrl.query

if (!parsedUrl || !query) {
return null
}

switch (type) {
case ledgerMediaProviders.YOUTUBE:
{
const parsedUrl = urlParse(xhr)
let query = null

if (parsedUrl && parsedUrl.query) {
query = queryString.parse(parsedUrl.query)
result = queryString.parse(parsedUrl.query)
break
}
case ledgerMediaProviders.TWITCH:
{
result = queryString.parse(parsedUrl.query)
let obj = Buffer.from(result.data, 'base64').toString('utf8')
if (obj == null) {
break
}
result = query

result = JSON.parse(obj)
break
}
}
Expand All @@ -261,15 +288,35 @@ const getMediaData = (xhr, type) => {
const getMediaDuration = (data, type) => {
let duration = 0
switch (type) {
case ledgerMediaProviders.YOUTUBE: {
duration = getYouTubeDuration(data)
break
}
case ledgerMediaProviders.YOUTUBE:
{
duration = getYouTubeDuration(data)
break
}
case ledgerMediaProviders.TWITCH:
{
duration = getTwitchDuration(data)
break
}
}

return duration
}

const getTwitchDuration = (data) => {
let time = 0

if (data == null) {
return time
}

if (data.properties.minutes_logged) {
time = 60 * 1000 // 1 min
}

return time
}

const getYouTubeDuration = (data) => {
let time = 0

Expand All @@ -294,7 +341,7 @@ const getYouTubeDuration = (data) => {
return parseInt(time)
}

const getMediaProvider = (url) => {
const getMediaProvider = (url, firstPartyUrl, referrer) => {
let provider = null

if (url == null) {
Expand All @@ -303,7 +350,18 @@ const getMediaProvider = (url) => {

// Youtube
if (url.startsWith('https://www.youtube.com/api/stats/watchtime?')) {
provider = ledgerMediaProviders.YOUTUBE
return ledgerMediaProviders.YOUTUBE
}

// Twitch
if (
(
firstPartyUrl.startsWith('https://www.twitch.tv') ||
referrer.startsWith('https://player.twitch.tv')
) &&
url.startsWith('https://api.mixpanel.com')
) {
return ledgerMediaProviders.TWITCH
}

return provider
Expand Down
18 changes: 9 additions & 9 deletions app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,21 @@ function registerForBeforeRequest (session, partition) {
}

const firstPartyUrl = module.exports.getMainFrameUrl(details)
const url = details.url
// this can happen if the tab is closed and the webContents is no longer available
if (!firstPartyUrl) {
muonCb({ cancel: true })
return
}

if (module.exports.isResourceEnabled('ledger') && module.exports.isResourceEnabled('ledgerMedia')) {
// Ledger media
const provider = ledgerUtil.getMediaProvider(url, firstPartyUrl, details.referrer)
if (provider) {
appActions.onLedgerMediaData(url, provider, details.tabId)
}
}

for (let i = 0; i < beforeRequestFilteringFns.length; i++) {
let results = beforeRequestFilteringFns[i](details, isPrivate)
const isAdBlock = (results.resourceName === appConfig.resourceNames.ADBLOCK) ||
Expand Down Expand Up @@ -201,22 +210,13 @@ function registerForBeforeRequest (session, partition) {
}
}
// Redirect to non-script version of DDG when it's blocked
const url = details.url
if (details.resourceType === 'mainFrame' &&
url.startsWith('https://duckduckgo.com/?q') &&
module.exports.isResourceEnabled('noScript', url, isPrivate)) {
muonCb({redirectURL: url.replace('?q=', 'html?q=')})
} else {
muonCb({})
}

if (module.exports.isResourceEnabled('ledger') && module.exports.isResourceEnabled('ledgerMedia')) {
// Ledger media
const provider = ledgerUtil.getMediaProvider(url)
if (provider) {
appActions.onLedgerMediaData(url, provider, details.tabId)
}
}
})
}

Expand Down
Loading

0 comments on commit e47adca

Please sign in to comment.