-
Notifications
You must be signed in to change notification settings - Fork 2
/
Exporter.py
executable file
·78 lines (61 loc) · 2.58 KB
/
Exporter.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
#!/usr/bin/env python3
"""Exporter of profiles for github profile crawler"""
from BaseLogger import BaseLogger
from DatabaseAccessor import DatabaseAccessor
from contextlib import closing
from csv import DictWriter
from json import dump
from os import remove
from platform import node
from time import strftime
from zipfile import ZipFile, ZIP_DEFLATED
class Exporter(BaseLogger):
def __init__(self, log_level=None):
BaseLogger.__init__(self, self.__class__.__name__, log_level)
self._db_conn = DatabaseAccessor()
self._log_info("exporter start @%s", node())
def process(self):
filelist = []
data = self._db_conn.profile_read()
self._log_info("load all profiles data from database")
filelist.append(self._save_as_json(data))
filelist.append(self._save_as_csv(data))
data = self._db_conn.profile_read('email')
self._log_info("load profiles data with email from database")
filelist.append(self._save_as_json(data, "profile_email.json"))
filelist.append(self._save_as_csv(data, "profile_email.csv"))
self._archive_into_zipfile(filelist)
def _save_as_json(self, data, filename="profile.json"):
with open(filename, 'w') as jsonfile:
for item in data:
dump(item, jsonfile, sort_keys=True)
jsonfile.write("\n")
self._log_info("save %d items as json file: %s", len(data), filename)
return filename
def _save_as_csv(self, data, filename="profile.csv"):
fields = set()
for item in data:
fields = fields.union(set(item.keys()))
with open(filename, 'w', encoding='utf8', newline='') as csvfile:
writer = DictWriter(csvfile, extrasaction='ignore', dialect='excel', fieldnames=sorted(fields, reverse=True))
writer.writeheader()
for item in data:
writer.writerow(item)
self._log_info("save %d items as csv file: %s", len(data), filename)
return filename
def _archive_into_zipfile(self, filelist):
zipname = "profile_{}.zip".format(strftime("%Y-%m-%d_%H-%M-%S"))
with ZipFile(zipname, 'w', ZIP_DEFLATED) as zip:
for filename in filelist:
zip.write(filename)
remove(filename)
self._log_info("archive exported files into %s", zipname)
def close(self):
self._db_conn.close()
self._log_info("exporter exit")
self._close_logger()
def main():
with closing(Exporter()) as exporter:
exporter.process()
if __name__ == '__main__':
main()