requests下载文件并重新上传
Posted li1992
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了requests下载文件并重新上传相关的知识,希望对你有一定的参考价值。
import re import requests from io import BytesIO from django.core.files.uploadedfile import InMemoryUploadedFile u = "http[s]?://(?:[a-zA-Z]|[0-9]|[[email protected]&+]|[!*(),]|(?:%[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下载文件并重新上传的主要内容,如果未能解决你的问题,请参考以下文章