zoukankan      html  css  js  c++  java
  • 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字段的值

  • 相关阅读:
    JavaScript内存泄漏(转)
    深入理解JavaScript的变量作用域(转)
    Spring MVC 配置(备忘)
    French fashion in Paris
    Windows Server 2003、 WindowsXP, 中和 Windows 2000 中的缓存凭据安全
    doc.txt.exe病毒。
    Exchange server 增加RBL
    无以穷尽的工作。。。
    windows SBS Exchange 数据库恢复
    VLAN介绍
  • 原文地址:https://www.cnblogs.com/li1992/p/10438247.html
Copyright © 2011-2022 走看看