identifying myself
This commit is contained in:
		
							parent
							
								
									e693e57cae
								
							
						
					
					
						commit
						39b5c2640d
					
				| 
						 | 
					@ -2,13 +2,21 @@ const fetch = require('node-fetch')
 | 
				
			||||||
const maxRetries = 5
 | 
					const maxRetries = 5
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function getInstance() {
 | 
					async function getInstance() {
 | 
				
			||||||
    const instances = await (await fetch('https://api.invidious.io/instances.json?pretty=1')).json()
 | 
					    const instances = await (await fetch('https://api.invidious.io/instances.json?pretty=1', {
 | 
				
			||||||
 | 
					        headers: {
 | 
				
			||||||
 | 
					            'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)'
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    })).json()
 | 
				
			||||||
    const sorted = instances.filter(o => o[1].type == 'https' && o[0] != 'invidious.io.lol' && o[0] != 'invidious.0011.lt')
 | 
					    const sorted = instances.filter(o => o[1].type == 'https' && o[0] != 'invidious.io.lol' && o[0] != 'invidious.0011.lt')
 | 
				
			||||||
    return `https://${sorted[Math.floor(Math.random() * sorted.length)][0]}`
 | 
					    return `https://${sorted[Math.floor(Math.random() * sorted.length)][0]}`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function getPipedInstance() {
 | 
					async function getPipedInstance() {
 | 
				
			||||||
    const instances = await (await fetch('https://piped-instances.kavin.rocks/')).json()
 | 
					    const instances = await (await fetch('https://piped-instances.kavin.rocks/', {
 | 
				
			||||||
 | 
					        headers: {
 | 
				
			||||||
 | 
					            'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)'
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    })).json()
 | 
				
			||||||
    return (instances[Math.floor(Math.random() * instances.length)]).api_url
 | 
					    return (instances[Math.floor(Math.random() * instances.length)]).api_url
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -16,7 +24,11 @@ async function getVideoMetadata(id) {
 | 
				
			||||||
    for (let retries = 0; retries < maxRetries; retries++) {
 | 
					    for (let retries = 0; retries < maxRetries; retries++) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            const instance = await getInstance()
 | 
					            const instance = await getInstance()
 | 
				
			||||||
            const response = await fetch(`${instance}/api/v1/videos/${id}?fields=videoId,title,descriptionHtml,videoThumbnails,published,authorId,error&pretty=1`)
 | 
					            const response = await fetch(`${instance}/api/v1/videos/${id}?fields=videoId,title,descriptionHtml,videoThumbnails,published,authorId,error&pretty=1`, {
 | 
				
			||||||
 | 
					                headers: {
 | 
				
			||||||
 | 
					                    'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)'
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            })
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            if (response.ok) {
 | 
					            if (response.ok) {
 | 
				
			||||||
                const json = await response.json()
 | 
					                const json = await response.json()
 | 
				
			||||||
| 
						 | 
					@ -36,7 +48,11 @@ async function getChannel(id) {
 | 
				
			||||||
    for (let retries = 0; retries < maxRetries; retries++) {
 | 
					    for (let retries = 0; retries < maxRetries; retries++) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            const instance = await getInstance()
 | 
					            const instance = await getInstance()
 | 
				
			||||||
            const response = await fetch(`${instance}/api/v1/channels/${id}?pretty=1`)
 | 
					            const response = await fetch(`${instance}/api/v1/channels/${id}?pretty=1`, {
 | 
				
			||||||
 | 
					                headers: {
 | 
				
			||||||
 | 
					                    'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)'
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (response.ok) {
 | 
					            if (response.ok) {
 | 
				
			||||||
                const json = await response.json()
 | 
					                const json = await response.json()
 | 
				
			||||||
| 
						 | 
					@ -57,13 +73,21 @@ async function getChannelVideos(id) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            const videos = []
 | 
					            const videos = []
 | 
				
			||||||
            const instance = await getPipedInstance()
 | 
					            const instance = await getPipedInstance()
 | 
				
			||||||
            const json = await (await fetch(`${instance}/channel/${id}`)).json()
 | 
					            const json = await (await fetch(`${instance}/channel/${id}`, {
 | 
				
			||||||
 | 
					                headers: {
 | 
				
			||||||
 | 
					                    'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)'
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            })).json()
 | 
				
			||||||
            videos.push(...json.relatedStreams)
 | 
					            videos.push(...json.relatedStreams)
 | 
				
			||||||
            if (json.nextpage) await getNextPage(json.nextpage)
 | 
					            if (json.nextpage) await getNextPage(json.nextpage)
 | 
				
			||||||
            else resolve(videos)
 | 
					            else resolve(videos)
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            async function getNextPage(payload) {
 | 
					            async function getNextPage(payload) {
 | 
				
			||||||
                const page = await (await fetch(`${instance}/nextpage/channel/${id}?nextpage=${encodeURIComponent(payload)}`)).json()
 | 
					                const page = await (await fetch(`${instance}/nextpage/channel/${id}?nextpage=${encodeURIComponent(payload)}`, {
 | 
				
			||||||
 | 
					                    headers: {
 | 
				
			||||||
 | 
					                        'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)'
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                })).json()
 | 
				
			||||||
                videos.push(...page.relatedStreams)
 | 
					                videos.push(...page.relatedStreams)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (videos.length >= 60) return resolve(videos)
 | 
					                if (videos.length >= 60) return resolve(videos)
 | 
				
			||||||
| 
						 | 
					@ -79,7 +103,11 @@ async function getChannelVideos(id) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function getPlaylistVideos(id) {
 | 
					async function getPlaylistVideos(id) {
 | 
				
			||||||
    const instance = await getPipedInstance()
 | 
					    const instance = await getPipedInstance()
 | 
				
			||||||
    const json = await (await fetch(`${instance}/playlists/${id}`)).json()
 | 
					    const json = await (await fetch(`${instance}/playlists/${id}`, {
 | 
				
			||||||
 | 
					        headers: {
 | 
				
			||||||
 | 
					            'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)'
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    })).json()
 | 
				
			||||||
    return json
 | 
					    return json
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue