173/174-路飞14&15-课程详情接口/前台&区间过滤

16 篇文章 0 订阅
9 篇文章 0 订阅

今日内容

1 课程详情接口,课程章节接口

1 查询所有课程接口
2 查询单个课程接口
	-继续再查询所有接口上写
    -再写一个视图类,单独写查询课程详情接口

4 写课程章节接口,每个章节下有很多课程
	Linux从入门到精通
    	第一章节:入门
        	1 安装环境
            2 配置环境
            3 放弃
         第二章节:入土
        	1 哇肯恶搞
            2 挖坑
    
    
    
    
   Python从入门到精通
    	第一章节:入门
        	1 安装环境
            2 配置环境
            3 放弃
         第二章节:入土
        	1 哇肯恶搞
            2 挖坑

课程详情接口

class CourseView(GenericViewSet, ListModelMixin,RetrieveModelMixin):
    queryset = Course.objects.filter(is_delete=False, is_show=True).order_by('-orders')
    serializer_class =CourseSerializer
    filter_backends = [OrderingFilter,DjangoFilterBackend]
    ordering_fields = ['id', 'price', 'students']
    filter_fields = ['course_category']
    pagination_class =BasicPagination

课程章节接口(views.py)

class ChapterViewSet(GenericViewSet,ListModelMixin):
    queryset = CourseChapter.objects.filter(is_show=True,is_delete=False).order_by('orders')
    serializer_class = CourseChapterSerializer

    # 加上过滤功能,按课程的id号来过滤
    filter_backends = [DjangoFilterBackend]
    filter_fields = ['course']
序列化类
## 课时序列化类
class CourseSectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseSection
        fields='__all__'


# 课程章节序列化类
class CourseChapterSerializer(serializers.ModelSerializer):
    # 子序列化,课时是多条,一定要写many=True
    coursesections=CourseSectionSerializer(many=True)
    class Meta:
        model = CourseChapter
        fields=['course','chapter','name','summary','pub_date','coursesections']
路由
router.register('chapters',ChapterViewSet,'ChapterViewSet')

2 课程详情前台

# 取路径中的值
     mounted() {
                //http://localhost:8080/course/detail/79?name=lqz&age=18
                console.log(this.$route.params) //打印出{pk:79}
                console.log(this.$route.query)//打印出{name:lqz,age:18}
        },

3 七牛云对象存储托管视频

1 视频文件,图片文件之前保存在自己服务器
2 搭建自己的存储服务器(fastdfs,go-fastdfs)
	-https://sjqzhang.github.io/go-fastdfs/usage.html#python
	-docekr 拉起一个来 http://10.1.5.9:8080/group1/upload
    import requests
    url = 'http://10.1.5.9:8080/group1/upload'
    files = {'file': open('report.xls', 'rb')}
    options={'output':'json','path':'','scene':''} #参阅浏览器上传的选项
    r = requests.post(url,data=options, files=files)
    print(r.text)
    
3 第三方云存储(阿里云oss,七牛云存储。。。。)
	七牛云托管视频(go语言的践行者)
# from qiniu import Auth, put_file, etag
# import qiniu.config
# #需要填写你的 Access Key 和 Secret Key
# access_key = '-BoMxfk8dxgDq87mA-qLkgmI_AHYfF27tOFan_NJ'
# secret_key = 'Wijh6Fg6gedtBqoqKElkxrrlySoiVWbFSfQ6I-qM'
# #构建鉴权对象
# q = Auth(access_key, secret_key)
# #要上传的空间
# bucket_name = 'lqz'
# #上传后保存的文件名
# key = 'lqz.mp4'
# #生成上传 Token,可以指定过期时间等
# token = q.upload_token(bucket_name, key, 3600)
# #要上传文件的本地路径
# localfile = './6-测试功能.mp4'
# ret, info = put_file(token, key, localfile)
# print(info)
# assert ret['key'] == key
# assert ret['hash'] == etag(localfile)



