cookies.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  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
  17. from http.cookies import Morsel, SimpleCookie
  18. from aiohttp import CookieJar
  19. from yarl import URL
  20. from mautrix.types import Serializable, JSON
  21. from ..errors import IGCookieNotFoundError
  22. ig_url = URL("https://instagram.com")
  23. class Cookies(Serializable):
  24. jar: CookieJar
  25. def __init__(self) -> None:
  26. self.jar = CookieJar()
  27. def serialize(self) -> JSON:
  28. return {
  29. "version": "tough-cookie@4.0.0",
  30. "storeType": "MemoryCookieStore",
  31. "rejectPublicSuffixes": True,
  32. "cookies": [{
  33. **morsel,
  34. "key": key,
  35. "value": morsel.value,
  36. } for key, morsel in self.jar.filter_cookies(ig_url).items()],
  37. }
  38. @classmethod
  39. def deserialize(cls, raw: JSON) -> 'Cookies':
  40. cookie = SimpleCookie()
  41. for item in raw["cookies"]:
  42. key = item.pop("key")
  43. cookie[key] = item.pop("value")
  44. item.pop("hostOnly", None)
  45. item.pop("lastAccessed", None)
  46. item.pop("creation", None)
  47. try:
  48. # Morsel.update() is case-insensitive, but not dash-insensitive
  49. item["max-age"] = item.pop("maxAge")
  50. except KeyError:
  51. pass
  52. cookie[key].update(item)
  53. cookies = cls()
  54. cookies.jar.update_cookies(cookie, ig_url)
  55. return cookies
  56. @property
  57. def csrf_token(self) -> str:
  58. try:
  59. return self["csrftoken"]
  60. except IGCookieNotFoundError:
  61. return "missing"
  62. @property
  63. def user_id(self) -> str:
  64. return self["ds_user_id"]
  65. @property
  66. def username(self) -> str:
  67. return self["ds_username"]
  68. def get(self, key: str) -> Morsel:
  69. filtered = self.jar.filter_cookies(ig_url)
  70. return filtered.get(key)
  71. def get_value(self, key: str) -> Optional[str]:
  72. cookie = self.get(key)
  73. return cookie.value if cookie else None
  74. def __getitem__(self, key: str) -> str:
  75. cookie = self.get(key)
  76. if not cookie:
  77. raise IGCookieNotFoundError(key)
  78. return cookie.value