Mercurial > hg > config
annotate python/tld.py @ 845:70ae14b6be51
[sync] ~/k0s is no longer in PATH as it corrupts PYTHONPATH
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Thu, 17 Aug 2017 09:48:49 -0700 |
parents | 0bc7c1a5eb83 |
children |
rev | line source |
---|---|
117
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 |
812
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
3 """ |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
4 Top-Level Domain names (TLDs), country codes only |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
5 """ |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
6 |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
7 import argparse |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
8 import sys |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
9 import urllib2 |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
10 |
117
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 URL = 'http://www.palosverdes.com/jesse/irc/country.txt' |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 def codes(): |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 f = urllib2.urlopen(URL) |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 codes = {} |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 for line in f.readlines(): |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 line = line.strip() |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 if not line: |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 continue |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 key, value = line.split(None, 1) |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 codes[key.lower()] = value |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 return codes |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 codes = codes() |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 |
812
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
26 |
117
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 def get(code): |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 code = code.lstrip('.').lower() |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 return codes.get(code) |
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 |
812
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
31 def main(args=sys.argv[1:]): |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
32 """CLI""" |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
33 |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
34 # parse command line |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
35 parser = argparse.ArgumentParser(description=__doc__) |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
36 parser.add_argument('tld', nargs='+', |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
37 help="country-code TLD to look up") |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
38 options = parser.parse_args(args) |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
39 |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
40 # lookup + output |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
41 for arg in options.tld: |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
42 print ('{}: {}'.format(arg, get(arg))) |
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
43 |
117
c7edd15d7f8b
add a function to get a TLD from a webservice
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 if __name__ == '__main__': |
812
0bc7c1a5eb83
make into an almost real CLI
Jeff Hammel <k0scist@gmail.com>
parents:
117
diff
changeset
|
45 main() |