ytdl-patched/devscripts/make_peertube_instance_list.py

76 lines
4.0 KiB
Python

# coding: utf-8
from __future__ import unicode_literals
import sys
import re
sys.path[:0] = ['.', 'devscripts']
from scraper_helper import ie, sanitize_hostname, traverse_sanitize
from yt_dlp.utils import ExtractorError
script_id = 'peertube'
results = set()
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 https://instances.joinpeertube.org {begin}, len(results)={len(results)}')
results.update(traverse_sanitize(data, ('data', ..., 'host')))
begin += page_size
if not data['data']:
break
if 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',
})
results.update(traverse_sanitize(data, ('data', 'nodes', ..., 'host')))
except BaseException as ex:
ie.report_warning(ex)
if True:
try:
url = 'https://peertube.fediverse.observer/tabledata.php?software=peertube'
data = ie._download_webpage(
url, script_id, note=f'Scraping https://peertube.fediverse.observer, len(results)={len(results)}')
for instance in re.finditer(r'href="/go\.php\?domain=([a-z0-9\.-]+)">\1</a>', data):
results.add(sanitize_hostname(instance.group(1)))
except BaseException as ex:
ie.report_warning(ex)
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)