forked from FreeTubeApp/FreeTube
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvidious.js
More file actions
112 lines (100 loc) · 3.08 KB
/
invidious.js
File metadata and controls
112 lines (100 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import store from '../../store/index'
import { stripHTML, toLocalePublicationString } from '../utils'
import autolinker from 'autolinker'
function getCurrentInstance() {
return store.getters.getCurrentInvidiousInstance
}
export function invidiousAPICall({ resource, id = '', params = {} }) {
return new Promise((resolve, reject) => {
const requestUrl = getCurrentInstance() + '/api/v1/' + resource + '/' + id + '?' + new URLSearchParams(params).toString()
fetch(requestUrl)
.then((response) => response.json())
.then((json) => {
resolve(json)
})
.catch((error) => {
console.error('Invidious API error', requestUrl, error)
reject(error)
})
})
}
export async function invidiousGetChannelInfo(channelId) {
return new Promise((resolve, reject) => {
invidiousAPICall({
resource: 'channels',
id: channelId,
}).then(response => {
resolve(response)
}).catch(xhr => {
console.error(xhr)
reject(xhr)
})
})
}
export async function invidiousGetPlaylistInfo(playlistId) {
const payload = {
resource: 'playlists',
id: playlistId
}
return new Promise((resolve, reject) => {
invidiousAPICall(payload).then((response) => {
resolve(response)
}).catch((xhr) => {
console.error(xhr)
reject(xhr)
})
})
}
export async function invidiousGetVideoInformation(videoId) {
return new Promise((resolve, reject) => {
invidiousAPICall({ resource: 'videos', id: videoId }).then((response) => {
resolve(response)
}).catch((xhr) => {
console.error(xhr)
reject(xhr)
})
})
}
export async function invidiousGetComments({ id, nextPageToken = '', sortNewest = true }) {
const payload = {
resource: 'comments',
id: id,
params: {
continuation: nextPageToken ?? '',
sort_by: sortNewest ? 'new' : 'top'
}
}
const response = await invidiousAPICall(payload)
const commentData = parseInvidiousCommentData(response)
return { response, commentData }
}
export async function invidiousGetCommentReplies({ id, replyContinuation }) {
const payload = {
resource: 'comments',
id: id,
params: {
continuation: replyContinuation
}
}
const response = await invidiousAPICall(payload)
return parseInvidiousCommentData(response)
}
function parseInvidiousCommentData(comments) {
return comments.map((comment) => {
comment.showReplies = false
comment.authorLink = comment.authorId
comment.authorThumb = comment.authorThumbnails[1].url.replace('https://yt3.ggpht.com', `${getCurrentInstance()}/ggpht/`)
comment.likes = comment.likeCount
comment.text = autolinker.link(stripHTML(comment.content))
comment.dataType = 'invidious'
comment.isOwner = comment.authorIsChannelOwner
comment.numReplies = comment.replies?.replyCount ?? 0
comment.replyContinuation = comment.replies?.continuation ?? ''
comment.isHearted = comment.creatorHeart !== undefined
comment.replies = []
comment.time = toLocalePublicationString({
publishText: comment.publishedText
})
return comment
})
}