You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.5 KiB

  1. # Copyright (c) 2020 Tulir Asokan
  2. #
  3. # This Source Code Form is subject to the terms of the Mozilla Public
  4. # License, v. 2.0. If a copy of the MPL was not distributed with this
  5. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. from typing import Optional, TYPE_CHECKING
  7. import json
  8. from aiohttp import ClientSession
  9. from yarl import URL
  10. access_token: Optional[str] = None
  11. homeserver_url: Optional[str] = None
  12. upload_url: Optional[URL] = None
  13. if TYPE_CHECKING:
  14. from typing import TypedDict
  15. class MediaInfo(TypedDict):
  16. w: int
  17. h: int
  18. size: int
  19. mimetype: str
  20. thumbnail_url: Optional[str]
  21. thumbnail_info: Optional['MediaInfo']
  22. class StickerInfo(TypedDict, total=False):
  23. body: str
  24. url: str
  25. info: MediaInfo
  26. id: str
  27. else:
  28. MediaInfo = None
  29. StickerInfo = None
  30. async def load_config(path: str) -> None:
  31. global access_token, homeserver_url, upload_url
  32. try:
  33. with open(path) as config_file:
  34. config = json.load(config_file)
  35. homeserver_url = config["homeserver"]
  36. access_token = config["access_token"]
  37. except FileNotFoundError:
  38. print("Matrix config file not found. Please enter your homeserver and access token.")
  39. homeserver_url = input("Homeserver URL: ")
  40. access_token = input("Access token: ")
  41. whoami_url = URL(homeserver_url) / "_matrix" / "client" / "r0" / "account" / "whoami"
  42. user_id = await whoami(whoami_url, access_token)
  43. with open(path, "w") as config_file:
  44. json.dump({
  45. "homeserver": homeserver_url,
  46. "user_id": user_id,
  47. "access_token": access_token
  48. }, config_file)
  49. print(f"Wrote config to {path}")
  50. upload_url = URL(homeserver_url) / "_matrix" / "media" / "r0" / "upload"
  51. async def whoami(url: URL, access_token: str) -> str:
  52. headers = {"Authorization": f"Bearer {access_token}"}
  53. async with ClientSession() as sess, sess.get(url, headers=headers) as resp:
  54. resp.raise_for_status()
  55. user_id = (await resp.json())["user_id"]
  56. print(f"Access token validated (user ID: {user_id})")
  57. return user_id
  58. async def upload(data: bytes, mimetype: str, filename: str) -> str:
  59. url = upload_url.with_query({"filename": filename})
  60. headers = {"Content-Type": mimetype, "Authorization": f"Bearer {access_token}"}
  61. async with ClientSession() as sess, sess.post(url, data=data, headers=headers) as resp:
  62. return (await resp.json())["content_uri"]