升级导入功能,支持直接使用PTPP备份包导入,不再支持单个JSON文件导入

This commit is contained in:
ngfchl
2022-09-13 13:20:29 +08:00
parent ca7b1f3f67
commit 99c3a3f27e
8 changed files with 11908 additions and 110 deletions

View File

@@ -1,7 +1,6 @@
import json
import socket
import subprocess
import sys
from datetime import datetime, timedelta
import docker
@@ -80,7 +79,6 @@ def test_notify(request):
def do_sql(request):
print('exit')
sys.exit(3)
return JsonResponse('ok', safe=False)
@@ -88,22 +86,14 @@ def import_from_ptpp(request):
if request.method == 'GET':
return render(request, 'auto_pt/import_ptpp.html')
else:
# print(request.body)
data_list = json.loads(request.body).get('ptpp')
datas = json.loads(data_list)
print('content', len(datas))
res = pt_spider.parse_ptpp_cookies(datas)
data_list = json.loads(request.body).get('user')
res = pt_spider.parse_ptpp_cookies(data_list)
if res.code == StatusCodeEnum.OK.code:
cookies = res.data
print(cookies)
else:
return JsonResponse(res.to_dict(), safe=False)
# success_messages = []
# error_messages = []
message_list = []
# print(datas)
for data in cookies:
try:
print(data)
@@ -127,7 +117,6 @@ def import_from_ptpp(request):
'msg': message,
'tag': 'warning'
})
# raise
return JsonResponse(CommonResponse.success(data={
'messages': message_list
}).to_dict(), safe=False)

View File

@@ -1,3 +1,4 @@
import json
import logging
import re
import threading
@@ -228,94 +229,47 @@ class PtSpider:
msg=StatusCodeEnum.OCR_ACCESS_ERR.errmsg + str(e)
)
def parse_ptpp_cookies(self, datas):
def parse_ptpp_cookies(self, data_list):
# 解析前端传来的数据
datas = json.loads(data_list.get('cookies'))
info_list = json.loads(data_list.get('info'))
cookies = []
try:
if datas[0].get('domain'):
print(datas[0].get('domain'))
return CommonResponse.success(data=self.parse_cookie_expire(datas))
for data in datas:
domain = data.get('host')
for data, info in zip(datas, info_list):
cookie_list = data.get('cookies')
cookie_str = ''
for cookie in cookie_list:
cookie_str += cookie.get('name') + '=' + cookie.get('value') + ';'
print(domain, cookie_str)
# print(domain, cookie_str)
cookies.append({
'domain': domain,
'url': data.get('url'),
'uid': info.get('user').get('id'),
'passkey': info.get('passkey'),
'cookies': cookie_str.rstrip(';')
})
print(len(cookies))
print(cookies)
return CommonResponse.success(data=cookies)
except Exception as e:
# raise
return CommonResponse.error(msg='Cookies解析失败请确认导入了正确的cookies备份文件')
def parse_cookie_expire(self, datas):
"""
使用魔改版的一键延长COOKIE有效期备份文件
:param datas:
:return:
"""
cookies = []
cookie = {
'domain': '',
'cookies': ''
}
for index, data in enumerate(datas):
domain = data.get('domain').lstrip('.').lstrip('www.')
# print('domain', domain)
value = data.get('name') + '=' + data.get('value') + ';'
# print('value', value)
if not cookie.get('domain'):
cookie['domain'] = domain
cookie['cookies'] = value
elif domain in cookie.get('domain'):
# new_value = cookie['cookies'] + value
# cookie['cookies'] = new_value
cookie['cookies'] += value
# print('new_value', cookie['cookies'])
else:
cookie['cookies'] = cookie['cookies'].rstrip(';')
cookies.append(cookie)
# print(len(cookies))
# cookie = {}
# cookie['domain'] = domain
# cookie['cookies'] = value
cookie = {'domain': domain, 'cookies': value}
if index == len(datas) - 1:
cookies.append(cookie)
# print('cookie:', cookie)
print('cookies,', len(cookies))
return cookies
def get_uid_and_passkey(self, cookie: dict):
site = Site.objects.filter(url__contains=cookie.get('domain')).first()
site = Site.objects.filter(url__contains=cookie.get('url')).first()
# print('查询站点信息:',site)
if not site:
return CommonResponse.error(msg='尚未支持此站点:' + cookie.get('domain'))
return CommonResponse.error(msg='尚未支持此站点:' + cookie.get('url'))
my_site = MySite.objects.filter(site=site).first()
# print('查询我的站点:',my_site)
# 如果有更新cookie如果没有继续创建
if not my_site:
my_site = MySite(
site=site,
cookie=cookie.get('cookies')
cookie=cookie.get('cookies'),
user_id=cookie.get('uid'),
passkey=cookie.get('passkey'),
)
url = site.url + site.page_control_panel
# print(my_site.cookie)
print(url)
response = self.send_request(my_site=my_site, url=url)
# print(response.content.decode('utf8'))
user_details = self.parse(response, site.my_uid_rule)
print(user_details)
user_details_href = user_details[0]
passkey = self.parse(response, site.my_passkey_rule)[0]
uid = re.sub(r'\D', '', user_details_href)
print(uid, passkey)
my_site.user_id = uid
my_site.passkey = passkey
my_site.save()
print(site.name, uid, passkey)
return CommonResponse.success(msg=site.name + ' 信息导入成功!')
else:
my_site.cookie = cookie.get('cookies').rstrip(';')

