Files
ytdl-patched-mirror/devscripts/make_peertube_instance_list.py

86 lines
3.8 KiB
Python

# coding: utf-8
from __future__ import unicode_literals, print_function
import sys
import re
sys.path[:0] = ['.']
from yt_dlp.utils import ExtractorError
from yt_dlp.extractor.common import InfoExtractor
from test.helper import FakeYDL
class TestIE(InfoExtractor):
pass
ie = TestIE(FakeYDL({'verbose': False}))
script_id = 'peertube'
results = set()
def sanitize_hostname(hostname):
# trim trailing slashes
hostname = re.sub(r'[/\\]+$', '', hostname)
# trim port number
hostname = re.sub(r':\d+$', '', hostname)
return hostname
begin, page_size = 0, 10
while True:
url = 'https://instances.joinpeertube.org/api/v1/instances?start=%d&count=%d&sort=-createdAt' % (begin, page_size)
data = ie._download_json(
url, script_id, note=f'Paging {begin}, len(results)={len(results)}')
for instance in data['data']:
results.add(sanitize_hostname(instance['host']))
begin += page_size
if not data['data']:
break
while True:
try:
url = 'https://the-federation.info/graphql?query=query%20Platform(%24name%3A%20String!)%20%7B%0A%20%20platforms(name%3A%20%24name)%20%7B%0A%20%20%20%20name%0A%20%20%20%20code%0A%20%20%20%20displayName%0A%20%20%20%20description%0A%20%20%20%20tagline%0A%20%20%20%20website%0A%20%20%20%20icon%0A%20%20%20%20__typename%0A%20%20%7D%0A%20%20nodes(platform%3A%20%24name)%20%7B%0A%20%20%20%20id%0A%20%20%20%20name%0A%20%20%20%20version%0A%20%20%20%20openSignups%0A%20%20%20%20host%0A%20%20%20%20platform%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20icon%0A%20%20%20%20%20%20__typename%0A%20%20%20%20%7D%0A%20%20%20%20countryCode%0A%20%20%20%20countryFlag%0A%20%20%20%20countryName%0A%20%20%20%20services%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20__typename%0A%20%20%20%20%7D%0A%20%20%20%20__typename%0A%20%20%7D%0A%20%20statsGlobalToday(platform%3A%20%24name)%20%7B%0A%20%20%20%20usersTotal%0A%20%20%20%20usersHalfYear%0A%20%20%20%20usersMonthly%0A%20%20%20%20localPosts%0A%20%20%20%20localComments%0A%20%20%20%20__typename%0A%20%20%7D%0A%20%20statsNodes(platform%3A%20%24name)%20%7B%0A%20%20%20%20node%20%7B%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20__typename%0A%20%20%20%20%7D%0A%20%20%20%20usersTotal%0A%20%20%20%20usersHalfYear%0A%20%20%20%20usersMonthly%0A%20%20%20%20localPosts%0A%20%20%20%20localComments%0A%20%20%20%20__typename%0A%20%20%7D%0A%7D%0A&operationName=Platform&variables=%7B%22name%22%3A%22peertube%22%7D'
data = ie._download_json(
url, script_id, note=f'Scraping https://the-federation.info/peertube, len(results)={len(results)}',
headers={
'content-type': 'application/json, application/graphql',
'accept': 'application/json, application/graphql',
})
for instance in data['data']['nodes']:
results.add(sanitize_hostname(instance['host']))
break
except BaseException:
continue
if not results:
raise ExtractorError('no instances found')
results = {x.encode('idna').decode('utf8') for x in results}
ie.to_screen(f'{script_id}: converted domain names to punycode, len(results)={len(results)}')
results = {x for x in results if '.' in x}
ie.to_screen(f'{script_id}: excluded domain names without dot, len(results)={len(results)}')
results = {x for x in results if not (x.endswith('.ngrok.io') or x.endswith('.localhost.run') or x.endswith('.serveo.net'))}
ie.to_screen(f'{script_id}: excluded temporary domain names, len(results)={len(results)}')
lf = '\n'
pycode = f'''# coding: utf-8
# AUTOMATICALLY GENERATED FILE. DO NOT EDIT.
# Generated by ./devscripts/make_peertube_instance_list.py
from __future__ import unicode_literals
instances = {{
# list of instances here
{lf.join(f' "{r}",' for r in sorted(results))}
}}
__all__ = ['instances']
'''
with open('./yt_dlp/extractor/peertube/instances.py', 'w') as w:
w.write(pycode)