def export(data, f_name, f_type, scrape): """ Write data to either CSV or JSON. Calls external module methods and previously defined private and public methods: InitializeDirectory.make_type_directory() Export._get_filename_extension() Export.write_json() Export.write_csv() Parameters ---------- data: dict Dictionary of scrape data f_name: str Filename f_type: str File type (.csv or .json) scrape: str Scrape type ("subreddits", "redditors", or "comments") Returns ------- None """ InitializeDirectory.make_type_directory(scrape) filename = Export._get_filename_extension(f_name, f_type, scrape) Export.write_json(data, filename) \ if f_type == eo[1] \ else Export.write_csv(data, filename)
def test_make_type_directory(self): scrape = "subreddit" InitializeDirectory.make_type_directory(scrape) assert True \ if os.path.isdir("../scrapes/%s/%s" % (date, scrape)) == True \ else False
def write_structured_comments(data, f_name): """ Write structured comments to JSON by using the custom JSONEncoder class with the `cls` parameter within `json.dumps()`. Parameters ---------- data: dict Dictionary of scrape data f_name: str String denoting the filename Returns ------- None """ InitializeDirectory.make_type_directory("comments") filename = Export._get_filename_extension(f_name, "json", "comments") with open(filename, "w", encoding = "utf-8") as results: json.dump(data, results, indent = 4, cls = EncodeNode)