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.

88 lines
3.1 KiB

  1. # maunium-stickerpicker - A fast and simple Matrix sticker picker widget.
  2. # Copyright (C) 2020 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from typing import Optional, TYPE_CHECKING
  17. import json
  18. from aiohttp import ClientSession
  19. from yarl import URL
  20. access_token: Optional[str] = None
  21. homeserver_url: Optional[str] = None
  22. upload_url: Optional[URL] = None
  23. if TYPE_CHECKING:
  24. from typing import TypedDict
  25. class MediaInfo(TypedDict):
  26. w: int
  27. h: int
  28. size: int
  29. mimetype: str
  30. thumbnail_url: Optional[str]
  31. thumbnail_info: Optional['MediaInfo']
  32. class StickerInfo(TypedDict, total=False):
  33. body: str
  34. url: str
  35. info: MediaInfo
  36. id: str
  37. msgtype: str
  38. else:
  39. MediaInfo = None
  40. StickerInfo = None
  41. async def load_config(path: str) -> None:
  42. global access_token, homeserver_url, upload_url
  43. try:
  44. with open(path) as config_file:
  45. config = json.load(config_file)
  46. homeserver_url = config["homeserver"]
  47. access_token = config["access_token"]
  48. except FileNotFoundError:
  49. print("Matrix config file not found. Please enter your homeserver and access token.")
  50. homeserver_url = input("Homeserver URL: ")
  51. access_token = input("Access token: ")
  52. whoami_url = URL(homeserver_url) / "_matrix" / "client" / "r0" / "account" / "whoami"
  53. user_id = await whoami(whoami_url, access_token)
  54. with open(path, "w") as config_file:
  55. json.dump({
  56. "homeserver": homeserver_url,
  57. "user_id": user_id,
  58. "access_token": access_token
  59. }, config_file)
  60. print(f"Wrote config to {path}")
  61. upload_url = URL(homeserver_url) / "_matrix" / "media" / "r0" / "upload"
  62. async def whoami(url: URL, access_token: str) -> str:
  63. headers = {"Authorization": f"Bearer {access_token}"}
  64. async with ClientSession() as sess, sess.get(url, headers=headers) as resp:
  65. resp.raise_for_status()
  66. user_id = (await resp.json())["user_id"]
  67. print(f"Access token validated (user ID: {user_id})")
  68. return user_id
  69. async def upload(data: bytes, mimetype: str, filename: str) -> str:
  70. url = upload_url.with_query({"filename": filename})
  71. headers = {"Content-Type": mimetype, "Authorization": f"Bearer {access_token}"}
  72. async with ClientSession() as sess, sess.post(url, data=data, headers=headers) as resp:
  73. return (await resp.json())["content_uri"]