2d61795ff93de01a3333e7657ed990f76cd3e265
[novacoin.git] / contrib / seeds / getseeds.py
1 #!/usr/bin/python
2
3 ###
4 ###  Automatic seeding over HTTP
5 ###  (C) 2016 Alex D.
6 ###
7 ###  http://www.gnu.org/licenses/agpl-3.0.en.html
8 ###
9
10 import os
11 import sys
12 import json
13 import random
14 import urllib2
15 import platform
16
17
18 sources = ['http://dalexhz1.cloudapp.net', 'http://dalexhz2.cloudapp.net', 'http://dalexhz4.cloudapp.net', 'http://dalexhz5.cloudapp.net']
19 data = urllib2.urlopen(random.choice(sources))
20
21 if 'norpc' in sys.argv:
22     for node in json.load(data):
23         print node
24     sys.exit()
25
26 ###  This module is required to function properly: 
27 ###      https://github.com/jgarzik/python-bitcoinrpc
28
29 import errno
30 from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
31 from socket import error as socket_error
32
33 def confFile():
34     folder = ''
35     if platform.system() == 'Windows':
36         folder = os.path.join(os.path.join(os.environ['APPDATA'], 'NovaCoin'))
37     else:
38         if platform.system() == 'Darwin':
39             folder = os.path.expanduser('~/Library/Application Support/NovaCoin/')
40         else:
41             folder = os.path.expanduser('~/.novacoin')
42
43     return os.path.join(folder, 'novacoin.conf')
44
45 conf_path = confFile()
46 if not os.path.exists(conf_path):
47     parser.error('''Novacoin configuration file not found. Manually enter your RPC password.\r\n'''
48         '''If you actually haven't created a configuration file, you should create one at %s with the text like this:\r\n'''
49         '''\r\n'''
50         '''server=1\r\n'''
51         '''rpcuser=yourname\r\n'''
52         '''rpcpassword=%x\r\n'''
53         '''\r\n'''
54         '''Keep that password secret! After creating the file, restart Novacoin.''' % (conf_path, random.randrange(2**128)))
55
56 conf = open(conf_path, 'rb').read()
57 contents = {}
58
59 for line in conf.splitlines(True):
60     if '#' in line:
61         line = line[:line.index('#')]
62     if '=' not in line:
63         continue
64     k, v = line.split('=', 1)
65     contents[k.strip()] = v.strip()
66
67 if 'rpcpassword' not in contents.keys():
68     parser.error(
69         '''RPC password is not found in the %s file.''' % (conf_path))
70
71 rpcuser = 'novacoin'
72 rpcpassword = contents['rpcpassword']
73 rpcport = 8344
74 rpclisten = '127.0.0.1'
75
76 if 'rpcport' in contents.keys():
77     rpcport = contents['rpcport']
78
79 if 'rpcuser' in contents.keys():
80     rpcuser = contents['rpcuser']
81
82 if 'rpclisten' in contents.keys():
83     rpcuser = contents['rpclisten']
84
85 url = "http://"+rpcuser+":"+rpcpassword+"@"+rpclisten+":"+rpcport+"/"
86
87 access = AuthServiceProxy(url)
88
89 for node in json.load(data):
90     print 'Adding', node
91     try:
92         access.addnode(node, 'add')
93     except socket_error, e:
94         if e.errno == errno.ECONNREFUSED:
95             print 'Unable to communicate with Novacoin RPC'
96         break
97     except JSONRPCException, e:
98         if e.code == -23:
99             print 'Already added'
100             continue
101         break