SiteManagerPlugin.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import logging
  2. import re
  3. import time
  4. from Config import config
  5. from Plugin import PluginManager
  6. allow_reload = False # No reload supported
  7. log = logging.getLogger("ZeronamePlugin")
  8. @PluginManager.registerTo("SiteManager")
  9. class SiteManagerPlugin(object):
  10. site_zeroname = None
  11. db_domains = {}
  12. db_domains_modified = None
  13. def load(self, *args, **kwargs):
  14. super(SiteManagerPlugin, self).load(*args, **kwargs)
  15. if not self.get(config.bit_resolver):
  16. self.need(config.bit_resolver) # Need ZeroName site
  17. # Return: True if the address is .bit domain
  18. def isBitDomain(self, address):
  19. return re.match(r"(.*?)([A-Za-z0-9_-]+\.bit)$", address)
  20. # Resolve domain
  21. # Return: The address or None
  22. def resolveBitDomain(self, domain):
  23. domain = domain.lower()
  24. if not self.site_zeroname:
  25. self.site_zeroname = self.need(config.bit_resolver)
  26. site_zeroname_modified = self.site_zeroname.content_manager.contents.get("content.json", {}).get("modified", 0)
  27. if not self.db_domains or self.db_domains_modified != site_zeroname_modified:
  28. self.site_zeroname.needFile("data/names.json", priority=10)
  29. s = time.time()
  30. try:
  31. self.db_domains = self.site_zeroname.storage.loadJson("data/names.json")
  32. except Exception as err:
  33. log.error("Error loading names.json: %s" % err)
  34. log.debug(
  35. "Domain db with %s entries loaded in %.3fs (modification: %s -> %s)" %
  36. (len(self.db_domains), time.time() - s, self.db_domains_modified, site_zeroname_modified)
  37. )
  38. self.db_domains_modified = site_zeroname_modified
  39. return self.db_domains.get(domain)
  40. # Turn domain into address
  41. def resolveDomain(self, domain):
  42. return self.resolveBitDomain(domain) or super(SiteManagerPlugin, self).resolveDomain(domain)
  43. # Return: True if the address is domain
  44. def isDomain(self, address):
  45. return self.isBitDomain(address) or super(SiteManagerPlugin, self).isDomain(address)
  46. @PluginManager.registerTo("ConfigPlugin")
  47. class ConfigPlugin(object):
  48. def createArguments(self):
  49. group = self.parser.add_argument_group("Zeroname plugin")
  50. group.add_argument(
  51. "--bit_resolver", help="ZeroNet site to resolve .bit domains",
  52. default="1Name2NXVi1RDPDgf5617UoW7xA6YrhM9F", metavar="address"
  53. )
  54. return super(ConfigPlugin, self).createArguments()