2626
2727
2828
29- ip_sources = ["Auto select" , "IP-API " , "IPInfoDB " , "freegeoip.net" ]
30- ip_urls = ["" , "http://ip-api.com /json" , "http://www.ipinfodb. com/my_ip_location.php " , "http://freegeoip.net/json/" ]
29+ ip_sources = ["Auto select" , "ipinfo.io " , "IP-API " , "freegeoip.net" ]
30+ ip_urls = ["" , "http://ipinfo.io /json" , "http://ip-api. com/json " , "http://freegeoip.net/json/" ]
3131LIST_DEFAULT = "0,0,0"
3232
3333MAX_ERROR = 64
@@ -38,7 +38,7 @@ def getIPInfoFrom(source):
3838 # No info generated from call is "no info", "unknown", "unknown", "unknown", url response
3939 # Or general error is "error", "error", "error", reason, url response
4040 link = ""
41- try :
41+ try :
4242 # Determine the URL, make the call and read the response
4343 url = getIPSourceURL (source )
4444 if url == "" : return "error" , "error" , "error" , "unknown source" , ""
@@ -47,23 +47,45 @@ def getIPInfoFrom(source):
4747 response = urllib2 .urlopen (req )
4848 link = response .read ()
4949 response .close ()
50-
51- # Call the right routine to parse the reply using regex.
52- # If the website changes, this parsing can fail...sigh
53- if source == "IPInfoDB" : match = getIPInfoDB (link )
50+ except Exception as e :
51+ errorTrace ("ipinfo.py" , "Couldn't connect to IP provider " + source )
52+ errorTrace ("ipinfo.py" , str (e ))
53+ recordError (source )
54+ return "error" , "error" , "error" , "call failed" , link
55+
56+ try :
57+ # This makes stupid regex easier
58+ link = link .replace ("\n " ,"" )
59+ debugTrace ("IP provider " + source + " returned " + link )
60+ # Call the right routine to parse the reply using regex
61+ # These all return JSON so probably a JSON parser should really be used
62+ if source == "ipinfo.io" : match = getipinfo (link )
5463 if source == "IP-API" : match = getIPAPI (link )
5564 if source == "freegeoip.net" : match = getFreeGeoIP (link )
56- if len (match ) > 0 :
65+
66+ if not match == None :
5767 recordWorking (source )
5868 for ip , country , region , city , isp in match :
5969 return ip , country , region , city , isp
6070 else :
71+ errorTrace ("ipinfo.py" , "No matches found for IP provider " + "source" + " " + link )
6172 recordError (source )
6273 return "no info" , "unknown location" , "unknown location" , "no matches" , link
63- except :
74+ except Exception as e :
75+ errorTrace ("ipinfo.py" , "Couldn't parse response from IP provider " + source + " " + link )
76+ errorTrace ("ipinfo.py" , str (e ))
6477 recordError (source )
65- return "error" , "error" , "error" , "call failed" , link
78+ return "error" , "error" , "error" , "parse failed" , link
6679
80+
81+ def getipinfo (link ):
82+ match = re .compile (ur'"ip": "(.*?)".*"city": "(.*?)".*"region": "(.*?)".*"country": "(.*?)".*"org": "(.*?)"' ).findall (link )
83+ if len (match ) > 0 :
84+ for ip , city , region , country , isp in match :
85+ return [(ip , country , region , city , isp )]
86+ else :
87+ return None
88+
6789
6890def getIPAPI (link ):
6991 match = re .compile (ur'"city":"(.*?)".*"country":"(.*?)".*"isp":"(.*?)".*"query":"(.*?)".*"regionName":"(.*?)"' ).findall (link )
@@ -74,15 +96,6 @@ def getIPAPI(link):
7496 return None
7597
7698
77- def getIPInfoDB (link ):
78- match = re .compile (ur'<h5>Your IP address.*</h5>.*\s*.*<br>.*IP2Location.*\s*.*\s*<li>IP address.*<strong>(.+?)</strong>.*\s*\s*<li>Country : (.+?) <img.*\s*<li>State.*: (.+?)</li>.*\s*<li>City : (.+?)</li>' ).findall (link )
79- if len (match ) > 0 :
80- for ip , country , region , city in match :
81- return [(ip , country , region , city , "Unknown" )]
82- else :
83- return None
84-
85-
8699def getFreeGeoIP (link ):
87100 match = re .compile (ur'"ip":"(.*?)".*"country_name":"(.*?)".*"region_name":"(.*?).*"city":"(.*?)".*' ).findall (link )
88101 if len (match ) > 0 :
0 commit comments