__init__.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:hackpad-migrator 作者: Stek-io 项目源码 文件源码
def create_pads_from_files(job_id, attachment, email, client_id, client_secret):
    """ For each HTML file in zipped attachment, create a new pad, return the number of
    created pads
    """
    logging.info("Opening attached zip %s." % attachment)
    m = re.search('^.+attachments/(.+)\.zip$', attachment)
    directory = './data/' + m.group(1)
    unzip_attachment(attachment, directory)
    files = os.listdir(directory)

    hackpad = Hackpad(api_scheme = os.getenv('HACKPAD_API_SCHEME') or 'http',
                      api_domain = os.getenv('HACKPAD_API_DOMAIN') or 'hackpad.dev',
                      sub_domain = os.getenv('HACKPAD_SUB_DOMAIN') or '',
                      consumer_key = client_id,
                      consumer_secret = client_secret)

    pads_created = pads_skipped = 0

    for file_name in files:
        file_path = directory + '/' + file_name
        # check if it is really an html file
        file_type = magic.from_file(file_path, mime=True)
        if file_type != 'text/html':
            logging.info('Invalid file type for file %s :%s' % (file_path, file_type))
            continue 

        fh = open(file_path)

        logging.info('importing for %s: %s' % (email, file_name))

        if insert_pad_from_file(job_id, hackpad, fh, file_name, client_id, client_secret):
            pads_created += 1
        else:
            pads_skipped += 1
        fh.close()
    # Check if all files are imported
    if pads_created + pads_skipped != len(files):
        email_error("Not all files were processed", job_id)

    return pads_created, pads_skipped
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号