|
| 1 | +import urllib |
| 2 | + |
| 3 | +from py12306 import config |
| 4 | +from py12306.helpers.api import * |
| 5 | +from py12306.helpers.request import Request |
| 6 | +from py12306.log.common_log import CommonLog |
| 7 | + |
| 8 | + |
| 9 | +class Notification(): |
| 10 | + """ |
| 11 | + 通知类 |
| 12 | + """ |
| 13 | + session = None |
| 14 | + |
| 15 | + def __init__(self): |
| 16 | + self.session = Request() |
| 17 | + |
| 18 | + @classmethod |
| 19 | + def voice_code(cls, phone, name='', content=''): |
| 20 | + self = cls() |
| 21 | + self.send_voice_code_of_yiyuan(phone, name=name, content=content) |
| 22 | + |
| 23 | + def send_voice_code_of_yiyuan(self, phone, name='', content=''): |
| 24 | + """ |
| 25 | + 发送语音验证码 |
| 26 | + 购买地址 https://market.aliyun.com/products/57126001/cmapi019902.html?spm=5176.2020520132.101.5.37857218O6iJ3n |
| 27 | + :return: |
| 28 | + """ |
| 29 | + appcode = config.NOTIFICATION_API_APP_CODE |
| 30 | + if not appcode: |
| 31 | + CommonLog.add_quick_log(CommonLog.MESSAGE_EMPTY_APP_CODE).flush() |
| 32 | + return False |
| 33 | + body = { |
| 34 | + 'userName': name, |
| 35 | + 'mailNo': content |
| 36 | + } |
| 37 | + params = { |
| 38 | + 'content': body, |
| 39 | + 'mobile': phone, |
| 40 | + 'sex': 2, |
| 41 | + 'tNum': 'T170701001056' |
| 42 | + } |
| 43 | + response = self.session.request(url=API_NOTIFICATION_BY_VOICE_CODE + urllib.parse.urlencode(params), |
| 44 | + method='GET', headers={ |
| 45 | + 'Authorization': 'APPCODE {}'.format(appcode) |
| 46 | + }) |
| 47 | + response_message = '-' |
| 48 | + result = {} |
| 49 | + try: |
| 50 | + result = response.json() |
| 51 | + response_message = result['showapi_res_body']['remark'] |
| 52 | + except: |
| 53 | + pass |
| 54 | + if response.status_code == 401 or response.status_code == 403: |
| 55 | + return CommonLog.add_quick_log(CommonLog.MESSAGE_VOICE_API_FORBID).flush() |
| 56 | + if response.status_code == 200 and 'showapi_res_body' in result and result['showapi_res_body'].get('flag'): |
| 57 | + CommonLog.add_quick_log(CommonLog.MESSAGE_VOICE_API_SEND_SUCCESS.format(response_message)).flush() |
| 58 | + return True |
| 59 | + else: |
| 60 | + return CommonLog.add_quick_log(CommonLog.MESSAGE_VOICE_API_SEND_FAIL.format(response_message)).flush() |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == '__main__': |
| 64 | + Notification.voice_code('13800138000', '张三', '你的车票 广州 到 深圳 购买成功,请登录 12306 进行支付') |
0 commit comments