188
static/js/FileSaver.js Normal file
View File

@@ -0,0 +1,188 @@
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define([], factory);
} else if (typeof exports !== "undefined") {
factory();
} else {
var mod = {
exports: {}
};
factory();
global.FileSaver = mod.exports;
}
})(this, function () {
"use strict";
/*
* FileSaver.js
* A saveAs() FileSaver implementation.
*
* By Eli Grey, http://eligrey.com
*
* License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
* source : http://purl.eligrey.com/github/FileSaver.js
*/
// The one and only way of getting global scope in all environments
// https://stackoverflow.com/q/3277182/1008999
var _global = typeof window === 'object' && window.window === window ? window : typeof self === 'object' && self.self === self ? self : typeof global === 'object' && global.global === global ? global : void 0;
function bom(blob, opts) {
if (typeof opts === 'undefined') opts = {
autoBom: false
};else if (typeof opts !== 'object') {
console.warn('Deprecated: Expected third argument to be a object');
opts = {
autoBom: !opts
};
} // prepend BOM for UTF-8 XML and text/* types (including HTML)
// note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
return new Blob([String.fromCharCode(0xFEFF), blob], {
type: blob.type
});
}
return blob;
}
function download(url, name, opts) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.onload = function () {
saveAs(xhr.response, name, opts);
};
xhr.onerror = function () {
console.error('could not download file');
};
xhr.send();
}
function corsEnabled(url) {
var xhr = new XMLHttpRequest(); // use sync to avoid popup blocker
xhr.open('HEAD', url, false);
try {
xhr.send();
} catch (e) {}
return xhr.status >= 200 && xhr.status <= 299;
} // `a.click()` doesn't work for all browsers (#465)
function click(node) {
try {
node.dispatchEvent(new MouseEvent('click'));
} catch (e) {
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
node.dispatchEvent(evt);
}
} // Detect WebView inside a native macOS app by ruling out all browsers
// We just need to check for 'Safari' because all other browsers (besides Firefox) include that too
// https://www.whatismybrowser.com/guides/the-latest-user-agent/macos
var isMacOSWebView = /Macintosh/.test(navigator.userAgent) && /AppleWebKit/.test(navigator.userAgent) && !/Safari/.test(navigator.userAgent);
var saveAs = _global.saveAs || ( // probably in some web worker
typeof window !== 'object' || window !== _global ? function saveAs() {}
/* noop */
// Use download attribute first if possible (#193 Lumia mobile) unless this is a macOS WebView
: 'download' in HTMLAnchorElement.prototype && !isMacOSWebView ? function saveAs(blob, name, opts) {
var URL = _global.URL || _global.webkitURL;
var a = document.createElement('a');
name = name || blob.name || 'download';
a.download = name;
a.rel = 'noopener'; // tabnabbing
// TODO: detect chrome extensions & packaged apps
// a.target = '_blank'
if (typeof blob === 'string') {
// Support regular links
a.href = blob;
if (a.origin !== location.origin) {
corsEnabled(a.href) ? download(blob, name, opts) : click(a, a.target = '_blank');
} else {
click(a);
}
} else {
// Support blobs
a.href = URL.createObjectURL(blob);
setTimeout(function () {
URL.revokeObjectURL(a.href);
}, 4E4); // 40s
setTimeout(function () {
click(a);
}, 0);
}
} // Use msSaveOrOpenBlob as a second approach
: 'msSaveOrOpenBlob' in navigator ? function saveAs(blob, name, opts) {
name = name || blob.name || 'download';
if (typeof blob === 'string') {
if (corsEnabled(blob)) {
download(blob, name, opts);
} else {
var a = document.createElement('a');
a.href = blob;
a.target = '_blank';
setTimeout(function () {
click(a);
});
}
} else {
navigator.msSaveOrOpenBlob(bom(blob, opts), name);
}
} // Fallback to using FileReader and a popup
: function saveAs(blob, name, opts, popup) {
// Open a popup immediately do go around popup blocker
// Mostly only available on user interaction and the fileReader is async so...
popup = popup || open('', '_blank');
if (popup) {
popup.document.title = popup.document.body.innerText = 'downloading...';
}
if (typeof blob === 'string') return download(blob, name, opts);
var force = blob.type === 'application/octet-stream';
var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari;
var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
if ((isChromeIOS || force && isSafari || isMacOSWebView) && typeof FileReader !== 'undefined') {
// Safari doesn't allow downloading of blob URLs
var reader = new FileReader();
reader.onloadend = function () {
var url = reader.result;
url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
if (popup) popup.location.href = url;else location = url;
popup = null; // reverse-tabnabbing #460
};
reader.readAsDataURL(blob);
} else {
var URL = _global.URL || _global.webkitURL;
var url = URL.createObjectURL(blob);
if (popup) popup.location = url;else location.href = url;
popup = null; // reverse-tabnabbing #460
setTimeout(function () {
URL.revokeObjectURL(url);
}, 4E4); // 40s
}
});
_global.saveAs = saveAs.saveAs = saveAs;
if (typeof module !== 'undefined') {
module.exports = saveAs;
}
});

