|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# BSD 2-Clause License |
| 4 | +# |
| 5 | +# Apprise - Push Notification Library. |
| 6 | +# Copyright (c) 2025, Chris Caron <lead2gold@gmail.com> |
| 7 | +# |
| 8 | +# Redistribution and use in source and binary forms, with or without |
| 9 | +# modification, are permitted provided that the following conditions are met: |
| 10 | +# |
| 11 | +# 1. Redistributions of source code must retain the above copyright notice, |
| 12 | +# this list of conditions and the following disclaimer. |
| 13 | +# |
| 14 | +# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | +# this list of conditions and the following disclaimer in the documentation |
| 16 | +# and/or other materials provided with the distribution. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | +# POSSIBILITY OF SUCH DAMAGE. |
| 29 | + |
| 30 | +# Assumes QQ Push API provided by third-party bridge like message-pusher |
| 31 | + |
| 32 | +import re |
| 33 | +import requests |
| 34 | + |
| 35 | +from ..utils.parse import validate_regex |
| 36 | +from ..url import PrivacyMode |
| 37 | +from .base import NotifyBase |
| 38 | +from ..locale import gettext_lazy as _ |
| 39 | +from ..common import NotifyType |
| 40 | + |
| 41 | + |
| 42 | +class NotifyQQ(NotifyBase): |
| 43 | + """ |
| 44 | + A wrapper for QQ Push Notifications |
| 45 | + """ |
| 46 | + |
| 47 | + # The default descriptive name associated with the Notification |
| 48 | + service_name = _('QQ Push') |
| 49 | + |
| 50 | + # The services URL |
| 51 | + service_url = 'https://github.com/songquanpeng/message-pusher' |
| 52 | + |
| 53 | + # The default secure protocol |
| 54 | + secure_protocol = 'qq' |
| 55 | + |
| 56 | + # A URL that takes you to the setup/help of the specific protocol |
| 57 | + setup_url = 'https://github.com/caronc/apprise/wiki/Notify_qq' |
| 58 | + |
| 59 | + # URL used to send notifications with |
| 60 | + notify_url = 'https://qmsg.zendee.cn/send/' |
| 61 | + |
| 62 | + templates = ( |
| 63 | + '{schema}://{token}', |
| 64 | + ) |
| 65 | + |
| 66 | + template_tokens = dict(NotifyBase.template_tokens, **{ |
| 67 | + 'token': { |
| 68 | + 'name': _('User Token'), |
| 69 | + 'type': 'string', |
| 70 | + 'private': True, |
| 71 | + 'required': True, |
| 72 | + 'regex': (r'^[a-z0-9]{24,64}$', 'i'), |
| 73 | + }, |
| 74 | + }) |
| 75 | + |
| 76 | + def __init__(self, token, **kwargs): |
| 77 | + """ |
| 78 | + Initialize QQ Push Object |
| 79 | +
|
| 80 | + Args: |
| 81 | + token (str): User push token from QQ Push provider (e.g., Qmsg) |
| 82 | + """ |
| 83 | + super().__init__(**kwargs) |
| 84 | + |
| 85 | + self.token = validate_regex( |
| 86 | + token, *self.template_tokens['token']['regex'] |
| 87 | + ) |
| 88 | + if not self.token: |
| 89 | + msg = 'The QQ Push token ({}) is invalid.'.format(token) |
| 90 | + self.logger.warning(msg) |
| 91 | + raise TypeError(msg) |
| 92 | + |
| 93 | + self.webhook_url = f'{self.notify_url}{self.token}' |
| 94 | + |
| 95 | + def url(self, privacy=False, *args, **kwargs): |
| 96 | + """ |
| 97 | + Returns the URL built dynamically based on specified arguments. |
| 98 | + """ |
| 99 | + params = self.url_parameters(privacy=privacy, *args, **kwargs) |
| 100 | + return '{schema}://{token}/?{params}'.format( |
| 101 | + schema=self.secure_protocol, |
| 102 | + token=self.pprint(self.token, privacy, mode=PrivacyMode.Secret), |
| 103 | + params=self.urlencode(params), |
| 104 | + ) |
| 105 | + |
| 106 | + @property |
| 107 | + def url_identifier(self): |
| 108 | + """ |
| 109 | + Returns a unique identifier for this plugin instance |
| 110 | + """ |
| 111 | + return (self.secure_protocol, self.token) |
| 112 | + |
| 113 | + def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs): |
| 114 | + """ |
| 115 | + Send a QQ Push Notification |
| 116 | + """ |
| 117 | + payload = { |
| 118 | + 'msg': f'{title}\n{body}' if title else body |
| 119 | + } |
| 120 | + |
| 121 | + headers = { |
| 122 | + 'User-Agent': self.app_id, |
| 123 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 124 | + } |
| 125 | + |
| 126 | + self.throttle() |
| 127 | + try: |
| 128 | + response = requests.post( |
| 129 | + self.webhook_url, |
| 130 | + headers=headers, |
| 131 | + data=payload, |
| 132 | + verify=self.verify_certificate, |
| 133 | + timeout=self.request_timeout, |
| 134 | + ) |
| 135 | + |
| 136 | + if response.status_code != requests.codes.ok: |
| 137 | + self.logger.warning( |
| 138 | + 'QQ Push notification failed: %d - %s', |
| 139 | + response.status_code, response.text) |
| 140 | + return False |
| 141 | + |
| 142 | + except requests.RequestException as e: |
| 143 | + self.logger.warning(f'QQ Push Exception: {e}') |
| 144 | + return False |
| 145 | + |
| 146 | + self.logger.info('QQ Push notification sent successfully.') |
| 147 | + return True |
| 148 | + |
| 149 | + @staticmethod |
| 150 | + def parse_url(url): |
| 151 | + """ |
| 152 | + Parses the URL and returns arguments to re-instantiate the object |
| 153 | + """ |
| 154 | + results = NotifyBase.parse_url(url, verify_host=False) |
| 155 | + if not results: |
| 156 | + return results |
| 157 | + |
| 158 | + if 'token' in results['qsd'] and results['qsd']['token']: |
| 159 | + results['token'] = NotifyQQ.unquote(results['qsd']['token']) |
| 160 | + else: |
| 161 | + results['token'] = NotifyQQ.unquote(results['host']) |
| 162 | + |
| 163 | + return results |
| 164 | + |
| 165 | + @staticmethod |
| 166 | + def parse_native_url(url): |
| 167 | + """ |
| 168 | + Parse native QQ push-style URL into Apprise format |
| 169 | + """ |
| 170 | + match = re.match( |
| 171 | + r'^https://qmsg\.zendee\.cn/send/([a-z0-9]+)$', url, re.I) |
| 172 | + if not match: |
| 173 | + return None |
| 174 | + |
| 175 | + return NotifyQQ.parse_url( |
| 176 | + '{schema}://{token}'.format( |
| 177 | + schema=NotifyQQ.secure_protocol, |
| 178 | + token=match.group(1))) |
0 commit comments