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

Commit

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

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Feb 14, 2018
1 parent 6fd0946 commit b8b0d8e
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 141 deletions.
7 changes: 6 additions & 1 deletion app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2501,10 +2501,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 @@ -222,6 +222,19 @@ const getMediaId = (data, type) => {
id = data.docid
break
}
case ledgerMediaProviders.TWITCH:
{
if (
data.event === 'video-play' ||
data.event === 'minute-watched'
) {
id = data.properties.channel

if (data.properties.vod) {
id += `_vod_${data.properties.vod}`
}
}
}
}

return id
Expand All @@ -242,16 +255,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 @@ -262,15 +287,37 @@ 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
} else {
time = 1000 // 1s TODO do we want to log 1s when we start video play?
}

return time
}

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

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

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

if (url == null) {
Expand All @@ -304,7 +351,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 b8b0d8e

Please sign in to comment.