archiving community contributions on YouTube: unpublished captions, title and description translations and caption credits
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

54 lines
1.7 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. class HTTP3Response:
  9. def __init__(self, input) -> None:
  10. headers, content = input
  11. self.content = content
  12. try:
  13. self.text = content.decode()
  14. except:
  15. print("Text decoding error")
  16. self.text = ""
  17. self.headers = {}
  18. for k, v in headers.items():
  19. self.headers[k.decode()] = v.decode()
  20. try:
  21. self.status_code = int(headers[b":status"])
  22. except:
  23. print("Status code not included as header, defaulting to 200")
  24. self.status_code = 200
  25. self.ok = self.status_code < 400
  26. async def main(address, headers={}):
  27. parsed = urlparse(address)
  28. configuration = QuicConfiguration(
  29. is_client=True, alpn_protocols=H3_ALPN
  30. )
  31. async with connect(parsed.netloc, port=443, configuration=configuration, create_protocol=HttpClient) as client:
  32. client = cast(HttpClient, client)
  33. events = await perform_http_request(client=client, url=address, headers=headers)
  34. return HTTP3Response(prepare_response(events))
  35. def get(url, headers={}, params={}):
  36. plist = []
  37. for item in params:
  38. #print(item)
  39. k, v = item
  40. plist.append(str(k)+"="+str(v))
  41. if plist:
  42. pstring = "?"+"&".join(plist)
  43. else:
  44. pstring = ""
  45. #print(url+pstring)
  46. loop = asyncio.new_event_loop()
  47. return loop.run_until_complete(main(url+pstring, headers=headers))