summaryrefslogtreecommitdiffstats
path: root/src/locationverifier.py
blob: 031ba793ed26b4081fb9d5fcd841a8038cd62228 (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
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

import csv
from datetime import date

class LocationVerifier:
	def __init__(self, locationTableFile):
		self.locations = []
		self.misses = []
		self.totalEmailsInRange = 0
		self.locationHit = 0
		self.defaultLocationHit = 0
		reader = csv.reader(locationTableFile, delimiter=',', quotechar='"')
		self.defaultLocation = reader.next()[0]
		self.startDate = date.max
		self.endDate = date.min
		for row in reader:
			start = row[0].split("/")
			end = row[1].split("/")
			start = date(int(start[2]), int(start[0]), int(start[1]))
			end = date(int(end[2]), int(end[0]), int(end[1]))
			self.locations.append((start, end, row[2:]))
			if start < self.startDate:
				self.startDate = start
			if end > self.endDate:
				self.endDate = end
		
	def checkEmail(self, email):
		if email.date.date() < self.startDate or email.date.date() > self.endDate:
			return
		self.totalEmailsInRange += 1
		country = email.location()[1][2]
		inDefault = False
		if country.find(self.defaultLocation) != -1:
			inDefault = True
		for location in self.locations:
			if email.date.date() >= location[0] and email.date.date() <= location[1]:
				if email.date.date() != location[0] and email.date.date() != location[1]:
					inDefault = False
				for place in location[2]:
					if country.find(place) != -1:
						self.locationHit += 1
						return True
		if inDefault:
			self.defaultLocationHit += 1
			return True
		else:
			self.misses.append(email)
			return False
	def __str__(self):
		return "%s emails from %s to %s\n%s in default location (%s)\n%s in verified locations\n%s in non-verified locations\n%s%% in default or verified locations\n%s%% in verified locations out of all non-default locations" % (self.totalEmailsInRange, self.startDate, self.endDate, self.defaultLocationHit, self.defaultLocation, self.locationHit, len(self.misses), ((float(self.defaultLocationHit) + float(self.locationHit)) / float(self.totalEmailsInRange) * 100.0), (float(self.locationHit) / (float(self.totalEmailsInRange) - float(self.defaultLocationHit)) * 100.0))