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.

87 lines
3.0 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. else:
  38. MediaInfo = None
  39. StickerInfo = None
  40. async def load_config(path: str) -> None:
  41. global access_token, homeserver_url, upload_url
  42. try:
  43. with open(path) as config_file:
  44. config = json.load(config_file)
  45. homeserver_url = config["homeserver"]
  46. access_token = config["access_token"]
  47. except FileNotFoundError:
  48. print("Matrix config file not found. Please enter your homeserver and access token.")
  49. homeserver_url = input("Homeserver URL: ")
  50. access_token = input("Access token: ")
  51. whoami_url = URL(homeserver_url) / "_matrix" / "client" / "r0" / "account" / "whoami"
  52. user_id = await whoami(whoami_url, access_token)
  53. with open(path, "w") as config_file:
  54. json.dump({
  55. "homeserver": homeserver_url,
  56. "user_id": user_id,
  57. "access_token": access_token
  58. }, config_file)
  59. print(f"Wrote config to {path}")
  60. upload_url = URL(homeserver_url) / "_matrix" / "media" / "r0" / "upload"
  61. async def whoami(url: URL, access_token: str) -> str:
  62. headers = {"Authorization": f"Bearer {access_token}"}
  63. async with ClientSession() as sess, sess.get(url, headers=headers) as resp:
  64. resp.raise_for_status()
  65. user_id = (await resp.json())["user_id"]
  66. print(f"Access token validated (user ID: {user_id})")
  67. return user_id
  68. async def upload(data: bytes, mimetype: str, filename: str) -> str:
  69. url = upload_url.with_query({"filename": filename})
  70. headers = {"Content-Type": mimetype, "Authorization": f"Bearer {access_token}"}
  71. async with ClientSession() as sess, sess.post(url, data=data, headers=headers) as resp:
  72. return (await resp.json())["content_uri"]