#!/usr/bin/env python3  
# -*- coding: utf-8 -*-  
"""
@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
@AUTHOR: ASJRD018
@NAME: langer
@software: PyCharm
@DATE: 2019/9/4 17:18
@Version: python3.6
@MODIFY DECORD:ansjer dev
@file: CommonService.py
@Contact: chanjunkai@163.com
"""
from random import Random  # 用于生成随机码
import hashlib
from urllib.request import urlopen


# 通过url获取文件md5
class CommonService:
    @staticmethod
    def get_remote_md5_sum(url, max_file_size=100 * 1024 * 1024):
        remote = urlopen(url)
        hash = hashlib.md5()
        total_read = 0
        while True:
            data = remote.read(4096)
            total_read += 4096

            if not data or total_read > max_file_size:
                break

            hash.update(data)

        return hash.hexdigest()

    @staticmethod
    def RandomStr(randomlength=8, number=False):
        str = ''
        if number == False:
            characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \
                           'tUuVvWwXxYyZz0123456789'
        else:
            characterSet = '0123456789'

        length = len(characterSet) - 1

        random = Random()
        for index in range(randomlength):
            str += characterSet[random.randint(0, length)]

        return str
# print(CommonService.get_remote_md5_sum(url='http://www.dvema.com/web/asdf'))