domainLookup.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
  2. import time, json, os, sys, re, socket, json
  3. # Either returns domain's address or none if it doesn't exist
  4. # Supports subdomains and .bit on the end
  5. def lookupDomain(domain):
  6. domain = domain.lower()
  7. #remove .bit on end
  8. if domain[-4:] == ".bit":
  9. domain = domain[0:-4]
  10. #check for subdomain
  11. if domain.find(".") != -1:
  12. subdomain = domain[0:domain.find(".")]
  13. domain = domain[domain.find(".")+1:]
  14. else:
  15. subdomain = ""
  16. try:
  17. domain_object = rpc.name_show("d/"+domain)
  18. except:
  19. #domain doesn't exist
  20. return None
  21. domain_json = json.loads(domain_object['value'])
  22. try:
  23. domain_address = domain_json["zeronet"][subdomain]
  24. except:
  25. #domain exists but doesn't have any zeronet value
  26. return None
  27. return domain_address
  28. # Loading config...
  29. # Check whether platform is on windows or linux
  30. # On linux namecoin is installed under ~/.namecoin, while on on windows it is in %appdata%/Namecoin
  31. if sys.platform == "win32":
  32. namecoin_location = os.getenv('APPDATA') + "/Namecoin/"
  33. else:
  34. namecoin_location = os.path.expanduser("~/.namecoin/")
  35. # Getting rpc connect details
  36. namecoin_conf = open(namecoin_location + "namecoin.conf").read()
  37. # Connecting to RPC
  38. rpc_user = re.search("rpcuser=(.*)$", namecoin_conf, re.M).group(1)
  39. rpc_pass = re.search("rpcpassword=(.*)$", namecoin_conf, re.M).group(1)
  40. rpc_url = "http://%s:%s@127.0.0.1:8336" % (rpc_user, rpc_pass)
  41. rpc = AuthServiceProxy(rpc_url, timeout=60*5)
  42. """
  43. while 1:
  44. print "Waiting for new block",
  45. sys.stdout.flush()
  46. while 1:
  47. try:
  48. rpc = AuthServiceProxy(rpc_url, timeout=60*5)
  49. if (int(rpc.getinfo()["blocks"]) > last_block): break
  50. time.sleep(1)
  51. rpc.waitforblock()
  52. print "Found"
  53. break # Block found
  54. except socket.timeout: # Timeout
  55. print ".",
  56. sys.stdout.flush()
  57. except Exception, err:
  58. print "Exception", err.__class__, err
  59. time.sleep(5)
  60. last_block = int(rpc.getinfo()["blocks"])
  61. for block_id in range(config["lastprocessed"]+1, last_block+1):
  62. processBlock(block_id)
  63. config["lastprocessed"] = last_block
  64. open(config_path, "w").write(json.dumps(config, indent=2))
  65. """