__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import string
  2. from datetime import datetime
  3. from .. import BaseProvider, ElementsType
  4. class Provider(BaseProvider):
  5. """Implement default user agent provider for Faker."""
  6. user_agents: ElementsType = (
  7. "chrome",
  8. "firefox",
  9. "internet_explorer",
  10. "opera",
  11. "safari",
  12. )
  13. windows_platform_tokens: ElementsType = (
  14. "Windows 95",
  15. "Windows 98",
  16. "Windows 98; Win 9x 4.90",
  17. "Windows CE",
  18. "Windows NT 4.0",
  19. "Windows NT 5.0",
  20. "Windows NT 5.01",
  21. "Windows NT 5.1",
  22. "Windows NT 5.2",
  23. "Windows NT 6.0",
  24. "Windows NT 6.1",
  25. "Windows NT 6.2",
  26. "Windows NT 10.0",
  27. )
  28. linux_processors: ElementsType = ("i686", "x86_64")
  29. mac_processors: ElementsType = ("Intel", "PPC", "U; Intel", "U; PPC")
  30. android_versions: ElementsType = (
  31. "1.0",
  32. "1.1",
  33. "1.5",
  34. "1.6",
  35. "2.0",
  36. "2.0.1",
  37. "2.1",
  38. "2.2",
  39. "2.2.1",
  40. "2.2.2",
  41. "2.2.3",
  42. "2.3",
  43. "2.3.1",
  44. "2.3.2",
  45. "2.3.3",
  46. "2.3.4",
  47. "2.3.5",
  48. "2.3.6",
  49. "2.3.7",
  50. "3.0",
  51. "3.1",
  52. "3.2",
  53. "3.2.1",
  54. "3.2.2",
  55. "3.2.3",
  56. "3.2.4",
  57. "3.2.5",
  58. "3.2.6",
  59. "4.0",
  60. "4.0.1",
  61. "4.0.2",
  62. "4.0.3",
  63. "4.0.4",
  64. "4.1",
  65. "4.1.1",
  66. "4.1.2",
  67. "4.2",
  68. "4.2.1",
  69. "4.2.2",
  70. "4.3",
  71. "4.3.1",
  72. "4.4",
  73. "4.4.1",
  74. "4.4.2",
  75. "4.4.3",
  76. "4.4.4",
  77. "5.0",
  78. "5.0.1",
  79. "5.0.2",
  80. "5.1",
  81. "5.1.1",
  82. "6.0",
  83. "6.0.1",
  84. "7.0",
  85. "7.1",
  86. "7.1.1",
  87. "7.1.2",
  88. "8.0.0",
  89. "8.1.0",
  90. "9",
  91. "10",
  92. "11",
  93. )
  94. apple_devices: ElementsType = ("iPhone", "iPad")
  95. ios_versions: ElementsType = (
  96. "3.1.3",
  97. "4.2.1",
  98. "5.1.1",
  99. "6.1.6",
  100. "7.1.2",
  101. "9.3.5",
  102. "9.3.6",
  103. "10.3.3",
  104. "10.3.4",
  105. "12.4.8",
  106. "14.2",
  107. "14.2.1",
  108. )
  109. def mac_processor(self) -> str:
  110. """Generate a MacOS processor token used in user agent strings."""
  111. return self.random_element(self.mac_processors)
  112. def linux_processor(self) -> str:
  113. """Generate a Linux processor token used in user agent strings."""
  114. return self.random_element(self.linux_processors)
  115. def user_agent(self) -> str:
  116. """Generate a random web browser user agent string."""
  117. name: str = self.random_element(self.user_agents)
  118. return getattr(self, name)()
  119. def chrome(
  120. self,
  121. version_from: int = 13,
  122. version_to: int = 63,
  123. build_from: int = 800,
  124. build_to: int = 899,
  125. ) -> str:
  126. """Generate a Chrome web browser user agent string."""
  127. saf: str = f"{self.generator.random.randint(531, 536)}.{self.generator.random.randint(0, 2)}"
  128. bld: str = self.lexify(self.numerify("##?###"), string.ascii_uppercase)
  129. tmplt: str = "({0}) AppleWebKit/{1} (KHTML, like Gecko)" " Chrome/{2}.0.{3}.0 Safari/{4}"
  130. tmplt_ios: str = "({0}) AppleWebKit/{1} (KHTML, like Gecko)" " CriOS/{2}.0.{3}.0 Mobile/{4} Safari/{1}"
  131. platforms: ElementsType = (
  132. tmplt.format(
  133. self.linux_platform_token(),
  134. saf,
  135. self.generator.random.randint(version_from, version_to),
  136. self.generator.random.randint(build_from, build_to),
  137. saf,
  138. ),
  139. tmplt.format(
  140. self.windows_platform_token(),
  141. saf,
  142. self.generator.random.randint(version_from, version_to),
  143. self.generator.random.randint(build_from, build_to),
  144. saf,
  145. ),
  146. tmplt.format(
  147. self.mac_platform_token(),
  148. saf,
  149. self.generator.random.randint(version_from, version_to),
  150. self.generator.random.randint(build_from, build_to),
  151. saf,
  152. ),
  153. tmplt.format(
  154. "Linux; {}".format(self.android_platform_token()),
  155. saf,
  156. self.generator.random.randint(version_from, version_to),
  157. self.generator.random.randint(build_from, build_to),
  158. saf,
  159. ),
  160. tmplt_ios.format(
  161. self.ios_platform_token(),
  162. saf,
  163. self.generator.random.randint(version_from, version_to),
  164. self.generator.random.randint(build_from, build_to),
  165. bld,
  166. ),
  167. )
  168. return "Mozilla/5.0 " + self.random_element(platforms)
  169. def firefox(self) -> str:
  170. """Generate a Mozilla Firefox web browser user agent string."""
  171. ver: ElementsType = (
  172. (
  173. f"Gecko/{self.generator.date_time_between(datetime(2011, 1, 1))} "
  174. f"Firefox/{self.generator.random.randint(4, 15)}.0"
  175. ),
  176. (
  177. f"Gecko/{self.generator.date_time_between(datetime(2010, 1, 1))} "
  178. f"Firefox/3.6.{self.generator.random.randint(1, 20)}"
  179. ),
  180. f"Gecko/{self.generator.date_time_between(datetime(2010, 1, 1))} Firefox/3.8",
  181. )
  182. tmplt_win: str = "({0}; {1}; rv:1.9.{2}.20) {3}"
  183. tmplt_lin: str = "({0}; rv:1.9.{1}.20) {2}"
  184. tmplt_mac: str = "({0}; rv:1.9.{1}.20) {2}"
  185. tmplt_and: str = "({0}; Mobile; rv:{1}.0) Gecko/{1}.0 Firefox/{1}.0"
  186. tmplt_ios: str = "({0}) AppleWebKit/{1} (KHTML, like Gecko) FxiOS/{2}.{3}.0 Mobile/{4} Safari/{1}"
  187. saf: str = "{}.{}".format(self.generator.random.randint(531, 536), self.generator.random.randint(0, 2))
  188. bld: str = self.lexify(self.numerify("##?###"), string.ascii_uppercase)
  189. bld2: str = self.lexify(self.numerify("#?####"), string.ascii_lowercase)
  190. platforms: ElementsType = (
  191. tmplt_win.format(
  192. self.windows_platform_token(),
  193. self.generator.locale().replace("_", "-"),
  194. self.generator.random.randint(0, 2),
  195. self.generator.random.choice(ver),
  196. ),
  197. tmplt_lin.format(
  198. self.linux_platform_token(),
  199. self.generator.random.randint(5, 7),
  200. self.generator.random.choice(ver),
  201. ),
  202. tmplt_mac.format(
  203. self.mac_platform_token(),
  204. self.generator.random.randint(2, 6),
  205. self.generator.random.choice(ver),
  206. ),
  207. tmplt_and.format(self.android_platform_token(), self.generator.random.randint(5, 68)),
  208. tmplt_ios.format(
  209. self.ios_platform_token(),
  210. saf,
  211. self.generator.random.randint(9, 18),
  212. bld2,
  213. bld,
  214. ),
  215. )
  216. return "Mozilla/5.0 " + self.random_element(platforms)
  217. def safari(self) -> str:
  218. """Generate a Safari web browser user agent string."""
  219. saf: str = (
  220. f"{self.generator.random.randint(531, 535)}."
  221. f"{self.generator.random.randint(1, 50)}."
  222. f"{self.generator.random.randint(1, 7)}"
  223. )
  224. ver: str = (
  225. f"{self.generator.random.randint(4, 5)}.{self.generator.random.randint(0, 1)}"
  226. if not self.generator.random.getrandbits(1)
  227. else f"{self.generator.random.randint(4, 5)}.0.{self.generator.random.randint(1, 5)}"
  228. )
  229. tmplt_win: str = "(Windows; U; {0}) AppleWebKit/{1} (KHTML, like Gecko)" " Version/{2} Safari/{3}"
  230. tmplt_mac: str = "({0} rv:{1}.0; {2}) AppleWebKit/{3} (KHTML, like Gecko)" " Version/{4} Safari/{5}"
  231. tmplt_ipod: str = (
  232. "(iPod; U; CPU iPhone OS {0}_{1} like Mac OS X; {2})"
  233. " AppleWebKit/{3} (KHTML, like Gecko) Version/{4}.0.5"
  234. " Mobile/8B{5} Safari/6{6}"
  235. )
  236. locale: str = self.generator.locale().replace("_", "-")
  237. platforms: ElementsType = (
  238. tmplt_win.format(self.windows_platform_token(), saf, ver, saf),
  239. tmplt_mac.format(
  240. self.mac_platform_token(),
  241. self.generator.random.randint(2, 6),
  242. locale,
  243. saf,
  244. ver,
  245. saf,
  246. ),
  247. tmplt_ipod.format(
  248. self.generator.random.randint(3, 4),
  249. self.generator.random.randint(0, 3),
  250. locale,
  251. saf,
  252. self.generator.random.randint(3, 4),
  253. self.generator.random.randint(111, 119),
  254. saf,
  255. ),
  256. )
  257. return "Mozilla/5.0 " + self.random_element(platforms)
  258. def opera(self) -> str:
  259. """Generate an Opera web browser user agent string."""
  260. token: str = (
  261. self.linux_platform_token() if self.generator.random.getrandbits(1) else self.windows_platform_token()
  262. )
  263. locale: str = self.generator.locale().replace("_", "-")
  264. platform: str = (
  265. f"({token}; {locale}) Presto/2.9.{self.generator.random.randint(160, 190)} "
  266. f"Version/{self.generator.random.randint(10, 12)}.00"
  267. )
  268. return f"Opera/{self.generator.random.randint(8, 9)}.{self.generator.random.randint(10, 99)}.{platform}"
  269. def internet_explorer(self) -> str:
  270. """Generate an IE web browser user agent string."""
  271. return (
  272. f"Mozilla/5.0 (compatible; MSIE {self.generator.random.randint(5, 9)}.0; "
  273. f"{self.windows_platform_token()}; "
  274. f"Trident/{self.generator.random.randint(3, 5)}.{self.generator.random.randint(0, 1)})"
  275. )
  276. def windows_platform_token(self) -> str:
  277. """Generate a Windows platform token used in user agent strings."""
  278. return self.random_element(self.windows_platform_tokens)
  279. def linux_platform_token(self) -> str:
  280. """Generate a Linux platform token used in user agent strings."""
  281. return f"X11; Linux {self.random_element(self.linux_processors)}"
  282. def mac_platform_token(self) -> str:
  283. """Generate a MacOS platform token used in user agent strings."""
  284. return (
  285. f"Macintosh; {self.random_element(self.mac_processors)} Mac OS X 10_"
  286. f"{self.generator.random.randint(5, 12)}_{self.generator.random.randint(0, 9)}"
  287. )
  288. def android_platform_token(self) -> str:
  289. """Generate an Android platform token used in user agent strings."""
  290. return f"Android {self.random_element(self.android_versions)}"
  291. def ios_platform_token(self) -> str:
  292. """Generate an iOS platform token used in user agent strings."""
  293. apple_device: str = self.random_element(self.apple_devices)
  294. ios_version: str = self.random_element(self.ios_versions)
  295. return f"{apple_device}; CPU {apple_device} " f'OS {ios_version.replace(".", "_")} like Mac OS X'