get_ua_cache.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/python3
  2. # SPDX-License-Identifier: Apache-2.0
  3. """
  4. Get user agents strings from ua_spoofer, and save in several formats, for use
  5. as a backup cache.
  6. """
  7. import json
  8. from datetime import datetime, timezone
  9. from pathlib import Path
  10. from ua_spoofer import UserAgent
  11. now = datetime.now(timezone.utc)
  12. timestamp = int(now.timestamp())
  13. ua = UserAgent()
  14. ua_list = "\n".join(ua.all)
  15. browsers = {k: sorted(v) for k, v in ua.dict.items()}
  16. ua_json = {
  17. "browsers": browsers,
  18. "last_updated_utc": timestamp,
  19. }
  20. ua_json = json.dumps(ua_json, indent=1)
  21. ua_md = "# Common User Agent Strings\n\nLast updated: {}\n".format(
  22. now.strftime("%Y-%m-%d %H:%M %Z")
  23. )
  24. for browser, strings in browsers.items():
  25. ua_md += "\n## {}\n\n".format(browser.replace("-", " ").title())
  26. for uas in strings:
  27. ua_md += " * {}\n".format(uas)
  28. output = Path("cache")
  29. output.mkdir(exist_ok=True)
  30. output /= "user_agents.json"
  31. output.write_text(ua_json)
  32. output.with_suffix(".txt").write_text(ua_list)
  33. output.with_suffix(".md").write_text(ua_md)