码迷,mamicode.com
首页 > Web开发 > 详细

requests下载文件并重新上传

时间:2019-02-26 17:42:07      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:对象   spl   wav   com   mod   pat   files   from   ret   

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字段的值

requests下载文件并重新上传

标签:对象   spl   wav   com   mod   pat   files   from   ret   

原文地址:https://www.cnblogs.com/li1992/p/10438247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!