自動実行の設定#
- Cron 設定ファイルを開く
crontab -e
、初めて設定する場合、システムがデフォルトのエディタを選択するように促します。 - 定期的なタスクを追加します。Cron 式の形式は以下の通りです。
* * * * * 実行するコマンド
- - - - -
| | | | |
| | | | +----- 週のある日 (0 - 7) (日曜日は0または7)
| | | +------- 月 (1 - 12)
| | +--------- 月のある日 (1 - 31)
| +----------- 時間 (0 - 23)
+------------- 分 (0 - 59)
- 以下の Cron 式は、毎日の真夜中(0 時)に Python スクリプトを実行することを示しています。
0 0 * * * /usr/bin/python3 /path/to/your/script.py
- 定期的なタスクを確認する
crontab -l
コード#
import time
import re
import base64
import hashlib
import rsa
import requests
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
# 下の2行の引用符内にアカウント(携帯電話番号のみサポート)とパスワードを貼り付けます
username = ""
password = ""
assert username and password, "23行目と24行目に有効なアカウントとパスワードを入力してください"
# メール通知の設定情報
smtp_server = 'smtp.163.com' # SMTPサーバーアドレス
smtp_port = 25 # SMTPサーバーポート番号
sender_email = '' # 送信者のメールアドレス
sender_password = '' # 送信者のメールアドレスのパスワード/認証コード
receiver_email = '' # 受信者のメールアドレス
BI_RM = list("0123456789abcdefghijklmnopqrstuvwxyz")
B64MAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
s = requests.Session()
def int2char(a):
return BI_RM[a]
def b64tohex(a):
d = ""
e = 0
c = 0
for i in range(len(a)):
if list(a)[i] != "=":
v = B64MAP.index(list(a)[i])
if e == 0:
e = 1
d += int2char(v >> 2)
c = 3 & v
elif e == 1:
e = 2
d += int2char(c << 2 | v >> 4)
c = 15 & v
elif e == 2:
e = 3
d += int2char(c)
d += int2char(v >> 2)
c = 3 & v
else:
e = 0
d += int2char(c << 2 | v >> 4)
d += int2char(15 & v)
if e == 1:
d += int2char(c << 2)
return d
def rsa_encode(j_rsakey, string):
rsa_key = f"-----BEGIN PUBLIC KEY-----\n{j_rsakey}\n-----END PUBLIC KEY-----"
pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(rsa_key.encode())
result = b64tohex((base64.b64encode(rsa.encrypt(f'{string}'.encode(), pubkey))).decode())
return result
def calculate_md5_sign(params):
return hashlib.md5('&'.join(sorted(params.split('&'))).encode('utf-8')).hexdigest()
def login(username, password):
urlToken = "https://m.cloud.189.cn/udb/udb_login.jsp?pageId=1&pageKey=default&clientType=wap&redirectURL=https://m.cloud.189.cn/zhuanti/2021/shakeLottery/index.html"
r = s.get(urlToken)
pattern = r"https?://[^\s'\"]+" # httpまたはhttpsで始まるurlをマッチ
match = re.search(pattern, r.text) # テキスト内でマッチを検索
if match: # マッチが見つかった場合
url = match.group() # マッチした文字列を取得
else: # マッチが見つからなかった場合
print("urlが見つかりませんでした")
return None
r = s.get(url)
pattern = r"<a id=\"j-tab-login-link\"[^>]*href=\"([^\"]+)\"" # idがj-tab-login-linkのaタグをマッチし、hrefの引用符内の内容をキャプチャ
match = re.search(pattern, r.text) # テキスト内でマッチを検索
if match: # マッチが見つかった場合
href = match.group(1) # キャプチャした内容を取得
else: # マッチが見つからなかった場合
print("hrefリンクが見つかりませんでした")
return None
r = s.get(href)
captchaToken = re.findall(r"captchaToken' value='(.+?)'", r.text)[0]
lt = re.findall(r'lt = "(.+?)"', r.text)[0]
returnUrl = re.findall(r"returnUrl= '(.+?)'", r.text)[0]
paramId = re.findall(r'paramId = "(.+?)"', r.text)[0]
j_rsakey = re.findall(r'j_rsaKey" value="(\S+)"', r.text, re.M)[0]
s.headers.update({"lt": lt})
username = rsa_encode(j_rsakey, username)
password = rsa_encode(j_rsakey, password)
url = "https://open.e.189.cn/api/logbox/oauth2/loginSubmit.do"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/76.0',
'Referer': 'https://open.e.189.cn/',
}
data = {
"appKey": "cloud",
"accountType": '01',
"userName": f"{{RSA}}{username}",
"password": f"{{RSA}}{password}",
"validateCode": "",
"captchaToken": captchaToken,
"returnUrl": returnUrl,
"mailSuffix": "@189.cn",
"paramId": paramId
}
r = s.post(url, data=data, headers=headers, timeout=5)
if r.json()['result'] == 0:
print(r.json()['msg'])
else:
print(r.json()['msg'])
redirect_url = r.json()['toUrl']
r = s.get(redirect_url)
return s
def send_email(subject, content):
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = Header(subject, 'utf-8')
text_part = MIMEText(content, 'plain', 'utf-8')
msg.attach(text_part)
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls() # セキュアな接続を開始
server.login(sender_email, sender_password)
server.sendmail(sender_email, receiver_email, msg.as_string())
print("メールが正常に送信されました")
except Exception as e:
print("メールの送信に失敗しました:", str(e))
finally:
if 'server' in locals():
server.quit()
def main():
s = login(username, password)
if not s:
print("ログインに失敗しました")
return
rand = str(round(time.time() * 1000))
surl = f'https://api.cloud.189.cn/mkt/userSign.action?rand={rand}&clientType=TELEANDROID&version=8.6.3&model=SM-G930K'
url = f'https://m.cloud.189.cn/v2/drawPrizeMarketDetails.action?taskId=TASK_SIGNIN&activityId=ACT_SIGNIN'
url2 = f'https://m.cloud.189.cn/v2/drawPrizeMarketDetails.action?taskId=TASK_SIGNIN_PHOTOS&activityId=ACT_SIGNIN'
url3 = f'https://m.cloud.189.cn/v2/drawPrizeMarketDetails.action?taskId=TASK_2022_FLDFS_KJ&activityId=ACT_SIGNIN'
headers = {
'User-Agent': 'Mozilla/5.0 (Linux; Android 5.1.1; SM-G930K Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.136 Mobile Safari/537.36 Ecloud/8.6.3 Android/22 clientId/355325117317828 clientModel/SM-G930K imsi/460071114317824 clientChannelId/qq proVersion/1.0.6',
"Referer": "https://m.cloud.189.cn/zhuanti/2016/sign/index.jsp?albumBackupOpened=1",
"Host": "m.cloud.189.cn",
"Accept-Encoding": "gzip, deflate",
}
response = s.get(surl, headers=headers)
netdiskBonus = response.json()['netdiskBonus']
if response.json()['isSign'] == "false":
print(f"未チェックイン、チェックインで{netdiskBonus}Mのスペースを獲得")
res1 = f"未チェックイン、チェックインで{netdiskBonus}Mのスペースを獲得"
else:
print(f"すでにチェックイン済み、チェックインで{netdiskBonus}Mのスペースを獲得")
res1 = f"すでにチェックイン済み、チェックインで{netdiskBonus}Mのスペースを獲得"
response = s.get(url, headers=headers)
if "errorCode" in response.text:
print(response.text)
res2 = ""
else:
description = response.json()['description']
print(f"抽選で{description}を獲得")
res2 = f"抽選で{description}を獲得"
response = s.get(url2, headers=headers)
if "errorCode" in response.text:
print(response.text)
res3 = ""
else:
description = response.json()['description']
print(f"抽選で{description}を獲得")
res3 = f"抽選で{description}を獲得"
response = s.get(url3, headers=headers)
if "errorCode" in response.text:
print(response.text)
res4 = ""
else:
description = response.json()['description']
print(f"リンク3の抽選で{description}を獲得")
res4 = f"リンク3の抽選で{description}を獲得"
title = "天翼クラウドチェックイン"
content = f"""
{res1}
{res2}
{res3}
{res4}
"""
# ここに通知を送信するコードを追加できます。具体的な通知サービスのインターフェースに基づいて実装します。
send_email(title, content)
if __name__ == "__main__":
main()
ソースコードは Github @QinglongScript から取得され、青龍パネルの通知をメール通知に変更しました。