1
0
mirror of https://gitlab.com/Cha_deL/peertubeify synced 2024-12-12 08:17:00 +01:00

Refactor PeertubeAPI usage

This commit is contained in:
Booteille 2018-11-12 21:34:43 +01:00
parent bf87557b84
commit 5c35ce031d
No known key found for this signature in database
GPG Key ID: 7FC1ED300B74CD91
5 changed files with 41 additions and 16 deletions

View File

@ -21,6 +21,7 @@ import { MessageKind, RedirectType } from './types';
import constants from './constants'; import constants from './constants';
import Preferences from './preferences'; import Preferences from './preferences';
import InvidiousAPI from './invidious-api'; import InvidiousAPI from './invidious-api';
import PeertubeAPI from './peertube-api';
import { getPeertubeVideoURL } from './util'; import { getPeertubeVideoURL } from './util';
/** /**
@ -53,13 +54,8 @@ const redirectYoutube = async (r) => {
throw new Error('No results.'); throw new Error('No results.');
}; };
const buildSearchByNameURL = (instance: string, query: string): string => `https://${instance}/api/v1/search/videos?search=${encodeURIComponent(query)}`;
const searchByName = query => new Promise(async (resolve, reject) => { const searchByName = query => new Promise(async (resolve, reject) => {
const instance = _.getOr(constants.defaultInstance, 'searchInstance', await browser.storage.local.get()).toString(); PeertubeAPI.searchVideo({ search: query })
fetch(buildSearchByNameURL(instance, query))
.then(res => res.json())
.then(function(data) { .then(function(data) {
if (data.total > 0) { if (data.total > 0) {
const video = data.data[0] const video = data.data[0]
@ -71,15 +67,12 @@ const searchByName = query => new Promise(async (resolve, reject) => {
}); });
}); });
const buildSearchByIDURL = (instance: string, id: string): string => `https://${instance}/api/v1/videos/${id}`;
const searchByID = id => new Promise(async (resolve, reject) => { const searchByID = id => new Promise(async (resolve, reject) => {
const instance = _.getOr(constants.defaultInstance, 'searchInstance', await browser.storage.local.get()).toString(); const prefs = await Preferences.getPreferences();
fetch(buildSearchByIDURL(instance, id)) PeertubeAPI.getVideo(id)
.then(res => res.json())
.then(function(video) { .then(function(video) {
resolve({ url: `https://${instance}/videos/watch/${id}`, video }); resolve({ url: getPeertubeVideoURL(video, prefs), video });
}) })
}) })

View File

@ -1,5 +1,8 @@
export default { export default {
peertubeAPI: {
defaultInstance: 'peertube.social', defaultInstance: 'peertube.social',
endpoint: 'api/v1',
},
invidiousAPI: { invidiousAPI: {
url: 'https://invidio.us/api/v1', url: 'https://invidio.us/api/v1',
videos: 'videos', videos: 'videos',

View File

@ -12,7 +12,7 @@ export default class InvidiousAPI {
return fetch(`${constants.invidiousAPI.url}/${action}/${paramString}`) return fetch(`${constants.invidiousAPI.url}/${action}/${paramString}`)
.then(res => res.json()) .then(res => res.json())
.catch(e => console.error( .catch(e => console.error(
'An error occured while trying to fetch API used by PeerTubeify: ' 'An error occured while trying to fetch Invidious API used by PeerTubeify: '
+ e.message + e.message
)); ));
} }

29
src/peertube-api.ts Normal file
View File

@ -0,0 +1,29 @@
import * as _ from 'lodash/fp';
import constants from './constants';
export default class PeertubeAPI {
private static async fetchAPI(path: string, query?: Object) {
const instance = _.getOr(constants.peertubeAPI.defaultInstance, 'searchInstance', await browser.storage.local.get()).toString();
let url = `https://${constants.peertubeAPI.defaultInstance}/${constants.peertubeAPI.endpoint}/${path}`;
if (query) {
url = url + '?' + Object.keys(query).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(query[key])).join('&');
}
return fetch(url)
.then(res => res.json())
.catch(e => console.error(
`An error occured while trying to fetch ${instance} API used by PeerTubeify: `
+ e.message
));
}
static async getVideo(id: string) {
return this.fetchAPI('videos/' + id);
}
static async searchVideo(query: Object) {
return this.fetchAPI('search/videos', query);
}
}

View File

@ -28,7 +28,7 @@ export default class Preferences {
redirectPeertube: RedirectType; redirectPeertube: RedirectType;
constructor(localStorage) { constructor(localStorage) {
this.searchInstance = _.defaultTo(constants.defaultInstance, localStorage.searchInstance as string); this.searchInstance = _.defaultTo(constants.peertubeAPI.defaultInstance, localStorage.searchInstance as string);
this.openInOriginalInstance = _.defaultTo(true, localStorage.openInOriginalInstance as boolean); this.openInOriginalInstance = _.defaultTo(true, localStorage.openInOriginalInstance as boolean);
this.redirectYoutube = _.defaultTo(RedirectType.Show, localStorage.redirectYoutube) this.redirectYoutube = _.defaultTo(RedirectType.Show, localStorage.redirectYoutube)
this.redirectPeertube = _.defaultTo(RedirectType.None, localStorage.redirectPeertube) this.redirectPeertube = _.defaultTo(RedirectType.None, localStorage.redirectPeertube)
@ -54,6 +54,6 @@ export default class Preferences {
} }
set searchInstance(instance) { set searchInstance(instance) {
this._searchInstance = _.isEmpty(instance) ? constants.defaultInstance : stripProtocol(instance) this._searchInstance = _.isEmpty(instance) ? constants.peertubeAPI.defaultInstance : stripProtocol(instance)
} }
} }