def open(self): mode = 'a+' if self.__append else 'w+' if self.__filename is None: Common.handle_wrong_cmdargs("Error -- no filename given.", self.__action) try: self.__filehandle = codecs.open(self.__filename, mode, encoding='utf-8') if self.__use_the_force: Common.print_if_verbose("Creating " + self.__filename + " ...", self.__verbose) except: Common.handle_wrong_cmdargs("Error on creating/writing to the file.", self.__action) if self.__filehandle is not None and not self.__print_as_plaintext: self.__add_html_header()
def create_file(self): if self.__filename is None: self.__filename = self.__construct_filename() if os.path.isfile(self.__filename) and not self.__append: if not self.__use_the_force: Common.handle_wrong_cmdargs("File " + self.__filename + " already exists. " + \ "Use -f to overwrite or -a to append.", self.__action) else: try: os.remove(self.__filename) Common.print_if_verbose ("Deleted file " + self.__filename + " ...", \ self.__verbose) except: #TODO improve Common.handle_wrong_cmdargs("Couldn't remove file " + self.__filename + \ ". Abort.", self.__action) elif not os.path.isfile(self.__filename) and self.__append: Common.print_if_verbose("Warning: can't append to " + self.__filename + \ " as it doesn't exist", self.__verbose)
sys.exit(0) cp = CP.CommentPrinter(filename, cf.get_yt_id()) cp.set_append(append) cp.set_force_overwriting(use_the_force) cp.set_disable_timestamps(disable_timestamps) cp.set_disable_links(disable_links) cp.set_print_as_plaintext(print_as_plaintext) cp.set_action_on_error(parser.print_usage) cp.set_enable_embed_video(enable_embed_video) cp.set_total_nof_comments(cf.get_total_nof_comments()) cp.set_req_nof_comments(min(limit, cf.get_total_nof_comments())) cp.create_file() Common.print_if_verbose("There are " + str(cf.get_total_nof_comments()) + \ " comment(s) for this video in total.", verbose) nof_comments = 0 cp.open() while (cf.has_more()): cf.process() comments = cf.get_comments() for comment in comments: cp.write(comment) nof_comments += len(comments) Common.print_if_verbose ("\r\x1b[K" + str(nof_comments) + \ " out of " + str(min(cf.get_total_nof_comments(), limit)) + \ " comments written.", verbose, add_newline=False, flush=True) cf.clear() cp.close()