summaryrefslogtreecommitdiffstats
path: root/src/weblookuptools.py
blob: b48b766612051ae937e8db28b8630cdcdf6aeb5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

import urllib2
import json
import atexit
import cPickle
import os
import sys
import socket

def iplocation(ip):
	if not iplocation.iplocationInitialized:
		iplocation.iplocationInitialized = True
		cachePath = os.path.join(os.path.dirname(sys.argv[0]), ".geoipcache")
		atexit.register(saveGeoIPCache, cachePath)
		if os.path.exists(cachePath):
			cacheFile = open(cachePath, "r")
			iplocation.cache = cPickle.Unpickler(cacheFile).load()
			cacheFile.close()
	if ip in iplocation.cache:
		return iplocation.cache[ip]
	try:
		response = json.load(urllib2.urlopen("http://ipinfodb.com/ip_query.php?ip=%s&output=json" % ip))
		location = ((response["Latitude"], response["Longitude"]), (response["City"], response["RegionName"], response["CountryName"]))
	except:
		location = ()
	iplocation.cache[ip] = location
	return location
iplocation.cache = {}
iplocation.iplocationInitialized = False

def saveGeoIPCache(cachePath):
	cacheFile = open(cachePath, "w")
	cPickle.Pickler(cacheFile).dump(iplocation.cache)
	cacheFile.close()

def ipFromHost(host):
	if not ipFromHost.ipFromHostInitialized:
		ipFromHost.ipFromHostInitialized = True
		cachePath = os.path.join(os.path.dirname(sys.argv[0]), ".dnscache")
		atexit.register(saveDNSCache, cachePath)
		if os.path.exists(cachePath):
			cacheFile = open(cachePath, "r")
			ipFromHost.cache = cPickle.Unpickler(cacheFile).load()
			cacheFile.close()
	if host in ipFromHost.cache:
		return ipFromHost.cache[host]
	try:
		ip = socket.gethostbyname(host)
	except:
		ip = ""
	ipFromHost.cache[host] = ip
	return ip
ipFromHost.cache = {}
ipFromHost.ipFromHostInitialized = False

def saveDNSCache(cachePath):
	cacheFile = open(cachePath, "w")
	cPickle.Pickler(cacheFile).dump(ipFromHost.cache)
	cacheFile.close()

def generateHostRegex():
	cachePath = os.path.join(os.path.dirname(sys.argv[0]), ".hostregexcache")
	if os.path.exists(cachePath):
		try:
			cacheFile = open(cachePath, "r")
			cached = cacheFile.read()
			cacheFile.close()
			return cached
		except:
			pass
	import urllib2
	tldList = urllib2.urlopen("http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/src/effective_tld_names.dat?raw=1")
	regex = r'[ \[(\n\t]([a-zA-Z0-9\-.]{4,70}\.(?:'
	first = True
	for line in tldList:
		if line.startswith("//") or line.startswith("!") or line == "\n":
			continue
		if not first:
			regex += "|"
		else:
			first = False
		regex += line.strip().replace("*.", "").replace('.', '\.');
	regex += r'))[ \[(\n\t]'
	cacheFile = open(cachePath, "w")
	cacheFile.write(regex)
	cacheFile.close()
	return regex