1. 优化导入PTPP历史数据

This commit is contained in:
ngfchl
2022-09-13 18:09:11 +08:00
parent e8b4b30aa4
commit ff1d2fa5d7
10 changed files with 416 additions and 44 deletions

2
.idea/misc.xml generated
View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (ptools)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (pt_assist)" project-jdk-type="Python SDK" />
</project>

2
.idea/ptools.iml generated
View File

@@ -17,7 +17,7 @@
<excludeFolder url="file://$MODULE_DIR$/venv" />
<excludeFolder url="file://$MODULE_DIR$/db" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="Python 3.9 (pt_assist)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TemplatesService">

View File

@@ -90,13 +90,13 @@ def import_from_ptpp(request):
res = pt_spider.parse_ptpp_cookies(data_list)
if res.code == StatusCodeEnum.OK.code:
cookies = res.data
print(cookies)
# print(cookies)
else:
return JsonResponse(res.to_dict(), safe=False)
message_list = []
for data in cookies:
try:
print(data)
# print(data)
res = pt_spider.get_uid_and_passkey(data)
msg = res.msg
print(msg)
@@ -117,6 +117,7 @@ def import_from_ptpp(request):
'msg': message,
'tag': 'warning'
})
raise
return JsonResponse(CommonResponse.success(data={
'messages': message_list
}).to_dict(), safe=False)

View File