3
static/js/FileSaver.min.js vendored Normal file
View File

@@ -0,0 +1,3 @@
(function(a,b){if("function"==typeof define&&define.amd)define([],b);else if("undefined"!=typeof exports)b();else{b(),a.FileSaver={exports:{}}.exports}})(this,function(){"use strict";function b(a,b){return"undefined"==typeof b?b={autoBom:!1}:"object"!=typeof b&&(console.warn("Deprecated: Expected third argument to be a object"),b={autoBom:!b}),b.autoBom&&/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(a.type)?new Blob(["\uFEFF",a],{type:a.type}):a}function c(a,b,c){var d=new XMLHttpRequest;d.open("GET",a),d.responseType="blob",d.onload=function(){g(d.response,b,c)},d.onerror=function(){console.error("could not download file")},d.send()}function d(a){var b=new XMLHttpRequest;b.open("HEAD",a,!1);try{b.send()}catch(a){}return 200<=b.status&&299>=b.status}function e(a){try{a.dispatchEvent(new MouseEvent("click"))}catch(c){var b=document.createEvent("MouseEvents");b.initMouseEvent("click",!0,!0,window,0,0,0,80,20,!1,!1,!1,!1,0,null),a.dispatchEvent(b)}}var f="object"==typeof window&&window.window===window?window:"object"==typeof self&&self.self===self?self:"object"==typeof global&&global.global===global?global:void 0,a=/Macintosh/.test(navigator.userAgent)&&/AppleWebKit/.test(navigator.userAgent)&&!/Safari/.test(navigator.userAgent),g=f.saveAs||("object"!=typeof window||window!==f?function(){}:"download"in HTMLAnchorElement.prototype&&!a?function(b,g,h){var i=f.URL||f.webkitURL,j=document.createElement("a");g=g||b.name||"download",j.download=g,j.rel="noopener","string"==typeof b?(j.href=b,j.origin===location.origin?e(j):d(j.href)?c(b,g,h):e(j,j.target="_blank")):(j.href=i.createObjectURL(b),setTimeout(function(){i.revokeObjectURL(j.href)},4E4),setTimeout(function(){e(j)},0))}:"msSaveOrOpenBlob"in navigator?function(f,g,h){if(g=g||f.name||"download","string"!=typeof f)navigator.msSaveOrOpenBlob(b(f,h),g);else if(d(f))c(f,g,h);else{var i=document.createElement("a");i.href=f,i.target="_blank",setTimeout(function(){e(i)})}}:function(b,d,e,g){if(g=g||open("","_blank"),g&&(g.document.title=g.document.body.innerText="downloading..."),"string"==typeof b)return c(b,d,e);var h="application/octet-stream"===b.type,i=/constructor/i.test(f.HTMLElement)||f.safari,j=/CriOS\/[\d]+/.test(navigator.userAgent);if((j||h&&i||a)&&"undefined"!=typeof FileReader){var k=new FileReader;k.onloadend=function(){var a=k.result;a=j?a:a.replace(/^data:[^;]*;/,"data:attachment/file;"),g?g.location.href=a:location=a,g=null},k.readAsDataURL(b)}else{var l=f.URL||f.webkitURL,m=l.createObjectURL(b);g?g.location=m:location.href=m,g=null,setTimeout(function(){l.revokeObjectURL(m)},4E4)}});f.saveAs=g.saveAs=g,"undefined"!=typeof module&&(module.exports=g)});
//# sourceMappingURL=FileSaver.min.js.map

