diff --git a/.idea/misc.xml b/.idea/misc.xml index 650ef91..5ae3a3c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/ptools.iml b/.idea/ptools.iml index 261fb4d..8114c7e 100644 --- a/.idea/ptools.iml +++ b/.idea/ptools.iml @@ -17,7 +17,7 @@ - + diff --git a/auto_pt/views.py b/auto_pt/views.py index 756eda4..2040ead 100644 --- a/auto_pt/views.py +++ b/auto_pt/views.py @@ -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) diff --git a/pt_site/UtilityTool.py b/pt_site/UtilityTool.py index 397fe7a..1b9c86f 100644 --- a/pt_site/UtilityTool.py +++ b/pt_site/UtilityTool.py @@ -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( diff --git a/pt_site/admin.py b/pt_site/admin.py index 46b0364..c7d699f 100644 --- a/pt_site/admin.py +++ b/pt_site/admin.py @@ -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' diff --git a/pt_test/urls.py b/pt_test/urls.py index 67ab04b..1f0e01d 100644 --- a/pt_test/urls.py +++ b/pt_test/urls.py @@ -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'), ] diff --git a/pt_test/views.py b/pt_test/views.py index d52d63a..27647f2 100644 --- a/pt_test/views.py +++ b/pt_test/views.py @@ -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') diff --git a/templates/auto_pt/import_ptpp.html b/templates/auto_pt/import_ptpp.html index e8adbb9..80adf48 100644 --- a/templates/auto_pt/import_ptpp.html +++ b/templates/auto_pt/import_ptpp.html @@ -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) diff --git a/templates/pt_test/test_import.html b/templates/pt_test/test_import.html index 1c844fd..2c2b98a 100644 --- a/templates/pt_test/test_import.html +++ b/templates/pt_test/test_import.html @@ -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, " ") + }) + } } }) }) diff --git a/templates/pt_test/test_json.html b/templates/pt_test/test_json.html new file mode 100644 index 0000000..1399e37 --- /dev/null +++ b/templates/pt_test/test_json.html @@ -0,0 +1,319 @@ +{% load static %} + + + + + {% include 'admin/includes/css-part.html' %} + + + +
+ {# #} + + +
+ + {# #} + {# 选取文件#} + {# #} + 导入 +
+
+ + {# #} + {#

用户信息

#} + {# #} + {#
#} + {# #} + {#

网站Cookies

#} + {# #} + {#
#} + +

网站Cookies

+ +
+ handle +
+ +
+ + +
+
+ +{% include 'admin/includes/js-part.html' %} + + + + + + + \ No newline at end of file