• requests下载文件并重新上传


    import re
    import requests
    from io import BytesIO
    from django.core.files.uploadedfile import InMemoryUploadedFile
    
    u = "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
    
    
    def match_by_re(str):
        pattern = re.compile(u, re.S)
        res = pattern.findall(string=str)
        if res: return res[0]
    
    
    def get_suffix(url):
        res = url.split('.')
        return res[-1]
    
    
    def get_name_content_type(url):
        suffix = get_suffix(url)
        image_suffix = ['BMP', 'JPG', 'JPEG', 'PNG', 'GIF']
        text_suffix = ['JSON']
        audio_suffix = ['ACT', 'REC', 'AAC', 'SC4', 'DVF', 'MSC', 'WMA', 'MP3', 'WAV']
        name = 'temp.{}'.format(suffix)
        if suffix.upper() in image_suffix:
            return name, 'image/jpeg'
        elif suffix.upper() in text_suffix:
            return name, 'application/json'
        elif suffix.upper() in audio_suffix:
            return name, 'audio/mp3'
    
    
    class DownloadTool():
    
        def __init__(self):
            self.los = {}
    
        def get_stream_data(self, url):
            stream_data = self.los.get(url)
            return stream_data
    
        def stream_download(self, url):
            url = match_by_re(url)
            stream = self.get_stream_data(url)
            if stream:
                return stream
            r = requests.get(url, stream=True)
            self.los[url] = r.content
            return r.content
    
        def get_file_obj(self, url):
            stream = self.stream_download(url)
            fd = BytesIO(stream)
            name, content_type = get_name_content_type(url)
            file_obj = InMemoryUploadedFile(fd, 'url', name, content_type, len(stream), charset='utf-8')
            return file_obj

    file_obj是Django的上传文件对象,可以作为model表中FileField字段的值

  • 相关阅读:
    算法与数据结构(二):排序
    冒泡排序
    责任型模式一:Observer(观察者)模式
    接口型模式(二)Bridge(桥接)模式
    内部排序:归并排序
    mean函数
    cross validation
    内存不足导致的java.lang.OutOfMemoryError: java heap space引出java工程启动参数设置
    angularjs 的hello world
    Nexus私库上传jar文件
  • 原文地址:https://www.cnblogs.com/li1992/p/10438247.html
Copyright © 2020-2023  润新知