# coding=UTF-8
import json
import os.path
from django.conf import settings
import jieba
from jieba.analyse import tfidf
from rest_framework.generics import GenericAPIView
from wordcloud import WordCloud
import pandas as pd
from sqlalchemy import create_engine
from app.models import MovieDetail, MovieComment
from user.models import UserToken, UserFavorites, UserRating
from utils.auth import LoginAuth
from utils.permission import MyPermission
from rest_framework import serializers, mixins
from rest_framework.viewsets import ModelViewSet
from rest_framework.decorators import action, api_view # 路由
from rest_framework.response import Response # Json返回
from rest_framework.pagination import PageNumberPagination # 分页
from rest_framework.filters import SearchFilter # 模糊搜索(过滤)
from django_pandas.io import read_frame # pandas.DataFrame
from app.recommend_base_content import RecommendBaseContent
# mysql驱动引擎
engine = create_engine('mysql+pymysql://root:123456@localhost:3306/douban_movie')
sql1 = 'select * from user_favorites;'
user_favor = pd.read_sql(sql1, engine)
sql2 = 'select * from user_rating;'
user_rating = pd.read_sql(sql2, engine)
def getMovieTypeDict(df):
"""统计每种类型的电影个数"""
typeList = [] # 存放电影种类的列表
for types in df['types'].values.tolist():
typeList += types.split(',')
# 统计每一类电影的个数
type_dict = dict()
for type in typeList:
if type in type_dict:
type_dict[type] += 1
else:
type_dict[type] = 1
return type_dict
def getMovieCountryDict(df):
"""统计每个制片国家的电影个数"""
countryList = [] # 存放电影种类的列表
for countries in df['countries'].values.tolist():
countryList += countries.split(',')
# 统计每一类电影的个数
country_dict = dict()
for country in countryList:
if country in country_dict:
country_dict[country] += 1
else:
country_dict[country] = 1
return country_dict
def getMovieLangDict(df):
"""统计每种语言的电影个数"""
langList = [] # 存放电影种类的列表
for lang in df['lang'].values.tolist():
langList += lang.split(',')
# 统计每一类电影的个数
lang_dict = dict()
for lang in langList:
if lang in lang_dict:
lang_dict[lang] += 1
else:
lang_dict[lang] = 1
return lang_dict
class MovieInfoSerializer(serializers.ModelSerializer):
class Meta:
model = MovieDetail
fields = '__all__'
class MovieInfoView(ModelViewSet):
""" movie信息 """
queryset = MovieDetail.objects.all()
serializer_class = MovieInfoSerializer
authentication_classes = [LoginAuth] # 登录认证
# permission_classes = [MyPermission] # 权限
pagination_class = None # 配置自定义的分页器
filter_backends = [SearchFilter]
search_fields = ['title']
# coerce_float=True: 尝试将值转换为非字符串,将非数字对象(如decimal.Decimal)转化为浮点类型
df = read_frame(qs=queryset, coerce_float=True)
df['release_date'] = pd.to_datetime(df['release_date'])
type_dict = getMovieTypeDict(df)
country_dict = getMovieCountryDict(df)
lang_dict = getMovieLangDict(df)
# 推荐类
recommendContent = RecommendBaseContent(df.copy(), user_favor, user_rating)
# 127.0.0.1:8000/api/movie/info/homeInfo/ ---->get请求
@action(methods=['GET'], detail=False)
def homeInfo(self, request):
columns = ['id', 'title', 'rate', 'release_date', 'comment_len', 'cover']
df = self.df.copy()[columns]
# 《天空之城》
home_movie = df.loc[df['title'] == '天空之城']
home_cover = home_movie['cover']
# 评分最高电影
# 查找rate最高的那一行数据
highest_movie = df.loc[df['rate'].idxmax()]
highest_movie = highest_movie.to_dict()
# 最新电影
# 查找上映日期最新的那一行数据
latest_movie = df.loc[df['release_date'].idxmax()]
latest_movie = latest_movie.to_dict()
# 近期热门电影
new_date = df['release_date'].max() # 最新电影上映日期
old_date = new_date - pd.Timedelta(60, unit='d') # 最新日期往前60天
recent_df = df[df['release_date'].between(old_date, new_date)] # 取这区间的电影
hot_movie = recent_df.loc[recent_df['comment_len'].idxmax()] # 用评论数来定义热门电影
hot_movie = hot_movie.to_dict()
return Response(
{'msg': 'OK', 'home_cover': home_cover, 'highest': highest_movie, 'latest': latest_movie, 'hot': hot_movie})
# 127.0.0.1:8000/api/movie/info/getSearchTitles/ ---->get请求
@action(methods=['GET'], detail=False)
def getSearchTitles(self, request):
"""根据输入的关键词搜索相关电影名称"""
keyword = request.GET.get('keyword')
if not keyword:
return Response({'msg': '请输入关键字', 'res_code': 0, 'titles': []})
titles = self.df[self.df['title'].str.contains(keyword)]['title']
if titles.empty:
return Response({'msg': '没有相关电影', 'res_code': 0, 'titles': []})
titles = titles.values.tolist()
return Response({'msg': 'OK', 'res_code': 1, 'titles': titles})
# 127.0.0.1:8000/api/movie/info/allMovieTypes/ ---->get请求
@action(methods=['GET'], detail=False)
def allMovieTypes(self, request):
# 按每种类型的个数从多到少排序
type_dict = sorted(self.type_dict.items(), key=lambda x: x[1], reverse=True)
# 取出全部类型名称
types = map(lambda x: x[0], type_dict)
if types:
return Response({'msg': 'OK', 'res_code': 1, 'types': types})
else:
return Response({'msg': 'Fail', 'res_code': 0, 'types': []})
# 127.0.0.1:8000/api/movie/info/allMovieYears/ ---->get请求
@action(methods=['GET'], detail=False)
def allMovieYears(self, request):
years = self.df['year'].unique().tolist()
years = sorted(years, reverse=True)
if years:
return Response({'msg': 'OK', 'res_code': 1, 'years': years})
else:
return Response({'msg': '未查到数据', 'res_code': 0, 'years': []})
# 127.0.0.1:8000/api/movie/info/allMovieCountries/ ---->get请求
@action(methods=['GET'], detail=False)
def allMovieCountries(self, request):
"""获取全部制片国家"""
# 按每种制片国家的个数从多到少排序
country_dict = sorted(self.country_dict.items(), key=lambda x: x[1], reverse=True)
# 取出全部制片国家名称
movie_countries = map(lambda x: x[0], country_dict)
if movie_countries:
return Response({'msg': 'OK', 'res_code': 1, 'countries': movie_countries})
else:
return Response({'msg': '未查到数据', 'res_code': 0, 'countries': []})
# 127.0.0.1:8000/api/movie/info/allMovieLang/ ---->get请求
@action(methods=['GET'], detail=False)
def allMovieLang(self, request):
"""获取全部语言种类"""
# 按每种语言的个数从多到少排序
lang_dict = sorted(self.lang_dict.items(), key=lambda x: x[1], reverse=True)
# 取出全部语言名称
movie_langs = map(lambda x: x[0], lang_dict)
if movie_langs:
return Response({'msg': 'OK', 'res_code': 1, 'langs': movie_langs})
else:
return Response({'msg': '未查到数据', 'res_code': 0, 'langs': []})
# 127.0.0.1:8000/api/movie/info/recommendBaseContent/ ---->GET请求
@action(methods=['GET']
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于Django的豆瓣电影数据可视化系统.zip (187个子文件)
all_movies2.csv 4.29MB
all_movies.csv 2MB
hot_movies.csv 242KB
486c592449d043239e69650478499e09.jfif 339KB
0ee560da098141ed90933a919d4726e8.jpg 450KB
43ff0225594d48f181720bf60c6b868f.jpg 429KB
da47410b97d14a38bdd4afc012d7904c.jpg 310KB
45289b17121542d4a7fedba199935578.jpg 171KB
6b34fab13278428890b0d9c45c32f6ec.jpg 171KB
fc7803f5a7ab4422bb9be5ab2df50517.jpg 171KB
img006.jpg 120KB
5ce1e4eaa5bc4db3b83098bfc941b17d.jpg 120KB
465a2c62b2284cea9066d9ea9b79ad05.jpg 120KB
d8553b3b2def4e3e89467e276cc6d44e.jpg 120KB
17e2afbde9b94c48a166761a2c10c97c.jpg 120KB
d0b9e993d6294f8ca7e6195f8feddb93.jpg 120KB
f3c17a0a5a8143c6a5d3cc13b62ef70e.jpg 120KB
281cf19f1be04f2d91098ffc776416a8.jpg 70KB
4428647da26947b7be202a8fa7ac2100.jpg 40KB
e27644a48fd74353b8fd3e23f699d558.jpg 27KB
ed5f00fd113947788be5908f570b5a48.jpg 15KB
user.js 11KB
index.js 7KB
movie.js 5KB
user.js 3KB
index.js 3KB
movie.js 3KB
request.js 2KB
permission.js 2KB
ResizeHandler.js 1KB
app.js 1KB
getters.js 1022B
main.js 854B
FixiOSBug.js 659B
settings.js 610B
auth.js 553B
index.js 386B
validate.js 366B
settings.js 313B
index.js 297B
get-page-title.js 260B
table.js 181B
index.js 142B
404.png 96KB
wc_img.png 44KB
wc_img.png 39KB
wc_img.png 38KB
wc_img.png 37KB
wc_img.png 36KB
wc_img.png 36KB
wc_img.png 36KB
wc_img.png 30KB
wc_img.png 29KB
BNLvob5ope.png 10KB
404_cloud.png 5KB
views.py 31KB
spider.py 19KB
views.py 13KB
recommend_base_content.py 10KB
itemCF.py 9KB
userCF.py 8KB
settings.py 5KB
random_user_rating.py 4KB
0001_initial.py 3KB
models.py 3KB
spider_ip.py 2KB
0001_initial.py 2KB
0002_alter_userfavorites_create_time_and_more.py 2KB
models.py 2KB
test4.py 2KB
customRenderer.py 1KB
test3.py 1KB
urls.py 919B
auth.py 882B
permission.py 851B
test2.py 763B
manage.py 691B
exception.py 676B
urls.py 647B
urls.py 637B
test.py 441B
wsgi.py 419B
asgi.py 419B
apps.py 146B
apps.py 144B
admin.py 66B
admin.py 66B
tests.py 63B
tests.py 63B
__init__.py 48B
__init__.py 0B
__init__.py 0B
__init__.py 0B
__init__.py 0B
views.cpython-38.pyc 20KB
spider.cpython-38.pyc 11KB
views.cpython-38.pyc 9KB
recommend_base_content.cpython-38.pyc 8KB
itemCF.cpython-38.pyc 5KB
userCF.cpython-38.pyc 5KB
共 187 条
- 1
- 2
资源评论
- mzm54662024-03-27感谢大佬分享的资源给了我灵感,果断支持!感谢分享~
我慢慢地也过来了
- 粉丝: 7447
- 资源: 4013
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 数据库管理工具:dbeaver-ce-23.1.5-stable.x86-64.rpm
- 以下是一些适用于英语六级作文的万能句型模板,涵盖了引言、正文和结论部分的各类表达方式.docx
- MATLAB中的非线性规划
- 进行C语言面试资格确认是招聘过程中一个重要的步骤,目的是确保候选人具备足够的C语言编程能力和知识.docx
- Java 轻量级的集群负载均衡设计
- 纹身师个人网站模板.jpg
- 在C语言中,连接两个字符串(即将一个字符串附加到另一个字符串的末尾)通常可以使用标准库中的 `strcat` 函数.docx
- 数据库管理工具:dbeaver-ce-23.1.1-stable.x86-64.rpm
- 以下是几个具体竞赛题目的详细解答,包括建模思路、方法和步骤 .docx
- 一份关于全国大学生建模大赛的相关教程!!
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功