device.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 uuid import UUID
  18. import pkgutil
  19. import random
  20. import string
  21. import time
  22. import json
  23. from mautrix.types import SerializableAttrs, field, dataclass
  24. builds = json.loads(pkgutil.get_data("mauigpapi.state", "samples/builds.json"))
  25. descriptors = json.loads(pkgutil.get_data("mauigpapi.state", "samples/devices.json"))
  26. @dataclass
  27. class AndroidDevice(SerializableAttrs['AndroidDevice']):
  28. id: Optional[str] = None
  29. descriptor: Optional[str] = None
  30. uuid: Optional[str] = None
  31. phone_id: Optional[str] = field(default=None, json="phoneId")
  32. # Google Play advertising ID
  33. adid: Optional[str] = None
  34. build: Optional[str] = None
  35. language: str = "en_US"
  36. radio_type: str = "wifi-none"
  37. connection_type: str = "WIFI"
  38. timezone_offset: str = str(-time.timezone)
  39. is_layout_rtl: bool = False
  40. @property
  41. def battery_level(self) -> int:
  42. rand = random.Random(self.id)
  43. percent_time = rand.randint(200, 600)
  44. return 100 - round(time.time() / percent_time) % 100
  45. @property
  46. def is_charging(self) -> bool:
  47. rand = random.Random(f"{self.id}{round(time.time() / 10800)}")
  48. return rand.choice([True, False])
  49. @property
  50. def payload(self) -> dict:
  51. device_parts = self.descriptor.split(";")
  52. android_version, android_release, *_ = device_parts[0].split("/")
  53. manufacturer, *_ = device_parts[3].split("/")
  54. model = device_parts[4]
  55. return {
  56. "android_version": android_version,
  57. "android_release": android_release,
  58. "manufacturer": manufacturer,
  59. "model": model,
  60. }
  61. def generate(self, seed: str) -> None:
  62. rand = random.Random(seed)
  63. self.id = f"android-{''.join(rand.choices(string.hexdigits, k=16))}"
  64. self.descriptor = rand.choice(descriptors)
  65. self.uuid = str(UUID(int=rand.getrandbits(128), version=4))
  66. self.phone_id = str(UUID(int=rand.getrandbits(128), version=4))
  67. self.adid = str(UUID(int=rand.getrandbits(128), version=4))
  68. self.build = rand.choice(builds)