def _determine_export(args, data, f_name): """ Export either structured or raw comments. Calls a public method from an external module: Export.export() Parameters ---------- args: Namespace Namespace object containing all arguments that were defined in the CLI data: dict Dictionary containing all scraped data f_name: str String denoting the filename Returns ------- None """ if args.raw: export_status = "Exporting %s comments in raw format." % data["scrape_settings"]["n_results"] Halo().info(export_status) logging.info(export_status) Export.export(data, f_name, "json", "comments") else: export_status = "Exporting %s comments in structured format." % data["scrape_settings"]["n_results"] Halo().info(export_status) logging.info(export_status) Export.write_structured_comments(data, f_name)
def test_write_structured_comments(self): test_nodes = [] first_node = MockNode("test one") EncodeNode().encode(first_node) second_node = MockNode("test two") EncodeNode().encode(second_node) third_node = MockNode("test three") EncodeNode().encode(third_node) first_node.replies.append(second_node) first_node.replies[0].replies.append(third_node) test_nodes.append(first_node) Export.write_structured_comments(test_nodes, "structured_comments_test") with open(f"../scrapes/{date}/comments/structured_comments_test.json", "r", encoding = "utf-8") as test_json: test_dict = json.load(test_json) assert test_dict == [{'string': 'test one', 'replies': [{'string': 'test two', 'replies': [{'string': 'test three', 'replies': []}]}]}]