今日内容
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
filter_backends = [DjangoFilterBackend]
filter_fields = ['course']
序列化类
class CourseSectionSerializer(serializers.ModelSerializer):
class Meta:
model = CourseSection
fields='__all__'
class CourseChapterSerializer(serializers.ModelSerializer):
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
-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语言的践行者)
'''
通过该程序下载的视频和音频是分成连个文件的,没有合成,
视频为:视频名_video.mp4
音频为:视频名_audio.mp4
修改url的值,换成自己想下载的页面节课
'''
import requests
import json
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)
playinfo = my_match(res.text, '__playinfo__=(.*?)</script><script>')
initial_state = my_match(res.text, r'__INITIAL_STATE__=(.*?);\(function\(\)')
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来实现该功能