Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

双签奖励修复,定时脚本新增完成时间 #82

Open
wants to merge 6 commits into
base: browser
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 35 additions & 56 deletions app/job/double_sign.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import traceback
import time

from pyquery import PyQuery

Expand All @@ -9,95 +10,73 @@
class DoubleSign(Daka):
job_name = '双签赢奖励'

index_url = 'https://ljd.m.jd.com/countersign/index.action'
sign_url = 'https://ljd.m.jd.com/countersign/receiveAward.json'
index_url = 'https://ms.jr.jd.com/gw/generic/jrm/h5/m/getAwardList?_=' + \
str((int)(time.time()))
sign_url = 'https://ms.jr.jd.com/gw/generic/jrm/h5/m/getSignAwardJR?_=' +\
str((int)(time.time()))
test_url = index_url

def is_signed(self):
signed = False

try:
signed = PyQuery(self.page_data())('#awardFlag').val() == '2'
self.logger.info('今日已双签: {}'.format(signed))

except Exception as e:
self.logger.error('返回数据结构可能有变化, 获取双签数据失败: {}'.format(e))
traceback.print_exc()

return signed

def sign(self):
# 参见 https://ljd.m.jd.com/js/countersign/countersign.js

sign_success = True
message = ''

document = PyQuery(self.page_data())

jd_signed = document('#jdHasSign').val() == 'true'
jr_signed = document('#jrHasSign').val() == 'true'

if not (jd_signed and jr_signed):
sign_success = False
message = '完成双签才可领取礼包'

else:
if(self.is_signed() == False):
try:
res = self.do_sign()
except RequestError as e:
self.logger.error('双签失败: {}'.format(e.message))
return False

if res['code'] == '0':
award_data = res.get('data')
if res['resultCode'] == 200:
award_data = res.get('awardList')

if not award_data:
message = '运气不佳,领到一个空空的礼包'

else:
award = award_data[0]
sign_success = True
message = '领到 {} 个{}'.format(award['awardCount'], award['awardName'])

else:
# 活动不定时开启,将活动时间未开始/已结束等情况都视作签到成功

if res['code'] == 'DS102':
message = '来早了,活动还未开始'
elif res['code'] == 'DS103':
message = '来晚了,活动已经结束了'
elif res['code'] == 'DS104':
message = '运气不佳,领到一个空空的礼包'
elif res['code'] == 'DS106':
sign_success = False
message = '完成双签才可领取礼包'
else:
sign_success = False
message = '未知错误,Code={}'.format(res['code'])
message = '领到 {} 个{}'.format(
award['count'], award['name'])
else:
sign_success = False
message = '完成双签才可领取礼包'

self.logger.info('双签成功: {}; Message: {}'.format(sign_success, message))

return sign_success

def is_signed(self):
sign = False
r = self.session.post(self.index_url)
as_json = r.json()
if 'resultData' in as_json and 'awardList' in as_json['resultData']:
award_data = as_json['resultData'].get('awardList')
sign = True
self.logger.info('今日已签到: {}; 双签奖励: {}'.format(
sign, award_data[0]['count']))
return sign
return False

def do_sign(self):
r = self.session.post(self.sign_url)

try:
as_json = r.json()
# {'resultCode': 0, 'resultMsg': '操作成功',
# 'resultData': {'resultCode': 200, '
# resultMsg': '响应成功',
# 'status': 0, 'awardList': [
# {'count': 5, 'name': '京豆', 'type': 1}]},
# 'channelEncrypt': 0}
except ValueError:
raise RequestError('unexpected response: url: {}; http code: {}'.format(self.sign_url, r.status_code), response=r)
raise RequestError('unexpected response: url: {}; http code: {}'.format(
self.sign_url, r.status_code), response=r)

if 'res' in as_json and 'code' in as_json['res']:
if 'resultData' in as_json and 'resultCode' in as_json['resultData']:
# 请求成功
return as_json['res']
return as_json['resultData']

else:
error_msg = as_json.get('message') or str(as_json)
error_code = as_json.get('businessCode') or as_json.get('code')
raise RequestError(error_msg, error_code)

def page_data(self):
if not hasattr(self, '_page_data'):
self._page_data = self.session.get(self.index_url).text

return self._page_data
4 changes: 2 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import datetime
import pickle
import traceback
from pathlib import Path
Expand Down Expand Up @@ -27,8 +28,7 @@ def main():

if not job.job_success:
jobs_failed.append(job.job_name)

print('=================================')
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M')+'=================')
print('= 任务数: {}; 失败数: {}'.format(len(jobs), len(jobs_failed)))

if jobs_failed:
Expand Down