@@ -7,6 +7,8 @@ from datetime import datetime
import aip
import cloudscraper
import dateutil.parser
import opencc
from django.db import transaction
from django.db.models import QuerySet
@@ -233,10 +235,12 @@ class PtSpider:
# 解析前端传来的数据
datas = json.loads(data_list.get('cookies'))
info_list = json.loads(data_list.get('info'))
userdata_list = json.loads(data_list.get('userdata'))
cookies = []
try:
for data, info in zip(datas, info_list):
cookie_list = data.get('cookies')
host = data.get('host')
cookie_str = ''
for cookie in cookie_list:
cookie_str += cookie.get('name') + '=' + cookie.get('value') + ';'
@@ -245,49 +249,72 @@ class PtSpider:
'url': data.get('url'),
'info': info.get('user'),
'passkey': info.get('passkey'),
'cookies': cookie_str.rstrip(';')
'cookies': cookie_str.rstrip(';'),
'userdatas': userdata_list.get(host)
})
print(len(cookies))
print(cookies)
# print(cookies)
return CommonResponse.success(data=cookies)
except Exception as e:
# raise
return CommonResponse.error(msg='Cookies解析失败请确认导入了正确的cookies备份文件')
def get_uid_and_passkey(self, cookie: dict):
site = Site.objects.filter(url__contains=cookie.get('url')).first()
url = cookie.get('url')
site = Site.objects.filter(url__contains=url).first()
# print('查询站点信息:',site)
if not site:
return CommonResponse.error(msg='尚未支持此站点:' + cookie.get('url'))
return CommonResponse.error(msg='尚未支持此站点:' + url)
my_site = MySite.objects.filter(site=site).first()
# print('查询我的站点:',my_site)
# 如果有更新cookie如果没有继续创建
my_level = re.sub(u'([^a-zA-Z_ ])', "", cookie.get('info').get('levelName'))
if not my_site:
my_site = MySite(
site=site,
cookie=cookie.get('cookies'),
passkey=cookie.get('passkey'),
user_id=cookie.get('info').get('id'),
my_level=my_level,
time_join=cookie.get('info').get('joinTime'),
seed=cookie.get('info').get('seeding'),
mail=cookie.get('info').get('messageCount')
)
my_site.save()
# status = SiteStatus(
# my_site=my_site,
# uploaded=cookie.get('uploaded'),
# downloaded=cookie.get('downloaded'),
# my_sp=cookie.get('bonus'),
# seed_vol=cookie.get('seedingSize'),
# )
return CommonResponse.success(msg=site.name + ' 信息导入成功!')
my_level_str = cookie.get('info').get('levelName')
if my_level_str:
my_level = re.sub(u'([^a-zA-Z_ ])', "", my_level_str)
else:
my_site.cookie = cookie.get('cookies').rstrip(';')
my_site.save()
return CommonResponse.success(msg=site.name + ' 信息更新成功!')
my_level = ' '
userdatas = cookie.get('userdatas')
if not my_site:
time_stamp = cookie.get('info').get('joinTime')
if time_stamp:
time_join = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time_stamp / 1000))
else:
time_join = None
result = MySite.objects.update_or_create(site=site, defaults={
'cookie': cookie.get('cookies'),
'passkey': cookie.get('passkey'),
'user_id': cookie.get('info').get('id'),
'my_level': my_level if my_level else ' ',
'time_join': time_join,
'seed': cookie.get('info').get('seeding') if cookie.get('info').get('seeding') else 0,
'mail': cookie.get('info').get('messageCount') if cookie.get('info').get('messageCount') else 0,
})
my_site = result[0]
for key, value in userdatas.items():
print(key)
downloaded = value.get('downloaded')
uploaded = value.get('uploaded')
seeding_size = value.get('seedingSize')
my_sp = value.get('bonus')
if not value.get(
'id') or key == 'latest' or not downloaded or not uploaded or not seeding_size or not my_sp:
continue
create_time = dateutil.parser.parse(key).date()
res_status = SiteStatus.objects.update_or_create(
site=my_site,
created_at__date=create_time,
defaults={
'uploaded': uploaded,
'downloaded': downloaded,
'my_sp': my_sp,
'seed_vol': seeding_size,
})
res_status[0].created_at = create_time
res_status[0].save()
print(res_status)
return CommonResponse.success(
msg=site.name + (' 信息导入成功!' if result[1] else ' 信息更新成功!')
)
def sign_in_hdsky(self, my_site: MySite, captcha=False):
"""HDSKY签到"""
@@ -451,7 +478,8 @@ class PtSpider:
bonus = res_json.get('message')
days = (int(bonus) - 10) / 2 + 1
signin_today.sign_in_today = True
message = '成功,已连续签到{}天,魔力值加{},明日继续签到可获取{}魔力值!'.format(days, bonus, bonus + 2)
message = '成功,已连续签到{}天,魔力值加{},明日继续签到可获取{}魔力值!'.format(days, bonus,
bonus + 2)
signin_today.sign_in_info = message
signin_today.save()
return CommonResponse.success(

View File

@@ -196,11 +196,11 @@ class StatusInlines(admin.TabularInline):
fields = [
'uploaded', 'downloaded', 'ratio',
'my_sp', 'my_bonus', 'seed_vol',
'updated_at'
'created_at'
]
classes = ['collapse']
readonly_fields = ['updated_at']
ordering = ['-updated_at']
readonly_fields = ['created_at']
ordering = ['-created_at']
# 自定义模板,删除外键显示
template = 'admin/pt_site/inline_status/tabular.html'
@@ -221,11 +221,11 @@ class SignInInlines(admin.StackedInline):
model = SignIn
fields = [
'sign_in_today', 'sign_in_info',
'updated_at'
'created_at'
]
classes = ['collapse']
readonly_fields = ['updated_at']
ordering = ['-updated_at']
readonly_fields = ['created_at']
ordering = ['-created_at']
# 自定义模板,删除外键显示
# template = 'admin/pt_site/inline_status/tabular.html'

View File

@@ -4,4 +4,5 @@ from . import views
urlpatterns = [
path(r'test_import', views.test_import, name='test_import'),
path(r'handle_json', views.handle_json, name='handle_json'),
]

View File

@@ -46,3 +46,8 @@ def test_import(request):
return JsonResponse(CommonResponse.success(data={
'messages': message_list
}).to_dict(), safe=False)
def handle_json(request):
if request.method == 'GET':
return render(request, 'pt_test/test_json.html')

View File

@@ -85,7 +85,8 @@
return {
user: {
info: '',
cookies: ''
cookies: '',
userdata: '',
},
import: true,
zip: ''
@@ -133,7 +134,15 @@
this.user.info = JSON.stringify(ptpp.sites, null, " ")
})
}
if (fileName.includes("userdatas.json")) {
console.log(fileName)
res.file(obj.name)
.async('text')
.then(response => {
let ptpp = JSON.parse(response);
this.user.userdata = JSON.stringify(ptpp, null, " ")
})
}
}
})
console.log(file_list)

