-
Notifications
You must be signed in to change notification settings - Fork 14
add twitch#channel URI #11
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,14 @@ const getPublisherFromMediaProps = (mediaProps, options, callback) => { | |
} | ||
|
||
const mappers = { | ||
twitch: (mediaProps) => { | ||
const mediaId = mediaProps.mediaId | ||
|
||
if (!mediaId) throw new Error('expecting mediaId for provider Twitch') | ||
|
||
return ('https://www.twitch.tv/videos/' + mediaId) | ||
}, | ||
|
||
youtube: (mediaProps) => { | ||
const mediaId = mediaProps.mediaId | ||
|
||
|
@@ -37,7 +45,7 @@ const mappers = { | |
} | ||
|
||
const getPublisherFromMediaURL = (mediaURL, options, callback) => { | ||
let parts, providers | ||
let domains, hostname, parts, providers | ||
|
||
if (typeof options === 'function') { | ||
callback = options | ||
|
@@ -51,12 +59,17 @@ const getPublisherFromMediaURL = (mediaURL, options, callback) => { | |
else throw new Error('security audit requires options.roundtrip for non-debug use') | ||
|
||
parts = url.parse(mediaURL) | ||
if ((parts) && (parts.protocol !== 'https:')) return setTimeout(() => { callback(new Error('non-https URL'), null) }, 0) | ||
if ((!parts) || (parts.protocol !== 'https:')) return setTimeout(() => { callback(new Error('invalid URL'), null) }, 0) | ||
|
||
hostname = parts.hostname | ||
domains = [ hostname, tldjs.getDomain(hostname) ] | ||
|
||
if (hostname.indexOf('www.') === 0) domains.push('api.' + hostname.substr(4)) | ||
|
||
providers = underscore.filter(options.ruleset, (rule) => { | ||
const schemes = rule.schemes | ||
|
||
if (!schemes.length) return ((parts) && (tldjs.getDomain(parts.hostname) === rule.domain)) | ||
if (!schemes.length) return (domains.indexOf(rule.domain) !== -1) | ||
|
||
for (let scheme in schemes) if (mediaURL.match(new RegExp(scheme.replace(/\*/g, '(.*)'), 'i'))) return true | ||
}) | ||
|
@@ -78,6 +91,7 @@ const getPublisherFromProviders = (providers, mediaURL, options, firstErr, callb | |
if (!resolver) return done(new Error('no resolver for ' + provider.provider_name)) | ||
|
||
parts = url.parse(provider.url + '?' + querystring.stringify({ format: 'json', url: mediaURL })) | ||
|
||
retryTrip({ | ||
server: parts.protocol + '//' + parts.host, | ||
path: parts.path, | ||
|
@@ -91,13 +105,13 @@ const getPublisherFromProviders = (providers, mediaURL, options, firstErr, callb | |
} | ||
|
||
const resolvers = { | ||
YouTube: (providers, mediaURL, options, payload, firstErr, callback) => { | ||
_channel: (providers, mediaURL, options, payload, firstErr, callback) => { | ||
const provider = underscore.first(providers) | ||
const parts = url.parse(payload.author_url) | ||
let paths | ||
|
||
paths = parts && parts.pathname.split('/') | ||
if ((!paths) || (paths.length !== 3)) throw new Error('invalid author_url: ' + payload.author_url) | ||
if ((!paths) || (!payload._channel.validP(paths))) throw new Error('invalid author_url: ' + payload.author_url) | ||
|
||
cachedTrip({ | ||
server: parts.protocol + '//' + parts.host, | ||
|
@@ -107,12 +121,12 @@ const resolvers = { | |
if (err) return next(providers, mediaURL, options, firstErr || err, callback) | ||
|
||
metascraper.scrapeHtml(body).then((result) => { | ||
console.log('result: ' + JSON.stringify(result, null, 2)) | ||
|
||
const parts = url.parse(result.url) | ||
const cpaths = parts && parts.pathname.split('/') | ||
const channel = (parts.pathname === parts.path) && (cpaths.length === 3) && (cpaths[1] === 'channel') | ||
? cpaths[2] : paths[2] | ||
const channel = payload._channel.get(paths, parts) | ||
const publisherInfo = { | ||
publisher: 'youtube#channel:' + channel, | ||
publisher: payload._channel.providerName + '#channel:' + channel, | ||
publisherType: 'provider', | ||
publisherURL: payload.author_url + '/videos', | ||
providerName: provider.provider_name, | ||
|
@@ -154,6 +168,35 @@ const resolvers = { | |
next(providers, mediaURL, options, firstErr || err, callback) | ||
}) | ||
}) | ||
}, | ||
|
||
Twitch: (providers, mediaURL, options, payload, firstErr, callback) => { | ||
resolvers._channel(providers, mediaURL, options, underscore.extend({ | ||
_channel: { | ||
providerName: 'twitch', | ||
param1: 2, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we using this one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes. that's the point of the PR. i refactored so the twitch and youtube code could maximally reuse code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where do we use it? in other library? I can't find it in this PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. take a look at media/provider.json! |
||
validP: (paths) => { return (paths.length === 2) }, | ||
get: (paths, parts) => { | ||
const cpaths = parts && parts.pathname.split('/') | ||
|
||
return ((parts.pathname === parts.path) && (cpaths.length === 2) ? cpaths[1] : paths[1]) | ||
} | ||
} | ||
}, payload), firstErr, callback) | ||
}, | ||
|
||
YouTube: (providers, mediaURL, options, payload, firstErr, callback) => { | ||
resolvers._channel(providers, mediaURL, options, underscore.extend({ | ||
_channel: { | ||
providerName: 'youtube', | ||
validP: (paths) => { return (paths.length === 3) }, | ||
get: (paths, parts) => { | ||
const cpaths = parts && parts.pathname.split('/') | ||
|
||
return ((parts.pathname === parts.path) && (cpaths.length === 3) && (cpaths[1] === 'channel') ? cpaths[2] : paths[2]) | ||
} | ||
} | ||
}, payload), firstErr, callback) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "bat-publisher", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Routines to identify publishers for the BAT.", | ||
"main": "index.js", | ||
"scripts": { | ||
|
@@ -47,6 +47,7 @@ | |
"level": "1.7.0", | ||
"npm-check-updates": "^2.12.1", | ||
"nsp": "^2.8.0", | ||
"oembed-parser": "^1.0.8", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need security review for this one. I see that there is no new usage, where we just missing it from the dependencies? cc @diracdeltas There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should not use ombed-parser in general since it uses Node's networking stack (https://github.com/ndaidong/oembed-parser/blob/master/src/utils/fetchEmbed.js), but this usage is ok for now since it only uses the provider.json rules file |
||
"standard": "10.0.3", | ||
"tap": "^10.7.2" | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this contains potentially sensitive information about the page and should not be logged by default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops! debug thing not removed. fixed.