Exemplo n.º 1
0
def replace_file_content(file_path):
    """
    替换知乎中的中转链接
    :param file_path: 替换的文件路径
    :return:
    """
    file_content = u_file.read_content(file_path)
    # text = '<a href="https://link.zhihu.com/?target=http%3A//index.iresearch.com.cn/pc" class=" wrap external">'
    replace_content = re.sub(
        r'href="[^"]+"', lambda match: unquote(
            match.group(0).replace('https://link.zhihu.com/?target=', '')),
        file_content)
    u_file.write_content(
        r'D:\forme\Git_Projects\python-base\tool\industry-analysis-'
        r'report-replace.html', replace_content)
Exemplo n.º 2
0
def build_content_html():
    target_album_id = 4815905
    track_infos = get_album_track_info_from_cache(target_album_id)
    template_content = u_file.get_content(r'cache\template.html')

    content = ''
    for track_info in track_infos:
        if track_info.get('title').find('坂本真绫') < 0:
            u_log.info('The track is not need. title: {}'.format(track_info.get('title')))
            continue
        if track_info.get('title').find('合集') >= 0:
            u_log.info('The track is collection. title: {}'.format(track_info.get('title')))
            continue
        content += '\n\n<h2>' + track_info.get('title') + '</h2>\n\n'
        content += track_info.get('richIntro')
    template_content.replace('{content}', content)
    u_file.write_content(r'cache\target.html', content)
Exemplo n.º 3
0
def output_course_list(course_data_path: str):
    course_info = u_file.load_json_from_file(course_data_path)

    template = u_file.read_content(r'cache/template.html')
    html_content = '<ul>\n'
    for stage_course in course_info['stageCourses']:
        html_content += '<li>' + stage_course['courseName']

        # 如果有题目的话,列出题目
        questions = stage_course['questions']
        if len(questions) > 0:
            html_content += '\n<ul>'
            for question in questions:
                # question_detail_content = question['detail']['content']
                html_content += '<li>' + question['name'] + '---' + question['summary'] + '\n'
            html_content += '</ul>'
        html_content += '</li>\n'
    html_content += '</ul>'
    template = template.replace('{{title}}', course_info['name'])
    template = template.replace('{{content}}', html_content)
    u_file.write_content(r'cache\output.html', template)
Exemplo n.º 4
0
def output_course_chapter_notes(name):
    course_data_path = r'cache\course-info-{}.json'.format(name)
    course_info = u_file.load_json_from_file(course_data_path)

    content = '# {}\n\n'.format(name)
    log.info('stage_course size: {}'.format(len(course_info['stageCourses'])))
    for stage_course in course_info['stageCourses']:
        chapters = stage_course['chapters']
        content += '## {}\n\n'.format(stage_course['courseName'])
        log.info('course {} chapters size: {}'.format(stage_course['courseName'], len(chapters)))
        if len(chapters) <= 0:
            continue

        # 遍历每一章节
        for chapter in chapters:
            content += '\n### {}\n\n'.format(chapter['name'])

            periods = chapter['periods']
            log.info('chapter: {}, periods size: {}'.format(chapter['name'], len(periods)))
            if len(periods) <= 0:
                continue

            # 遍历每个视频讲解
            for period in periods:
                # 获取笔记并保存
                content += '\n#### {}\n\n'.format(period['name'])
                notes = get_video_notes(period['id'])
                log.info('period: {}, notes size: {}'.format(period['name'], len(notes)))
                if len(notes) <= 0:
                    log.info('The period: {}, notes is empty.'.format(period['name']))
                    continue

                for note in notes:
                    if len(note['content']) <= 5:
                        log.info('The note is short: {}'.format(note['content']))
                        continue
                    content += note['content'] + '\n---------{}\n'.format(note['likeNum'])
        u_file.write_content(r'cache\output-note-{}.md'.format(name), content)
    u_file.write_content(r'cache\output-note-{}.md'.format(name), content)
Exemplo n.º 5
0
def output_course_question(name):
    course_data_path = r'cache\course-info-{}.json'.format(name)
    course_info = u_file.load_json_from_file(course_data_path)

    template = u_file.read_content(r'cache/template.html')
    html_content = ''
    for stage_course in course_info['stageCourses']:
        html_content += '<h1><a href="{}" target="_blank">{}</a></h1>\n'\
            .format(stage_course['url'], stage_course['courseName'])

        # 如果有题目的话,列出题目
        questions = stage_course['questions']
        if len(questions) > 0:
            for question in questions:
                # question_detail_content = question['detail']['content']
                html_content += '<h4><a href="{}" target="_blank">{}</a></h4>\n'\
                    .format(question['url'], question['title'])
                # html_content += question['detail']['content']

    template = template.replace('{{title}}', course_info['name'])
    template = template.replace('{{content}}', html_content)
    u_file.write_content(r'cache\output-title-{}.html'.format(name), template)
Exemplo n.º 6
0
def generate_gitbook_summary(source_dir):
    """
    生成gitbook的summary目录文件,通过遍历文件目录树实现
    :param source_dir: source_dir
    :return:
    """
    sub_file_paths = u_file.get_all_sub_files(source_dir, contain_dir=True)
    summary_content = ''
    sub_file_paths.sort()
    exclude_paths = [
        '.git', '.idea', 'assets', 'temp', 'node_modules', '_book'
    ]
    for sub_file_path in sub_file_paths:
        relative_path = sub_file_path.replace(source_dir + '\\', '')
        # 过滤掉非markdown文件
        ignore = False
        for exclude_path in exclude_paths:
            if sub_file_path.find(exclude_path) >= 0:
                ignore = True
        if ignore:
            continue

        path_depth = relative_path.count('\\')
        menu = '  ' * path_depth

        if os.path.isfile(sub_file_path):
            if sub_file_path.find('.md') >= 0:
                menu += '- [{}]({})'.format(
                    os.path.split(relative_path)[1].replace('.md', ''),
                    relative_path)
            else:
                continue
        else:
            menu += '- {}'.format(relative_path.split('\\')[-1])
        summary_content += menu + "\n"
    u_file.write_content(r'cache\result.md', summary_content)
    print(summary_content)
Exemplo n.º 7
0
def test_read_write_file():
    content = '--something--'
    file_path = r'cache\test.dat'
    file_path = os.path.abspath(file_path)
    u_file.write_content(file_path, content)
    u_unittest.assert_eq(content, u_file.read_content(file_path))