archiving community contributions on YouTube: unpublished captions, title and description translations and caption credits
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

73 řádky
2.4 KiB

  1. import asyncio
  2. from typing import cast
  3. from urllib.parse import urlparse
  4. from aioquic.h3.connection import H3_ALPN
  5. from aioquic.asyncio.client import connect
  6. from aioquic.quic.configuration import QuicConfiguration
  7. from http3_base import HttpClient, prepare_response, perform_http_request
  8. from urllib.parse import urlparse
  9. class HTTP3Response:
  10. def __init__(self, input) -> None:
  11. headers, content, url, redirect = input
  12. self.content = content
  13. try:
  14. self.text = content.decode()
  15. except:
  16. print("Text decoding error")
  17. self.text = ""
  18. self.headers = {}
  19. for k, v in headers.items():
  20. self.headers[k.decode()] = v.decode()
  21. try:
  22. self.status_code = int(headers[b":status"])
  23. self.url = url
  24. except:
  25. print("Status code not included as header, defaulting to 200")
  26. self.status_code = 200
  27. self.ok = self.status_code < 400
  28. async def main(address, headers={}):
  29. parsed = urlparse(address)
  30. configuration = QuicConfiguration(
  31. is_client=True, alpn_protocols=H3_ALPN
  32. )
  33. async with connect(parsed.netloc, port=443, configuration=configuration, create_protocol=HttpClient) as client:
  34. client = cast(HttpClient, client)
  35. events = await perform_http_request(client=client, url=address, headers=headers)
  36. return prepare_response(events)
  37. def get(url, headers={}, params={}):
  38. plist = []
  39. for item in params:
  40. #print(item)
  41. k, v = item
  42. plist.append(str(k)+"="+str(v))
  43. if plist:
  44. pstring = "?"+"&".join(plist)
  45. else:
  46. pstring = ""
  47. #print(url+pstring)
  48. redirect = False
  49. url = url+pstring
  50. while True:
  51. #print(url)
  52. loop = asyncio.new_event_loop()
  53. oheaders, ocontent = loop.run_until_complete(main(url, headers=headers))
  54. statuscode = int(oheaders[b":status"])
  55. if statuscode >= 300 and statuscode < 400 and b"location" in oheaders.keys():
  56. #print("Redirection")
  57. redirect = True
  58. origurl = url
  59. parsedorig = urlparse(origurl)
  60. url = oheaders[b"location"].decode()
  61. parsednew = urlparse(url)
  62. if not parsednew.scheme and not parsednew.netloc:
  63. url = parsedorig.scheme + "://" + parsedorig.netloc + url
  64. else:
  65. break
  66. return HTTP3Response((oheaders, ocontent, url+pstring, redirect))