forked from maldevel/IPGeoLocation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileExporter.py
135 lines (107 loc) · 5.82 KB
/
FileExporter.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
# encoding: UTF-8
"""
This file is part of IPGeoLocation tool.
Copyright (C) 2015-2016 @maldevel
https://github.com/maldevel/IPGeoLocation
IPGeoLocation - Retrieve IP Geolocation information
Powered by http://ip-api.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
For more see the file 'LICENSE' for copying permission.
"""
__author__ = 'maldevel'
import csv
from xml.etree import ElementTree as etree
from collections import OrderedDict
class FileExporter:
def __init__(self):
pass
def ExportListToCSV(self, ipGeoLocObjs, filename):
return self.__ExportToCSV(ipGeoLocObjs, filename)
def ExportToCSV(self, ipGeoLocObj, filename):
return self.__ExportToCSV([ipGeoLocObj], filename)
def ExportListToXML(self, ipGeoLocObjs, filename):
return self.__ExportToXML(ipGeoLocObjs, filename)
def ExportToXML(self, ipGeoLocObj, filename):
return self.__ExportToXML([ipGeoLocObj], filename)
def ExportListToTXT(self, ipGeoLocObjs, filename):
return self.__ExportToTXT(ipGeoLocObjs, filename)
def ExportToTXT(self, ipGeoLocObj, filename):
return self.__ExportToTXT([ipGeoLocObj], filename)
def __ExportToTXT(self, ipGeoLocObjs, filename):
try:
with open(filename, 'w') as txtfile:
txtfile.write('Results IPGeolocation\n')
for ipGeoLocObj in ipGeoLocObjs:
if ipGeoLocObj:
txtfile.write('Target: {}\n'.format(ipGeoLocObj.Query))
txtfile.write('IP: {}\n'.format(ipGeoLocObj.IP))
txtfile.write('ASN: {}\n'.format(ipGeoLocObj.ASN))
txtfile.write('City: {}\n'.format(ipGeoLocObj.City))
txtfile.write('Country: {}\n'.format(ipGeoLocObj.Country))
txtfile.write('Country Code: {}\n'.format(ipGeoLocObj.CountryCode))
txtfile.write('ISP: {}\n'.format(ipGeoLocObj.ISP))
txtfile.write('Latitude: {}\n'.format(ipGeoLocObj.Latitude))
txtfile.write('Longtitude: {}\n'.format(ipGeoLocObj.Longtitude))
txtfile.write('Organization: {}\n'.format(ipGeoLocObj.Organization))
txtfile.write('Region: {}\n'.format(ipGeoLocObj.Region))
txtfile.write('Region Name: {}\n'.format(ipGeoLocObj.RegionName))
txtfile.write('Timezone: {}\n'.format(ipGeoLocObj.Timezone))
txtfile.write('Zip: {}\n'.format(ipGeoLocObj.Zip))
txtfile.write('Google Maps: {}\n'.format(ipGeoLocObj.GoogleMapsLink))
txtfile.write('\n')
return True
except:
return False
def __ExportToXML(self, ipGeoLocObjs, filename):
try:
root = etree.Element('Results')
for ipGeoLocObj in ipGeoLocObjs:
if ipGeoLocObj:
orderedData = OrderedDict(sorted(ipGeoLocObj.ToDict().items()))
self.__add_items(etree.SubElement(root, 'IPGeolocation'),
((key.replace(' ', ''), value) for key, value in orderedData.items()))
tree = etree.ElementTree(root)
tree.write(filename, xml_declaration=True, encoding='utf-8')
return True
except:
return False
def __ExportToCSV(self, ipGeoLocObjs, filename):
try:
with open(filename, 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['Results', 'IPGeolocation'])
for ipGeoLocObj in ipGeoLocObjs:
if ipGeoLocObj:
writer.writerow(['Target', ipGeoLocObj.Query])
writer.writerow(['IP', ipGeoLocObj.IP])
writer.writerow(['ASN', ipGeoLocObj.ASN])
writer.writerow(['City', ipGeoLocObj.City])
writer.writerow(['Country', ipGeoLocObj.Country])
writer.writerow(['Country Code', ipGeoLocObj.CountryCode])
writer.writerow(['ISP', ipGeoLocObj.ISP])
writer.writerow(['Latitude', ipGeoLocObj.Latitude])
writer.writerow(['Longtitude', ipGeoLocObj.Longtitude])
writer.writerow(['Organization', ipGeoLocObj.Organization])
writer.writerow(['Region', ipGeoLocObj.Region])
writer.writerow(['Region Name', ipGeoLocObj.RegionName])
writer.writerow(['Timezone', ipGeoLocObj.Timezone])
writer.writerow(['Zip', ipGeoLocObj.Zip])
writer.writerow(['Google Maps', ipGeoLocObj.GoogleMapsLink])
writer.writerow([])
return True
except:
return False
def __add_items(self, root, items):
for name, text in items:
elem = etree.SubElement(root, name)
elem.text = text