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