Skip to content

Commit

Permalink
新增bangumiID,添加番剧信息时可以自动从bangumi补全部分信息
Browse files Browse the repository at this point in the history
  • Loading branch information
JxiaoC committed Apr 1, 2024
1 parent cac1c9e commit 5b9f3a8
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 7 deletions.
1 change: 1 addition & 0 deletions animeMusic_server/anime_music/apps/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
register.register_group_urls('/god/api/v1', [
('/anime/list', god.AnimeListHeader),
('/anime/add', god.AnimeAddHeader),
('/anime/search_bangumi', god.AnimeSearchBangumiHeader),
('/anime/(save|del)/([0-9a-f]{24})', god.AnimeHeader),
('/anime/upload/(logo|bg)/([0-9a-f]{24})', god.AnimeUploadHeader),

Expand Down
8 changes: 8 additions & 0 deletions animeMusic_server/anime_music/apps/app/god.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from helper import tietuku
from helper import file_server
from helper import ftp
from lib import bangumi
from aliyun import oss
from tornado import gen
from tornado.concurrent import run_on_executor
Expand Down Expand Up @@ -98,6 +99,13 @@ def post(self):
self.write({'code': 0, 'msg': 'ok'})


class AnimeSearchBangumiHeader(turbo.app.BaseHandler):
def post(self):
title = self.get_argument('title', '')
res = bangumi.search(title)
self.write({'code': 0, 'msg': 'ok', 'res': res})


class AnimeHeader(turbo.app.BaseHandler):
def post(self, type, id):
if not id or not ObjectId.is_valid(id):
Expand Down
101 changes: 94 additions & 7 deletions animeMusic_server/anime_music/static/god/anime.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,37 @@
width: auto !important;
}

.layui-upload-img{
.layui-upload-img {
min-height: 100px;
border: 1px solid #ccc;
}

.bangumi.items {
padding: 5px;
}

.bangumi .item {
display: inline-block;
overflow: hidden;
width: 100px;
position: relative;
cursor: pointer;
}

.bangumi img {
width: 100%;
}

.bangumi .item .title {
font-size: 12px;
height: 18px;
position: absolute;
bottom: 0;
background: #00000047;
color: #fff;
width: 100%;
text-align: center;
}
</style>
</head>
<body>
Expand All @@ -43,7 +70,8 @@

<div class="anime-add">
<div class="layui-inline">
<input class="layui-input" name="title" autocomplete="off" placeholder="新的anime名称" style="width: 210px;">
<input class="layui-input" name="title" autocomplete="off" placeholder="新的anime名称"
style="width: 210px;">
</div>
<button class="layui-btn" data-type="reload" id="search" onclick="add(this);">添加</button>
</div>
Expand Down Expand Up @@ -71,7 +99,20 @@
<div class="layui-form-item">
<label class="layui-form-label">名称</label>
<div class="layui-input-block">
<input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="名称" class="layui-input">
<input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="名称"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">BangumiID</label>
<div class="layui-input-block">
<div class="layui-input-inline" style="width: 100px;">
<input type="text" name="bangumi_id" placeholder="对应Bangumi的番剧id" autocomplete="off"
class="layui-input">
</div>
<div class="layui-input-inline" style="width: 100px;">
<button class="layui-btn" id="search_bangumi" onclick="search_bangumi();">查询</button>
</div>
</div>
</div>
<div class="layui-form-item">
Expand Down Expand Up @@ -121,6 +162,7 @@

<script>
var g_anime_info = {};
var g_bangumi_info = {};

layui.use('table', function () {
var table = layui.table
Expand Down Expand Up @@ -176,11 +218,9 @@
var data = obj.data;
if (obj.event === 'edit') {
edit(data);
}
else if (obj.event === 'music') {
} else if (obj.event === 'music') {
music(data._id);
}
else if (obj.event === 'del') {
} else if (obj.event === 'del') {
del(data._id);
}
});
Expand Down Expand Up @@ -327,6 +367,53 @@
});
}

