1
0
Fork 1
mirror of https://codeberg.org/keyoxide/doipjs.git synced 2025-04-23 14:51:32 -06:00

Added YouTube

This commit is contained in:
Daniel Levi 2025-02-26 16:38:42 +02:00
parent 496be64e82
commit 78ef4cddc9
No known key found for this signature in database
GPG key ID: D267DF29810DBDD5
2 changed files with 104 additions and 1 deletions
src/serviceProviders

View file

@ -43,6 +43,7 @@ import * as discord from './discord.js'
import * as bsky from './bsky.js'
import * as sourcehut from './sourcehut.js'
import * as pronounspage from './pronounspage.js'
import * as youtube from './youtube.js'
const _data = {
aspe,
@ -74,7 +75,8 @@ const _data = {
pronounspage,
discord,
bsky,
sourcehut
sourcehut,
youtube
}
export const list = Object.keys(_data)

View file

@ -0,0 +1,101 @@
/*
Copyright 2025 Daniel Levi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* YouTube service provider
* @module serviceProviders/youtube
* @example
* import { ServiceProviderDefinitions } from 'doipjs';
* const sp = ServiceProviderDefinitions.data.youtube.processURI('https://www.youtube.com/watch?v=VIDEO_ID');
*/
import * as E from '../enums.js'
import { ServiceProvider } from '../serviceProvider.js'
export const reURI = /https:\/\/www\.youtube\.com\/watch\?v=[a-zA-Z0-9_-]{11}/
/**
* @function
* @param {string} uri - Claim URI to process
* @returns {ServiceProvider} The service provider information based on the claim URI
*/
export function processURI (uri) {
const match = uri.match(reURI)
return new ServiceProvider({
about: {
id: 'youtube',
name: 'YouTube',
homepage: 'https://www.youtube.com'
},
profile: {
display: 'Unknown username',
uri: null,
qr: null
},
claim: {
uriRegularExpression: reURI.toString(),
uriIsAmbiguous: false
},
// Get proof from invites (https://discord.com/developers/docs/resources/invite#get-invite)
// See https://discord.com/developers/docs/reference#api-versioning for Discord's API versioning
proof: {
request: {
uri,
fetcher: E.Fetcher.HTTP,
accessRestriction: E.ProofAccessRestriction.NOCORS,
data: {
url: `https://www.youtube.com/oembed?url=${match[0]}`,
format: E.ProofFormat.JSON
}
},
response: {
format: E.ProofFormat.JSON
},
target: [
{
format: E.ClaimFormat.URI,
encoding: E.EntityEncodingFormat.PLAIN,
relation: E.ClaimRelation.EQUALS,
path: ['title']
}
]
}
})
}
export const functions = {
postprocess: async (claimData, proofData, opts) => {
claimData.profile.display = proofData.result.author_name
claimData.profile.uri = proofData.result.author_url
return { claimData, proofData }
}
}
export const tests = [
{
uri: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
shouldMatch: true
},
{
uri: 'https://www.youtube.com',
shouldMatch: false
},
{
uri: 'https://www.youtube.com/watch?v=',
shouldMatch: false
}
]