Skip to content

Commit a3f2517

Browse files
committed
Issue #62: Report to file (dump json object to file).
1 parent 32ed482 commit a3f2517

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

dirhunt/crawler.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
import json
23
import multiprocessing
34
from concurrent.futures import ThreadPoolExecutor
45
from concurrent.futures.thread import _python_exit
@@ -13,6 +14,7 @@
1314
from dirhunt.cli import random_spinner
1415
from dirhunt.crawler_url import CrawlerUrl
1516
from dirhunt.exceptions import EmptyError, RequestError, reraise_with_stack
17+
from dirhunt.json_report import JsonReportEncoder
1618
from dirhunt.sessions import Sessions
1719
from dirhunt.sources import Sources
1820
from dirhunt.url_info import UrlsInfo
@@ -181,6 +183,9 @@ def close(self):
181183
self.shutdown(False)
182184
atexit.unregister(_python_exit)
183185

186+
def create_report(self):
187+
json.dump(self.json(), open(self.to_file, 'w'), cls=JsonReportEncoder, indent=4, sort_keys=True)
188+
184189
def json(self):
185190
return {
186191
'current_processed_count': self.current_processed_count,

dirhunt/json_report.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from json import JSONEncoder
2+
3+
4+
class JsonReportEncoder(JSONEncoder):
5+
def default(self, obj):
6+
if isinstance(obj, (set, frozenset)):
7+
return list(obj)
8+
elif hasattr(obj, 'json'):
9+
return obj.json()
10+
return super(JsonReportEncoder, self).default(obj)

dirhunt/management.py

+2
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ def hunt(urls, threads, exclude_flags, include_flags, interesting_extensions, in
158158
crawler.print_urls_info()
159159
if not sys.stdout.isatty():
160160
output_urls(crawler, stdout_flags)
161+
if to_file:
162+
crawler.create_report()
161163

162164

163165
def main():

0 commit comments

Comments
 (0)