function search_bangumi() {
if ($('#search_bangumi').text() == '查询中') return;
$('#search_bangumi').text('查询中')
var title = $('.anime-info input[name="title"]').val();
$.post("/god/api/v1/anime/search_bangumi", {title: title}, function (res) {
console.log(res)
var html = '<div class="bangumi items">'
g_bangumi_info = {}
for (var f of res.res) {
g_bangumi_info[f['id']] = f
html += '\n<div class="item" data-id="' + f['id'] + '">\n'
html += '\n<img src="' + f['img'] + '"/>\n'
html += '\n<div class="title">' + f['name_cn'] + '(' + f['name'] + ')' + '</div>\n'
html += '\n</div>\n'
}
html += '\n</div>\n'
layer.open({
type: 1, // page 层类型
area: ['855px', '348px'],
title: '查询结果',
shade: 0.6, // 遮罩透明度
shadeClose: true, // 点击遮罩区域,关闭弹层
maxmin: true, // 允许全屏最小化
anim: 0, // 0-6 的动画形式,-1 不开启
content: html,
success: function () {
$('.bangumi .item').unbind().click(function(){
var bangumi_id = $(this).data('id');
select_bangumi_id(bangumi_id);
layer.closeAll();
})
}
});
$('#search_bangumi').text('查询')
});
}

function select_bangumi_id(id) {
$('.anime-info input[name="bangumi_id"]').val(id);
if(confirm('是否自动补全信息')){
$('.anime-info input[name="year"]').val(g_bangumi_info[id]['year']);
$('.anime-info input[name="month"]').val(g_bangumi_info[id]['month']);
$('.anime-info textarea[name="desc"]').val(g_bangumi_info[id]['desc']);
}
g_bangumi_info = {};
}

layui.use('form', function () {
var form = layui.form;
form.render();
Expand Down
1 change: 1 addition & 0 deletions animeMusic_server/helper/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def format_music_info(info):
info['type'] = info.get('type', '其他')
info['author'] = info.get('author', '未知')
info['recommend'] = True if info.get('recommend', False) else False
info['bangumi_id'] = info.get('bangumi_id', 0)
return info


Expand Down
34 changes: 34 additions & 0 deletions animeMusic_server/lib/bangumi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# bangumi API
import datetime
import json
from urllib.parse import quote
from cPython import cPython as cp

default_ua = 'xiaoc/AnimeMusic/1.0.0 (Web) (https://github.com/JxiaoC/animeMusic)'
host = 'https://api.bgm.tv'


def search(keywords: str):
url = '%s/search/subject/%s?type=2&max_results=25&responseGroup=large' % (host, quote(keywords))
html = cp.get_html(url, headers={'user-agent': default_ua})
json_data = json.loads(html)
res = []
for f in json_data.get('list', []):
if f.get('air_date').startswith('0000'):
air_date = datetime.datetime(1970, 1, 1)
else:
air_date = datetime.datetime.strptime(f.get('air_date'), '%Y-%m-%d')
res.append({
'id': f['id'],
'name': f.get('name', ''),
'name_cn': f.get('name_cn', ''),
'desc': f.get('summary', ''),
'year': air_date.year,
'month': air_date.month,
'img': f.get('images', {}).get('medium', ''),
})
return res


if __name__ == '__main__':
print(search('缘之空'))
2 changes: 2 additions & 0 deletions animeMusic_server/models/anime_music/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AnimeList(Model):
atime: 添加时间,时间戳格式
year: anime发行年份
month: anime发行月份
bangumi_id: 对应bangumi番剧id
"""
field = {
'title': (str, ''),
Expand All @@ -26,6 +27,7 @@ class AnimeList(Model):
'logo': (str, ''),
'year': (int, 0),
'month': (int, 0),
'bangumi_id': (int, 0),
'tietukuname': (str, ''),
}

Expand Down

0 comments on commit 5b9f3a8

Please sign in to comment.