Skip to content

Commit 8871625

Browse files
Refactor provider storage and loading
1 parent dbd286f commit 8871625

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

gandi_dyndns.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class GandiServerProxy(object):
1919
calling the API using Python attribute accessors instead of strings, and
2020
allows for the API key to be pre-loaded into all method calls.
2121
'''
22+
2223
def __init__(self, api_key, proxy=None, chain=[], test=False):
2324
self.api_key = api_key
2425
self.chain = chain
@@ -58,11 +59,8 @@ def __call__(self, *args):
5859
def get_external_ip(attempts=100, threshold=3):
5960
'''Return our current external IP address, or None if there was an error.'''
6061

61-
# read the list of IP address providers, de-duping and normalizing them
62-
providers = []
63-
with open('ip-providers.txt') as f:
64-
providers = set(line.strip() for line in f)
65-
providers = filter(lambda x: not not x, providers)
62+
# load the tuple of IP address providers
63+
providers = load_providers()
6664

6765
# we want several different providers to agree on the address, otherwise we
6866
# need to keep trying to get agreement. this prevents picking up 'addresses'
@@ -119,8 +117,14 @@ def get_external_ip(attempts=100, threshold=3):
119117
# return None if no agreement could be reached
120118
return None
121119

120+
def load_providers():
121+
'''Load the providers file as a de-duplicated and normalized tuple of URLs.'''
122+
with open('providers.json') as f:
123+
providers = json.load(f)['providers']
124+
return tuple(set([p.strip() for p in providers]))
125+
122126
def load_config():
123-
'''Load the config file from disk'''
127+
'''Load the config file from disk.'''
124128
with open('config.json') as f:
125129
return json.load(f)
126130

ip-providers.txt

Lines changed: 0 additions & 18 deletions
This file was deleted.

providers.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"providers": [
3+
"http://checkip.dyndns.org",
4+
"http://cmyip.com",
5+
"http://fmbip.com",
6+
"http://ip-api.com/csv",
7+
"http://ipchicken.com",
8+
"http://ipinfo.io/ip",
9+
"http://my-ip.asia",
10+
"http://ventrilo.com/myip.php",
11+
"http://whatismyipaddress.org",
12+
"http://whatsmyip.net",
13+
"http://www.givememyip.com",
14+
"http://www.moanmyip.com/",
15+
"http://www.myip.ru/en-EN/index.php",
16+
"http://www.myipnumber.com/my-ip-address.asp",
17+
"http://www.showmyip.gr",
18+
"http://www.whatsmyip.us",
19+
"https://freegeoip.net/csv/",
20+
"https://www.name.com/whats-my-ip-address"
21+
]
22+
}

0 commit comments

Comments
 (0)