Fix
[novacoin.git] / contrib / seeds / getseeds.py
index a739e7a..7a208e8 100755 (executable)
@@ -1,3 +1,5 @@
+#!/usr/bin/python3
+
 ###
 ###  Automatic seeding over HTTP
 ###  (C) 2016 Alex D.
@@ -5,19 +7,27 @@
 ###  http://www.gnu.org/licenses/agpl-3.0.en.html
 ###
 
+import os
+import sys
 import json
 import random
-import urllib2
-
-import os
 import platform
+import urllib.request
+
+sources = ['http://dalexhz1.cloudapp.net', 'http://dalexhz2.cloudapp.net', 'http://dalexhz4.cloudapp.net', 'http://dalexhz5.cloudapp.net']
+data = urllib.request.urlopen(random.choice(sources)).readall();
+
+if 'norpc' in sys.argv:
+    for node in json.loads(data.decode()):
+        print (node)
+    sys.exit()
 
 ###  This module is required to function properly: 
 ###      https://github.com/jgarzik/python-bitcoinrpc
 
+import errno
 from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
-
-sources = ['http://dalexhz1.cloudapp.net', 'http://dalexhz2.cloudapp.net', 'http://dalexhz4.cloudapp.net', 'http://dalexhz5.cloudapp.net']
+from socket import error as socket_error
 
 def confFile():
     folder = ''
@@ -46,41 +56,45 @@ conf = open(conf_path, 'rb').read()
 contents = {}
 
 for line in conf.splitlines(True):
-    if '#' in line:
+    if line.startswith(b'#'):
         line = line[:line.index('#')]
-    if '=' not in line:
+    if not line.__contains__(b'='):
         continue
-    k, v = line.split('=', 1)
+    k, v = line.split(b'=', 1)
     contents[k.strip()] = v.strip()
 
-if 'rpcpassword' not in contents.keys():
-    parser.error(
-        '''RPC password is not found in the %s file.''' % (conf_path))
+print(contents)
 
-rpcuser = 'novacoin'
-rpcpassword = contents['rpcpassword']
-rpcport = 8344
-rpclisten = '127.0.0.1'
+if b'rpcpassword' not in contents.keys():
+    print('''RPC password is not found in the %s file.''' % (conf_path))
+    sys.exit()
 
-if 'rpcport' in contents.keys():
-    rpcport = contents['rpcport']
+if b'rpcuser' not in contents.keys():
+    print('''RPC user is not found in the %s file.''' % (conf_path))
+    sys.exit()
 
-if 'rpcuser' in contents.keys():
-    rpcuser = contents['rpcuser']
-
-if 'rpclisten' in contents.keys():
-    rpcuser = contents['rpclisten']
+rpcuser = contents[b'rpcuser'].decode()
+rpcpassword = contents[b'rpcpassword'].decode()
+rpcport = 8344
+rpclisten = '127.0.0.1'
 
-url = "http://"+rpcuser+":"+rpcpassword+"@"+rpclisten+":"+rpcport+"/"
+if b'rpcport' in contents.keys():
+    rpcport = contents[b'rpcport'].decode()
+if b'rpclisten' in contents.keys():
+    rpclisten = contents[b'rpclisten'].decode()
 
-access = AuthServiceProxy(url)
-data = json.load(urllib2.urlopen(random.choice(sources)))
+access = AuthServiceProxy("http://"+rpcuser+":"+rpcpassword+"@"+rpclisten+":"+rpcport+"/")
 
-for node in data:
-    print 'Adding', node
+for node in json.loads(data.decode()):
+    print ('Adding', node)
     try:
         access.addnode(node, 'add')
-    except:
-        print 'Already added'
-        pass
-    
\ No newline at end of file
+    except socket_error as e:
+        if e.errno == errno.ECONNREFUSED:
+            print ('Unable to communicate with Novacoin RPC')
+        break
+    except JSONRPCException as e:
+        if e.code == -23:
+            print ('Already added')
+            continue
+        break