File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ import os as os
2+ import json as json
3+ import uuid as uuid
4+
5+
6+ class SaveSearchResults (object ):
7+ def __init__ (self , result_json ):
8+ self .result_json = self .__get_as_dict (result_json [0 ])
9+ self .save_results_filename = self .generate_file_name (os .getcwd ())
10+
11+ self .save_data_to_file (self .result_json , self .save_results_filename )
12+
13+ def save_data_to_file (self , data , filename ):
14+ with open (os .path .join (os .getcwd (), f"{ filename } .json" ),
15+ "w" ) as result_writer :
16+ json .dump (data , result_writer , indent = 6 )
17+
18+ def __get_as_dict (self , json_array ):
19+ result_dict = {}
20+ for index , element in enumerate (json_array ):
21+ result_dict [index ] = json_array [index ]
22+ return dict (result_dict )
23+
24+ def generate_file_name (self , directory ):
25+ file_list = os .listdir (directory )
26+ filename = str (uuid .uuid4 ()).replace ("-" , "_" )[:4 ]
27+ while filename in file_list :
28+ filename = str (uuid .uuid4 ()).replace ("-" , "_" )[:4 ]
29+
30+ return filename
You can’t perform that action at this time.
0 commit comments