From 9835934e9e6938ea73ad46477178b4c51cbd8975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ted=20=C3=85kerlund?= Date: Wed, 8 Dec 2021 10:40:01 +0100 Subject: [PATCH] Added support for videos streamed from google drive --- lib/index.d.ts | 1 + lib/index.js | 1 + lib/provider/googledrive.d.ts | 14 +++++++ lib/provider/googledrive.js | 72 ++++++++++++++++++++++++++++++++ lib/provider/googledrive.test.js | 51 ++++++++++++++++++++++ lib/urlParser.js | 6 +++ 6 files changed, 145 insertions(+) create mode 100644 lib/provider/googledrive.d.ts create mode 100644 lib/provider/googledrive.js create mode 100644 lib/provider/googledrive.test.js diff --git a/lib/index.d.ts b/lib/index.d.ts index 7bd24b1..e876d3b 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -5,6 +5,7 @@ export * from './provider/allocine'; export * from './provider/canalplus'; export * from './provider/coub'; export * from './provider/dailymotion'; +export * from './provider/googledrive'; export * from './provider/loom'; export * from './provider/soundcloud'; export * from './provider/teachertube'; diff --git a/lib/index.js b/lib/index.js index 0df118c..38d50d3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,6 +9,7 @@ require('./provider/vimeo'); require('./provider/wistia'); require('./provider/youku'); require('./provider/youtube'); +require('./provider/googledrive'); require('./provider/soundcloud'); require('./provider/teachertube'); require('./provider/tiktok'); diff --git a/lib/provider/googledrive.d.ts b/lib/provider/googledrive.d.ts new file mode 100644 index 0000000..bb6012d --- /dev/null +++ b/lib/provider/googledrive.d.ts @@ -0,0 +1,14 @@ +import { VideoInfo } from '../urlParser'; + +export interface GoogledriveUrlParameters { + start?: number; + [key: string]: any; +} + +export type GoogledriveMediaTypes = 'video'; + +export interface GoogledriveVideoInfo extends VideoInfo { + provider: 'googledrive'; +} + +export type GoogledriveParseResult = GoogledriveVideoInfo | undefined; diff --git a/lib/provider/googledrive.js b/lib/provider/googledrive.js new file mode 100644 index 0000000..a269895 --- /dev/null +++ b/lib/provider/googledrive.js @@ -0,0 +1,72 @@ +const { + combineParams, + getTime, +} = require('../util'); + +function Googledrive() { + this.provider = 'googledrive'; + this.defaultFormat = 'long'; + this.formats = { + long: this.createLongUrl, + }; + this.mediaTypes = { + VIDEO: 'video', + }; +} + +module.exports = Googledrive; + + +Googledrive.prototype.parseUrl = function(url) { + var match = url.match(/(?:\/file\/d\/)([A-Za-z0-9]+)(?:\/view)/i); + // https://drive.google.com/file/d/9OPhkjOO8I140ABwWtyY0cG0riyVB89B/view + return match ? match[1] : undefined; +}; + +Googledrive.prototype.parseParameters = function(params, result) { + if (params.start || params.t) { + params.start = getTime(params.start || params.t); + delete params.t; + } + // if (params.v === result.id) { + // delete params.v; + // } + // if (params.list === result.id) { + // delete params.list; + // } + + return params; +}; + +Googledrive.prototype.parse = function(url, params) { + var result = { + mediaType: this.mediaTypes.VIDEO, + id: this.parseUrl(url), + }; + result.params = this.parseParameters(params, result); + return result.id ? result : undefined; +}; + +Googledrive.prototype.createLongUrl = function(vi, params) { +// Googledrive.prototype.createLongUrl = function(vi) { + + //Create the url depending on the media type + if (vi.mediaType !== this.mediaTypes.VIDEO || !vi.id) { + // return 'https://drive.google.com/file/d/9OPhkjOO8I140ABwWtyY0cG0riyVB89B/view'; + return undefined; + } + + var url = 'https://drive.google.com/file/d/' + vi.id + '/view'; + // Add query parameters back e.g. + // https://template.com/example/id/abcde?foo=bar&baz=qux + + if (params.start) { + params.t = params.start; + delete (params.start); + } + url += combineParams(params); + + return url; + +}; +require('../base').bind(new Googledrive()); diff --git a/lib/provider/googledrive.test.js b/lib/provider/googledrive.test.js new file mode 100644 index 0000000..fa773b2 --- /dev/null +++ b/lib/provider/googledrive.test.js @@ -0,0 +1,51 @@ +const Googledrive = require('./googledrive'); +const UrlParser = require('../urlParser'); +const { + testUrls, +} = require('../testUrls'); + +function newParser() { + const parser = new UrlParser(); + parser.bind(new Googledrive()); + return parser; +} + +test('Googledrive: undefined', () => { + expect(newParser().parse('https://drive.google.com')).toBe(undefined); + expect(newParser().create({ videoInfo: { provider: 'googledrive' } })).toBe(undefined); + expect(newParser().create({ videoInfo: { provider: 'googledrive', mediaType: 'video' } })).toBe(undefined); +}); + +test('Googledrive: urls', () => { + testUrls(newParser(), { + videoInfo: { + provider: 'googledrive', + id: '9OPhkjOO8I140ABwWtyY0cG0riyVB89B', + mediaType: 'video', + }, + formats: { + long: 'https://drive.google.com/file/d/9OPhkjOO8I140ABwWtyY0cG0riyVB89B/view', + }, + urls: [ + 'https://drive.google.com/file/d/9OPhkjOO8I140ABwWtyY0cG0riyVB89B/view', + ], + }); +}); +test('Googledrive: timed url', () => { + testUrls(newParser(), { + videoInfo: { + provider: 'googledrive', + id: '9OPhkjOO8I140ABwWtyY0cG0riyVB89B', + mediaType: 'video', + params: { + start: 100, + }, + }, + formats: { + long: 'https://drive.google.com/file/d/9OPhkjOO8I140ABwWtyY0cG0riyVB89B/view?t=100', + }, + urls: [ + 'https://drive.google.com/file/d/9OPhkjOO8I140ABwWtyY0cG0riyVB89B/view?t=100', + ], + }); +}); diff --git a/lib/urlParser.js b/lib/urlParser.js index 007d131..938f502 100644 --- a/lib/urlParser.js +++ b/lib/urlParser.js @@ -17,6 +17,12 @@ function UrlParser() { module.exports = UrlParser; UrlParser.prototype.parseProvider = function(url) { + var googleDriveMatch = url.match( + /(?:(?:https?:)?\/\/)?(drive\.google)\./i + ); + if (googleDriveMatch) { + return 'googledrive'; + } var match = url.match( /(?:(?:https?:)?\/\/)?(?:[^.]+\.)?(\w+)\./i );