Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
maurosoria
GitHub Repository: maurosoria/dirsearch
Path: blob/master/lib/report/csv_report.py
896 views
1
# -*- coding: utf-8 -*-
2
# This program is free software; you can redistribute it and/or modify
3
# it under the terms of the GNU General Public License as published by
4
# the Free Software Foundation; either version 2 of the License, or
5
# (at your option) any later version.
6
#
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
# GNU General Public License for more details.
11
#
12
# You should have received a copy of the GNU General Public License
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15
# MA 02110-1301, USA.
16
#
17
# Author: Mauro Soria
18
19
from defusedcsv import csv
20
21
from lib.core.decorators import locked
22
from lib.report.factory import BaseReport, FileReportMixin
23
24
25
class CSVReport(FileReportMixin, BaseReport):
26
__format__ = "csv"
27
__extension__ = "csv"
28
29
def new(self):
30
return [["URL", "Status", "Size", "Content Type", "Redirection"]]
31
32
def parse(self, file):
33
with open(file) as fh:
34
rows = list(csv.reader(fh, delimiter=",", quotechar='"'))
35
# Not a dirsearch CSV report
36
if rows[0] != self.new()[0]:
37
raise Exception
38
39
return rows
40
41
@locked
42
def save(self, file, result):
43
rows = self.parse(file)
44
rows.append([result.url, result.status, result.length, result.type, result.redirect])
45
self.write(file, rows)
46
47
def write(self, file, rows):
48
with open(file, "w") as fh:
49
writer = csv.writer(fh, delimiter=",", quotechar='"')
50
for row in rows:
51
writer.writerow(row)
52
53