# 删除视频
# from qiniu import Auth
# from qiniu import BucketManager
# access_key = '-BoMxfk8dxgDq87mA-qLkgmI_AHYfF27tOFan_NJ'
# secret_key = 'Wijh6Fg6gedtBqoqKElkxrrlySoiVWbFSfQ6I-qM'
# #初始化Auth状态
# q = Auth(access_key, secret_key)
# #初始化BucketManager
# bucket = BucketManager(q)
# #你要测试的空间, 并且这个key在你空间中存在
# bucket_name = 'lqz'
# key = 'lqz.mp4'
# #删除bucket_name 中的文件 key
# ret, info = bucket.delete(bucket_name, key)
# print(info)

# import requests
#
#
# res=requests.get('https://vd3.bdstatic.com/mda-kkena9xzt4hmb7b7/sc/cae_h264_clips/1605430730/mda-kkena9xzt4hmb7b7.mp4?auth_key=1610529585-0-0-14a58ce4cca097b0a5c823cc506d95da&bcevod_channel=searchbox_feed&pd=1&pt=3&abtest=8656_1')
# with open('致命诱惑.mp4','wb') as f:
#     for line in res.iter_content():
#         f.write(line)






'''

通过该程序下载的视频和音频是分成连个文件的,没有合成,
视频为:视频名_video.mp4
音频为:视频名_audio.mp4
修改url的值,换成自己想下载的页面节课
'''

# 导入requests模块,模拟发送请求
import requests
# 导入json
import json
# 导入re
import re

# 定义请求头
headers = {
    'Accept': '*/*',
    'Accept-Language': 'en-US,en;q=0.5',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36'
}


# 正则表达式,根据条件匹配出值
def my_match(text, pattern):
    match = re.search(pattern, text)
    print(match.group(1))
    print()
    return json.loads(match.group(1))


def download_video(old_video_url, video_url, audio_url, video_name):
    headers.update({"Referer": old_video_url})
    print("开始下载视频:%s" % video_name)
    video_content = requests.get(video_url, headers=headers)
    print('%s视频大小:' % video_name, video_content.headers['content-length'])
    audio_content = requests.get(audio_url, headers=headers)
    print('%s音频大小:' % video_name, audio_content.headers['content-length'])
    # 下载视频开始
    received_video = 0
    with open('%s_video.mp4' % video_name, 'ab') as output:
        while int(video_content.headers['content-length']) > received_video:
            headers['Range'] = 'bytes=' + str(received_video) + '-'
            response = requests.get(video_url, headers=headers)
            output.write(response.content)
            received_video += len(response.content)
    # 下载视频结束
    # 下载音频开始
    audio_content = requests.get(audio_url, headers=headers)
    received_audio = 0
    with open('%s_audio.mp4' % video_name, 'ab') as output:
        while int(audio_content.headers['content-length']) > received_audio:
            # 视频分片下载
            headers['Range'] = 'bytes=' + str(received_audio) + '-'
            response = requests.get(audio_url, headers=headers)
            output.write(response.content)
            received_audio += len(response.content)
    # 下载音频结束
    return video_name


if __name__ == '__main__':
    # 换成你要爬取的视频地址
    url = 'https://www.bilibili.com/video/BV1TA411H7gQ'
    # 发送请求,拿回数据
    res = requests.get(url, headers=headers)
    # 视频详情json
    playinfo = my_match(res.text, '__playinfo__=(.*?)</script><script>')
    # 视频内容json
    initial_state = my_match(res.text, r'__INITIAL_STATE__=(.*?);\(function\(\)')
    # 视频分多种格式,直接取分辨率最高的视频 1080p
    video_url = playinfo['data']['dash']['video'][0]['baseUrl']
    # 取出音频地址
    audio_url = playinfo['data']['dash']['audio'][0]['baseUrl']
    video_name = initial_state['videoData']['title']
    print('视频名字为:video_name')
    print('视频地址为:', video_url)
    print('音频地址为:', audio_url)
    download_video(url, video_url, audio_url, video_name)

4 区间过滤

1 查询课程价格大于30小于90的课程
http://127.0.0.1:8000/course/free/?price_ge=30&price_lt=90
2 写一个过滤类,配置在视图类中
filter_backends = [SearchByPrice]




 3 通过扩写django-filter来实现该功能
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值