mirror of
https://github.com/artegoser/piped-api.git
synced 2024-11-27 13:26:21 +03:00
Compare commits
3 commits
c71702157f
...
5465963650
Author | SHA1 | Date | |
---|---|---|---|
5465963650 | |||
a37588554b | |||
6f0aaa4081 |
5 changed files with 179 additions and 160 deletions
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "piped-api",
|
"name": "piped-api",
|
||||||
"version": "1.1.2",
|
"version": "1.2.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "piped-api",
|
"name": "piped-api",
|
||||||
"version": "1.1.2",
|
"version": "1.2.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.4.0"
|
"axios": "^1.4.0"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "piped-api",
|
"name": "piped-api",
|
||||||
"version": "1.1.2",
|
"version": "1.2.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
@ -13,8 +13,8 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"docs": "typedoc src/index.ts",
|
"docs": "typedoc src/index.ts src/types.ts",
|
||||||
"prepublishOnly": "npm run build && npm run docs"
|
"prepublishOnly": "npm run build"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"api",
|
"api",
|
||||||
|
|
159
src/index.ts
159
src/index.ts
|
@ -1,2 +1,157 @@
|
||||||
export { PipedAPI } from "./piped-api";
|
import axios from "axios";
|
||||||
export * as Types from "./responses.interface";
|
import {
|
||||||
|
Channel,
|
||||||
|
Comments,
|
||||||
|
NextPageChannel,
|
||||||
|
NextPagePlaylist,
|
||||||
|
Playlist,
|
||||||
|
Search,
|
||||||
|
Sponsors,
|
||||||
|
Streams,
|
||||||
|
Tab,
|
||||||
|
Video,
|
||||||
|
} from "./types";
|
||||||
|
|
||||||
|
export class PipedAPI {
|
||||||
|
host: string;
|
||||||
|
constructor(host: string = "https://pipedapi.kavin.rocks") {
|
||||||
|
this.host = host;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves data from the specified URL.
|
||||||
|
*
|
||||||
|
* @param {string} url - The URL from which to retrieve data.
|
||||||
|
* @return {Promise<any>} - A Promise that resolves to the parsed JSON data.
|
||||||
|
*/
|
||||||
|
private async _get(url: string) {
|
||||||
|
const data = await axios.get(`${this.host}${url}`);
|
||||||
|
return data.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the trending videos for a specific region.
|
||||||
|
*
|
||||||
|
* @param {string} region - The region for which to retrieve trending videos.
|
||||||
|
* @return {Promise<Video[]>} - A promise that resolves to an array of Video objects representing the trending videos.
|
||||||
|
*/
|
||||||
|
async trending(region: string): Promise<Video[]> {
|
||||||
|
return await this._get(`/trending?region=${region}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the streams for a given video ID.
|
||||||
|
*
|
||||||
|
* @param {string} videoId - The ID of the video.
|
||||||
|
* @return {Promise<Streams>} A promise that resolves to the streams for the given video ID.
|
||||||
|
*/
|
||||||
|
async streams(videoId: string): Promise<Streams> {
|
||||||
|
return await this._get(`/streams/${videoId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves comments for a specific video.
|
||||||
|
*
|
||||||
|
* @param {string} videoId - The ID of the video for which to retrieve comments.
|
||||||
|
* @param {string} nextpage - The JSON encoded nextpage variable, to be sent as a query string (optional).
|
||||||
|
* @returns {Promise<Comments>} A Promise that resolves to the comments for the video.
|
||||||
|
*/
|
||||||
|
async comments(videoId: string, nextpage?: string): Promise<Comments> {
|
||||||
|
return nextpage
|
||||||
|
? await this._get(`/nextpage/comments/${videoId}?nextpage=${nextpage}`)
|
||||||
|
: await this._get(`/comments/${videoId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a channel by its ID.
|
||||||
|
*
|
||||||
|
* @param {string} id - The ID of the channel to retrieve.
|
||||||
|
* @param {string} nextpage - The JSON encoded nextpage variable, to be sent as a query string (optional).
|
||||||
|
* @return {Promise<Channel | NextPageChannel>} A Promise that resolves to the retrieved Channel object.
|
||||||
|
*/
|
||||||
|
async channel(
|
||||||
|
id: string,
|
||||||
|
nextpage?: string
|
||||||
|
): Promise<Channel | NextPageChannel> {
|
||||||
|
return nextpage
|
||||||
|
? await this._get(`/nextpage/channel/${id}?nextpage=${nextpage}`)
|
||||||
|
: await this._get(`/channel/${id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async channelTabs(data: string): Promise<Tab> {
|
||||||
|
return await this._get(`/channels/tabs/?data=${data}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a channel by its name.
|
||||||
|
*
|
||||||
|
* @param {string} name - The name of the channel.
|
||||||
|
* @return {Promise<Channel>} A promise that resolves to the channel object.
|
||||||
|
*/
|
||||||
|
async channelByName(name: string): Promise<Channel> {
|
||||||
|
return await this._get(`/c/${name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a user by their name.
|
||||||
|
*
|
||||||
|
* @param {string} name - The name of the user.
|
||||||
|
* @return {Promise<Channel>} A Promise that resolves to the user's channel.
|
||||||
|
*/
|
||||||
|
async user(name: string): Promise<Channel> {
|
||||||
|
return await this._get(`/user/${name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a playlist based on its ID.
|
||||||
|
*
|
||||||
|
* @param {string} id - The ID of the playlist to retrieve.
|
||||||
|
* @param {string} nextpage - The token for the next page of results (optional).
|
||||||
|
* @return {Promise<Playlist | NextPagePlaylist>} A Promise that resolves to a Playlist or NextPagePlaylist object.
|
||||||
|
*/
|
||||||
|
async playlist(
|
||||||
|
id: string,
|
||||||
|
nextpage?: string
|
||||||
|
): Promise<Playlist | NextPagePlaylist> {
|
||||||
|
return nextpage
|
||||||
|
? await this._get(`/nextpage/playlists/${id}?nextpage=${nextpage}`)
|
||||||
|
: await this._get(`/playlists/${id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves suggestions based on the given query.
|
||||||
|
*
|
||||||
|
* @param {string} query - The query string to search for suggestions.
|
||||||
|
* @return {Promise<string[]>} A promise that resolves to an array of string suggestions.
|
||||||
|
*/
|
||||||
|
async suggestions(query: string): Promise<string[]> {
|
||||||
|
return await this._get(`/suggestions?query=${query}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves sponsors based on the provided ID and category.
|
||||||
|
*
|
||||||
|
* @param {string} id - The ID of the video.
|
||||||
|
* @param {string[]} category - The category of sponsors you would like to skip. Example: ["sponsor"].
|
||||||
|
* @return {Promise<Sponsors>} A promise that resolves to the sponsors.
|
||||||
|
*/
|
||||||
|
async sponsors(
|
||||||
|
id: string,
|
||||||
|
category: string[] = ["sponsor"]
|
||||||
|
): Promise<Sponsors> {
|
||||||
|
return await this._get(
|
||||||
|
`/sponsors/${id}?category=${JSON.stringify(category)}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches videos for a given query with an optional filter.
|
||||||
|
*
|
||||||
|
* @param {string} query - The query to search for.
|
||||||
|
* @param {string} [filter="all"] - The optional filter to apply.
|
||||||
|
* @returns {Promise<Search>} - A Promise that resolves to the search results.
|
||||||
|
*/
|
||||||
|
async search(query: string, filter: string = "all"): Promise<Search> {
|
||||||
|
return await this._get(`/search?q=${query}&filter=${filter}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
152
src/piped-api.ts
152
src/piped-api.ts
|
@ -1,152 +0,0 @@
|
||||||
import axios from "axios";
|
|
||||||
import {
|
|
||||||
Channel,
|
|
||||||
Comments,
|
|
||||||
NextPageChannel,
|
|
||||||
NextPagePlaylist,
|
|
||||||
Playlist,
|
|
||||||
Search,
|
|
||||||
Sponsors,
|
|
||||||
Streams,
|
|
||||||
Video,
|
|
||||||
} from "./responses.interface";
|
|
||||||
|
|
||||||
export class PipedAPI {
|
|
||||||
host: string;
|
|
||||||
constructor(host: string = "https://pipedapi.kavin.rocks") {
|
|
||||||
this.host = host;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves data from the specified URL.
|
|
||||||
*
|
|
||||||
* @param {string} url - The URL from which to retrieve data.
|
|
||||||
* @return {Promise<any>} - A Promise that resolves to the parsed JSON data.
|
|
||||||
*/
|
|
||||||
private async _get(url: string) {
|
|
||||||
const data = await axios.get(`${this.host}${url}`);
|
|
||||||
return data.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the trending videos for a specific region.
|
|
||||||
*
|
|
||||||
* @param {string} region - The region for which to retrieve trending videos.
|
|
||||||
* @return {Promise<Video[]>} - A promise that resolves to an array of Video objects representing the trending videos.
|
|
||||||
*/
|
|
||||||
async trending(region: string): Promise<Video[]> {
|
|
||||||
return await this._get(`/trending?region=${region}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the streams for a given video ID.
|
|
||||||
*
|
|
||||||
* @param {string} videoId - The ID of the video.
|
|
||||||
* @return {Promise<Streams>} A promise that resolves to the streams for the given video ID.
|
|
||||||
*/
|
|
||||||
async streams(videoId: string): Promise<Streams> {
|
|
||||||
return await this._get(`/streams/${videoId}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves comments for a specific video.
|
|
||||||
*
|
|
||||||
* @param {string} videoId - The ID of the video for which to retrieve comments.
|
|
||||||
* @param {string} nextpage - The JSON encoded nextpage variable, to be sent as a query string (optional).
|
|
||||||
* @returns {Promise<Comments>} A Promise that resolves to the comments for the video.
|
|
||||||
*/
|
|
||||||
async comments(videoId: string, nextpage?: string): Promise<Comments> {
|
|
||||||
return nextpage
|
|
||||||
? await this._get(`/nextpage/comments/${videoId}?nextpage=${nextpage}`)
|
|
||||||
: await this._get(`/comments/${videoId}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves a channel by its ID.
|
|
||||||
*
|
|
||||||
* @param {string} id - The ID of the channel to retrieve.
|
|
||||||
* @param {string} nextpage - The JSON encoded nextpage variable, to be sent as a query string (optional).
|
|
||||||
* @return {Promise<Channel | NextPageChannel>} A Promise that resolves to the retrieved Channel object.
|
|
||||||
*/
|
|
||||||
async channel(
|
|
||||||
id: string,
|
|
||||||
nextpage?: string
|
|
||||||
): Promise<Channel | NextPageChannel> {
|
|
||||||
return nextpage
|
|
||||||
? await this._get(`/nextpage/channel/${id}?nextpage=${nextpage}`)
|
|
||||||
: await this._get(`/channel/${id}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves a channel by its name.
|
|
||||||
*
|
|
||||||
* @param {string} name - The name of the channel.
|
|
||||||
* @return {Promise<Channel>} A promise that resolves to the channel object.
|
|
||||||
*/
|
|
||||||
async channelByName(name: string): Promise<Channel> {
|
|
||||||
return await this._get(`/c/${name}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves a user by their name.
|
|
||||||
*
|
|
||||||
* @param {string} name - The name of the user.
|
|
||||||
* @return {Promise<Channel>} A Promise that resolves to the user's channel.
|
|
||||||
*/
|
|
||||||
async user(name: string): Promise<Channel> {
|
|
||||||
return await this._get(`/user/${name}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves a playlist based on its ID.
|
|
||||||
*
|
|
||||||
* @param {string} id - The ID of the playlist to retrieve.
|
|
||||||
* @param {string} nextpage - The token for the next page of results (optional).
|
|
||||||
* @return {Promise<Playlist | NextPagePlaylist>} A Promise that resolves to a Playlist or NextPagePlaylist object.
|
|
||||||
*/
|
|
||||||
async playlist(
|
|
||||||
id: string,
|
|
||||||
nextpage?: string
|
|
||||||
): Promise<Playlist | NextPagePlaylist> {
|
|
||||||
return nextpage
|
|
||||||
? await this._get(`/nextpage/playlists/${id}?nextpage=${nextpage}`)
|
|
||||||
: await this._get(`/playlists/${id}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves suggestions based on the given query.
|
|
||||||
*
|
|
||||||
* @param {string} query - The query string to search for suggestions.
|
|
||||||
* @return {Promise<string[]>} A promise that resolves to an array of string suggestions.
|
|
||||||
*/
|
|
||||||
async suggestions(query: string): Promise<string[]> {
|
|
||||||
return await this._get(`/suggestions?query=${query}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves sponsors based on the provided ID and category.
|
|
||||||
*
|
|
||||||
* @param {string} id - The ID of the video.
|
|
||||||
* @param {string[]} category - The category of sponsors you would like to skip. Example: ["sponsor"].
|
|
||||||
* @return {Promise<Sponsors>} A promise that resolves to the sponsors.
|
|
||||||
*/
|
|
||||||
async sponsors(
|
|
||||||
id: string,
|
|
||||||
category: string[] = ["sponsor"]
|
|
||||||
): Promise<Sponsors> {
|
|
||||||
return await this._get(
|
|
||||||
`/sponsors/${id}?category=${JSON.stringify(category)}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Searches videos for a given query with an optional filter.
|
|
||||||
*
|
|
||||||
* @param {string} query - The query to search for.
|
|
||||||
* @param {string} [filter="all"] - The optional filter to apply.
|
|
||||||
* @returns {Promise<Search>} - A Promise that resolves to the search results.
|
|
||||||
*/
|
|
||||||
async search(query: string, filter: string = "all"): Promise<Search> {
|
|
||||||
return await this._get(`/search?q=${query}&filter=${filter}`);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -102,6 +102,7 @@ export interface Channel {
|
||||||
description: string;
|
description: string;
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
|
tabs: TabInfo[];
|
||||||
nextpage: string;
|
nextpage: string;
|
||||||
relatedStreams: Video[];
|
relatedStreams: Video[];
|
||||||
subscriberCount: number;
|
subscriberCount: number;
|
||||||
|
@ -109,6 +110,18 @@ export interface Channel {
|
||||||
type?: string;
|
type?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface TabInfo {
|
||||||
|
name: string;
|
||||||
|
data: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Tab {
|
||||||
|
nextpage: string;
|
||||||
|
content: TabResp[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TabResp = Video | Channel | Playlist;
|
||||||
|
|
||||||
export interface NextPageChannel {
|
export interface NextPageChannel {
|
||||||
nextpage: string;
|
nextpage: string;
|
||||||
relatedStreams: Video[];
|
relatedStreams: Video[];
|
||||||
|
@ -124,6 +137,7 @@ export interface Playlist {
|
||||||
uploaderAvatar: string;
|
uploaderAvatar: string;
|
||||||
uploaderUrl: string;
|
uploaderUrl: string;
|
||||||
videos: number;
|
videos: number;
|
||||||
|
type?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NextPagePlaylist {
|
export interface NextPagePlaylist {
|
||||||
|
@ -147,7 +161,9 @@ export interface Segments {
|
||||||
|
|
||||||
export interface Search {
|
export interface Search {
|
||||||
nextpage: string;
|
nextpage: string;
|
||||||
items: (Video | Channel)[];
|
items: Item[];
|
||||||
corrected: boolean;
|
corrected: boolean;
|
||||||
suggestion: string | null;
|
suggestion: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type Item = Video | Channel;
|
Loading…
Add table
Reference in a new issue