import reimport requestsfrom io import BytesIOfrom django.core.files.uploadedfile import InMemoryUploadedFileu = "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字段的值
requests下载文件并重新上传
原文地址:https://www.cnblogs.com/li1992/p/10438247.html