File diff suppressed because one or more lines are too long

11577
static/js/jszip.js Normal file

File diff suppressed because it is too large Load Diff

13
static/js/jszip.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -12,38 +12,70 @@
border-radius: 3px;
line-height: 20px;
font-size: 16px;
width: 100%;
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">
<el-card class="box-card">
<div slot="header" class="clearfix">
<span>
<input type="file" v-model="cookies" class="upload-demo" @change="showFile($event)"/>
</span>
<el-button style="float: right;" type="success" @click="do_import">导入</el-button>
{# <el-upload#}
{# :show-file-list="false"#}
{# :on-change="showFile($event)"#}
{# :auto-upload="false">#}
{# <el-button type="success">点击上传</el-button>#}
{# </el-upload>#}
<label class="ui-upload">选取文件
<input type="file" v-model="zip" @change="showFile($event)"/>
</label>
<el-button style="float: right;" size="small" type="success" @click="do_import">导入</el-button>
</div>
<div class="text item">
<textarea id="content" readonly v-model="ptpp"></textarea>
<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-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">
@@ -51,33 +83,68 @@
el: '#ptpp',
data() {
return {
ptpp: '',
cookies: ''
user: {
info: '',
cookies: ''
},
import: true,
zip: ''
}
},
watch: {},
methods: {
do_format() {
let ptpp = JSON.parse(this.ptpp)
this.ptpp = JSON.stringify(ptpp, null, " ")
},
showFile(input) {
//支持chrome IE10
try {
if (window.FileReader) {
var file = input.target.files[0];
var reader = new FileReader();
reader.onload = ((event) => {
//显示文件
let ptpp = JSON.parse(event.target.result);
this.ptpp = JSON.stringify(ptpp, null, " ")
{#console.log(event.target.result)#}
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两个文件'
});
}
})
console.log(this.cookies)
console.info(file)
console.info(reader);
reader.readAsText(file);
} else {
this.$message({
type: 'warning',
@@ -94,6 +161,13 @@
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',
@@ -108,7 +182,7 @@
axios.post(
"{% url "import_from_ptpp" %}",
{
'ptpp': this.ptpp
'user': this.user
}).then(res => {
if (res.data.code === 0) {
let messages = res.data.data.messages
@@ -138,8 +212,7 @@
})
//清空PTPP
this.ptpp = ''
this.cookies = ''
{#this.location.refresh()#}
this.zip = ''
} else {
this.$message({
type: 'error',