View File

@@ -99,7 +99,8 @@
return {
user: {
info: '',
cookies: ''
cookies: '',
userdata: '',
},
import: true,
zip: ''
@@ -139,7 +140,15 @@
this.user.info = JSON.stringify(ptpp.sites, null, " ")
})
}
if (fileName.includes("userdatas.json")) {
console.log(fileName)
res.file(obj.name)
.async('text')
.then(response => {
let ptpp = JSON.parse(response);
this.user.userdata = JSON.stringify(ptpp.sites, null, " ")
})
}
}
})
})

View File

@@ -0,0 +1,319 @@
{% load static %}
<!DOCTYPE html>
<html>
<head>
{% include 'admin/includes/css-part.html' %}
<style>
#content {
background-color: dimgrey;
color: whitesmoke;
border: 2px solid dimgrey;
border-radius: 3px;
line-height: 20px;
font-size: 16px;
width: 90%;
height: 500px;
padding: 5px;
overflow-y: scroll;
word-break: break-all;
}
.ui-upload {
font-size: 14px;
width: 80px;
height: 30px;
line-height: 30px;
text-align: center;
position: relative;
cursor: pointer;
color: #fff;
background: #00abff;
border-radius: 3px;
overflow: hidden;
display: inline-block;
text-decoration: none;
}
.ui-upload input {
position: absolute;
font-size: 100px;
right: 0;
top: 0;
opacity: 0;
filter: alpha(opacity=0);
cursor: pointer
}
</style>
</head>
<body>
<div id="ptpp">
{# <input id="uploadFile" type="file" @change="changeFile($event)"/>#}
<el-card class="box-card">
<div slot="header" class="clearfix">
<label class="ui-upload">选取文件
<input type="file" v-model="zip" @change="showFile($event)"/>
</label>
{# <el-upload#}
{# id="upload"#}
{# style="width: 50%"#}
{# :show-file-list="false"#}
{# @http-request="showFile($event)"#}
{# :on-change="showFile"#}
{# :before-upload="showFile($event)"#}
{# :accept="zip"#}
{# :limit="1"#}
{# :auto-upload="false">#}
{# <el-button slot="trigger" type="primary" @change="showFile($event)">选取文件</el-button>#}
{# </el-upload>#}
<el-button style="float: right;" size="small" type="success" @click="do_import">导入</el-button>
</div>
<div class="text item">
<el-row>
{# <el-col :span="12" style="text-align: center">#}
{# <h3>用户信息</h3>#}
{# <textarea id="content" readonly v-model="user.info"></textarea>#}
{# </el-col>#}
{# <el-col :span="12" style="text-align: center">#}
{# <h3>网站Cookies</h3>#}
{# <textarea id="content" readonly v-model="user.cookies"></textarea>#}
{# </el-col>#}
<el-col :span="12" style="text-align: center">
<h3>网站Cookies</h3>
<textarea id="content" v-model="user.userdata"></textarea>
</el-col>
<el-button type="danger" @click="handleJson">handle</el-button>
</el-row>
</div>
</el-card>
</div>
{% include 'admin/includes/js-part.html' %}
<script src="{% static 'admin/simpleui-x/js/axios.min.js' %}"></script>
<script src="{% static 'js/FileSaver.min.js' %}"></script>
<script src="{% static 'js/jszip.min.js' %}"></script>
<script type="text/javascript">
const vm = new Vue({
el: '#ptpp',
data() {
return {
user: {
info: '',
cookies: '',
userdata: '',
},
import: true,
zip: ''
}
},
watch: {},
methods: {
handleJson() {
{#let ptpp = JSON.parse(this.user.userdata)#}
{#console.log(ptpp)#}
{#this.user.userdata = JSON.stringify(ptpp, null, " ")#}
let userdata = JSON.stringify(this.user.userdata)
{#console.log(this.user.userdata)#}
{#console.log(JSON.parse(userdata).length)#}
for (const key in userdata) {
console.log(key)
const element = userdata[key];
console.log("------->" + element)
console.log(element[0])
}
},
changeFile(input) {
let file = input.target.files[0];
JSZip.loadAsync(file).then((res) => {
console.log(res.files);
res.forEach((ele, obj) => {
if (!obj.dir) {
// 压缩包内文件名称
let fileName = obj.name;
if (fileName.includes("cookie")) {
console.log(fileName)
res.file(obj.name)
.async('text')
.then(response => {
let ptpp = JSON.parse(response);
this.user.cookies = JSON.stringify(ptpp, null, " ")
})
}
if (fileName.includes("options")) {
console.log(fileName)
res.file(obj.name)
.async('text')
.then(response => {
let ptpp = JSON.parse(response);
this.user.info = JSON.stringify(ptpp.sites, null, " ")
})
}
if (fileName.includes("userdatas")) {
console.log(fileName)
res.file(obj.name)
.async('text')
.then(response => {
let ptpp = JSON.parse(response);
this.user.userdata = JSON.stringify(ptpp.sites, null, " ")
})
}
}
})
})
}
,
showFile(input) {
//支持chrome IE10
try {
if (window.FileReader) {
let file = input.target.files[0];
if (!file.type.includes('zip')) {
console.log(file.type)
this.$message({
type: 'warning',
message: '请选择正确的ZIP文件'
});
return
}
let file_list = [];
JSZip.loadAsync(file).then((res) => {
{#console.log(res.files.item(0));#}
res.forEach((ele, obj) => {
if (!obj.dir) {
// 压缩包内文件名称
let fileName = obj.name;
file_list.push(fileName)
if (fileName.includes("cookie")) {
console.log(fileName)
res.file(obj.name)
.async('text')
.then(response => {
let ptpp = JSON.parse(response);
this.user.cookies = JSON.stringify(ptpp, null, " ")
})
}
if (fileName.includes("options")) {
console.log(fileName)
res.file(obj.name)
.async('text')
.then(response => {
let ptpp = JSON.parse(response);
this.user.info = JSON.stringify(ptpp.sites, null, " ")
})
}
}
})
console.log(file_list)
if (!file_list.includes('cookies.json') || !file_list.includes('options.json')) {
this.$message({
type: 'warning',
message: '请检查压缩包内是否包含cookies.json和options.json两个文件'
});
}
})
} else {
this.$message({
type: 'warning',
message: '可能不支持您的浏览器请使用Chrome或Edge'
});
}
} catch (e) {
this.$message({
type: 'warning',
message: 'PTPP数据必须为标准JSON格式请检查数据是否有误'
});
}
}
,
do_import() {
var self = this;
{#console.log(this.ptpp)#}
if (this.user.info === '' || this.user.cookies === '') {
this.$message({
type: 'warning',
message: '请检查压缩包内文件是否齐全至少要包含cookies.json和options.json两个文件'
});
return
}
if (this.ptpp === '') {
this.$message({
type: 'warning',
message: '数据获取失败,请检查数据文件是否有误???'
});
}
this.$confirm('确认导入数据 ', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
axios.post(
"{% url "test_import" %}",
{
'user': this.user
}).then(res => {
if (res.data.code === 0) {
let messages = res.data.data.messages
Array.from(messages).forEach(item => {
var duration = 0
switch (item.tag) {
case 'success':
duration = 1500;
break;
case 'warning':
duration = 0;
break;
case 'error':
duration = 0;
break;
}
setTimeout(function () {
console.log(duration)
self.$notify({
title: '提示',
message: item.msg,
type: item.tag,
dangerouslyUseHTMLString: true,
duration: duration
});
}, 200);
})
//清空PTPP
this.ptpp = ''
this.zip = ''
} else {
this.$message({
type: 'error',
message: res.data.msg
});
}
}).catch(() => {
this.$message({
type: 'warning',
message: '访问出错!'
});
})
}).catch(() => {
this.$message({
type: 'info',
message: '已取消'
});
});
}
}
})
</script>
</body>
</html>