Beispiel #1
0
    def run(self, cfg: dict) -> None:
        # from ipdb import set_trace;set_trace()
        course_id = cfg['course_id']
        if not course_id:
            sys.stderr.write("ERROR: couldn't find the target course id\n")
            return
        out_dir = os.path.join(cfg['output_folder'], 'ebook')
        if not os.path.isdir(out_dir):
            try:
                os.makedirs(out_dir)
            except OSError:
                sys.stderr.write("ERROR: couldn't create the output folder {}\n".format(out_dir))
                return
        try:
            dc = get_data_client(cfg)
        except:
            sys.stderr.write("ERROR: invalid geektime account or password\n"
                             "Use '%s <command> login --help' for  help.\n" % sys.argv[0].split(os.path.sep)[-1])
            return

        course_data = dc.get_course_intro(course_id, force=True)
        if int(course_data['column_type']) not in (1, 2):
            sys.stderr.write("ERROR: 该课程不提供文本:%s" % course_data['column_title'])
            return

        # data
        data = dc.get_course_content(course_id, force=cfg['force'])

        if cfg['enable_comments']:
            for post in data:
                post['article_content'] += self._render_comment_html(post['comments'], cfg['comments_count'])

        # source file
        course_data['column_title'] = maker.format_file_name(course_data['column_title'])
        self._render_column_source_files(course_data, data, out_dir, force=cfg['force'])

        # ebook
        if not cfg['source_only']:
            if course_data['update_frequency'] == '全集' and os.path.isfile(os.path.join(out_dir, self._title(course_data)) + '.mobi'):
                sys.stdout.write("{} exists\n".format(self._title(course_data)))
            else:
                make_mobi(source_dir=os.path.join(out_dir, course_data['column_title']), output_dir=out_dir)

        # push to kindle
        if cfg['push'] and not cfg['source_only']:
            fn = os.path.join(out_dir, "{}.mobi".format(self._title(course_data)))
            try:
                send_to_kindle(fn, cfg)
                sys.stdout.write("push to kindle done\n")
            except Exception as e:
                sys.stderr.write("ERROR: push to kindle failed, e={}\n".format(e))
Beispiel #2
0
 def _send_to_kindle(cfg, fn):
     try:
         send_to_kindle(fn, cfg)
     except Exception as e:
         sys.stderr.write("ERROR: push to kindle failed, e={}\n".format(e))
Beispiel #3
0
    def run(self, cfg: dict) -> None:

        course_id = cfg['course_id']
        if not course_id:
            sys.stderr.write("ERROR: couldn't find the target course id\n")
            return
        out_dir = os.path.join(cfg['output_folder'], 'ebook')
        out_dir = os.path.expanduser(out_dir)
        if not os.path.isdir(out_dir):
            try:
                os.makedirs(out_dir)
            except OSError:
                sys.stderr.write(
                    "ERROR: couldn't create the output folder {}\n".format(
                        out_dir))
                return
        try:
            dc = get_data_client(cfg)
        except Exception:
            sys.stderr.write("ERROR: invalid geektime account or password\n"
                             "Use '{} login --help' for  help.\n".format(
                                 sys.argv[0].split(os.path.sep)[-1]))
            return

        course_data = dc.get_course_intro(course_id, force=True)
        if int(course_data['column_type']) not in (1, 2):
            sys.stderr.write("ERROR: 该课程不提供文本:{}".format(
                course_data['column_title']))
            return

        # data
        sys.stdout.write('doing ......\n')
        data = dc.get_course_content(course_id, force=cfg['force'])
        if cfg['enable_comments']:
            for post in data:
                post['article_content'] += self._render_comment_html(
                    post['comments'], cfg['comments_count'])

        # source file
        course_data['column_title'] = Render.format_file_name(
            course_data['column_title'])
        self._render_source_files(course_data,
                                  data,
                                  out_dir,
                                  force=cfg['force'])

        # ebook
        ebook_name = self._title(course_data)
        if not cfg['source_only']:
            fn = os.path.join(out_dir, ebook_name) + '.mobi'
            if course_data['is_finish'] and os.path.isfile(fn):
                sys.stdout.write("{} exists\n".format(ebook_name))
            else:
                src_dir = os.path.join(out_dir, course_data['column_title'])
                make_mobi(source_dir=src_dir, output_dir=out_dir)

        # push to kindle
        if cfg['push'] and not cfg['source_only']:
            fn = os.path.join(out_dir, "{}.mobi".format(ebook_name))
            try:
                send_to_kindle(fn, cfg)
                sys.stdout.write("push to kindle done\n")
            except Exception as e:
                sys.stderr.write(
                    "ERROR: push to kindle failed, e={}\n".format(e))