majuna/app/lists/bc2.py

66 lines
2.1 KiB
Python
Raw Normal View History

import builtins
2022-04-27 13:30:49 +01:00
from datetime import datetime
from typing import List, Dict, Union
2022-03-10 14:26:22 +00:00
2022-04-27 13:30:49 +01:00
from pydantic import BaseModel, Field
2022-03-10 14:26:22 +00:00
2022-05-16 13:29:48 +01:00
from app.models.mirrors import Origin, Proxy, Mirror
2022-04-27 13:30:49 +01:00
class BC2Alternative(BaseModel):
proto: str
type: str
created_at: datetime
updated_at: datetime
url: str
class BC2Site(BaseModel):
main_domain: str = Field(
description="The main domain name of the website, excluding \"www.\" if present.",
2022-05-16 13:29:48 +01:00
examples=["bbc.co.uk", "bbc.com", "guardianproject.info"])
2022-04-27 13:30:49 +01:00
available_alternatives: List[BC2Alternative]
class BypassCensorship2(BaseModel):
2022-05-16 13:29:48 +01:00
version: str = Field(description="Version number of the Bypass Censorship Extension schema in use", )
2022-04-27 13:30:49 +01:00
sites: List[BC2Site]
class Config:
title = "Bypass Censorship Version 2"
2022-03-10 14:26:22 +00:00
2022-04-27 13:35:04 +01:00
2022-05-16 13:29:48 +01:00
def mirror_alternative(mirror: Mirror):
2022-03-10 14:26:22 +00:00
return {
2022-05-16 13:29:48 +01:00
"proto": "tor" if ".onion" in mirror.url else "https",
"type": "eotk" if ".onion" in mirror.url else "mirror",
"created_at": str(mirror.added),
"updated_at": str(mirror.updated),
"url": mirror.url
2022-03-10 14:26:22 +00:00
}
2022-05-16 13:29:48 +01:00
def proxy_alternative(proxy: Proxy):
return {
"proto": "https",
"type": "mirror",
"created_at": str(proxy.added),
"updated_at": str(proxy.updated),
"url": proxy.url
}
def mirror_sites(provider: str = "cloudfront") -> Dict[str,
Union[str, List[Dict[str, Union[str, List[Dict[str, str]]]]]]]:
return {"version": "2.0", "sites": [{
"main_domain": x.description[len("proxy:"):].replace("www.", "") if x.description.startswith(
"proxy:") else x.domain_name.replace("www.", ""),
"available_alternatives": [mirror_alternative(a) for a in x.mirrors if not a.deprecated and not a.destroyed] + [
proxy_alternative(a) for a in x.proxies if
a.url is not None and not a.deprecated and not a.destroyed and a.provider == provider]} for x in
Origin.query.order_by(Origin.domain_name).all() if x.destroyed is None]}
if getattr(builtins, "__sphinx_build__", False):
schema = BypassCensorship2.schema_json()