Skip to content

Commit

Permalink
Merge pull request #260 from DDS-Derek/ruff
Browse files Browse the repository at this point in the history
Python code formatting
  • Loading branch information
DDSDerek authored Dec 28, 2024
2 parents 97d2d45 + e06dd75 commit 8e8e5c4
Show file tree
Hide file tree
Showing 15 changed files with 641 additions and 367 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,42 @@ jobs:
with:
sh_checker_comment: true
sh_checker_exclude: "^deprecation/* ^xiaoyahelper/*"

ruff:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4

-
name: Ruff
uses: astral-sh/ruff-action@v3

pylint:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.12"]
steps:
-
name: Checkout
uses: actions/checkout@v4

-
name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint
pip install typing-extensions
pip install -r aliyuntvtoken_connector/requirements.txt
pip install -r glue_python/requirements.txt
- name: Analysing the code with pylint
run: |
pylint --rcfile=$(pwd)/.pylintrc --disable=R,C0301 $(git ls-files '*.py')
2 changes: 2 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[FORMAT]
max-line-length=120
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"ms-python.python",
"ms-python.vscode-pylance",
"charliermarsh.ruff"
]
}
22 changes: 11 additions & 11 deletions aliyuntvtoken_connector/main.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
from flask import Flask, request, Response
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
# pylint: disable=C0114
# pylint: disable=C0116
import json
import base64
import requests
import requests
import json
import uuid
import hashlib
import base64
import random

import requests
from flask import Flask, request, Response
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad


app = Flask(__name__)
headers = {
"token": "6733b42e28cdba32",
'User-Agent': 'Mozilla/5.0 (Linux; U; Android 9; zh-cn; SM-S908E Build/TP1A.220624.014) AppleWebKit/533.1 (KHTML, like Gecko) Mobile Safari/533.1',
'User-Agent': 'Mozilla/5.0 (Linux; U; Android 9; zh-cn; SM-S908E Build/TP1A.220624.014) AppleWebKit/533.1 (KHTML, like Gecko) Mobile Safari/533.1', # noqa: E501
'Host': 'api.extscreen.com'
}


def h(char_array, modifier):
unique_chars = list(dict.fromkeys(char_array))
numeric_modifier = int(modifier[7:])
transformed_string = "".join([chr(abs(ord(c) - (numeric_modifier % 127) - 1) + 33 if abs(ord(c) - (numeric_modifier % 127) - 1) < 33 else
transformed_string = "".join([chr(abs(ord(c) - (numeric_modifier % 127) - 1) + 33 if abs(ord(c) - (numeric_modifier % 127) - 1) < 33 else # noqa: E501
abs(ord(c) - (numeric_modifier % 127) - 1)) for c in unique_chars])
return transformed_string

Expand Down Expand Up @@ -74,11 +74,11 @@ def oauth_token():
"refresh_token": refresh_token
}

timestamp = str(requests.get('http://api.extscreen.com/timestamp').json()['data']['timestamp'])
timestamp = str(requests.get('http://api.extscreen.com/timestamp', timeout=10).json()['data']['timestamp'])
unique_id = uuid.uuid4().hex
wifimac = str(random.randint(10**11, 10**12 - 1))

resp = requests.post("http://api.extscreen.com/aliyundrive/v3/token", data=req_body, headers={**get_params(timestamp, unique_id, wifimac), **headers})
resp = requests.post("http://api.extscreen.com/aliyundrive/v3/token", data=req_body, headers={**get_params(timestamp, unique_id, wifimac), **headers}, timeout=10) # noqa: E501
if resp.status_code == 200:
resp_data = resp.json()
ciphertext = resp_data["data"]["ciphertext"]
Expand Down
104 changes: 62 additions & 42 deletions glue_python/115cookie/115cookie.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,54 @@
# pylint: disable=C0103
# pylint: disable=C0114
#!/usr/local/bin/python3

__author__ = "ChenyangGao <https://chenyanggao.github.io>"
__license__ = "GPLv3 <https://www.gnu.org/licenses/gpl-3.0.txt>"

from flask import Flask, render_template, jsonify
import threading
import time
import os
import base64
import logging
import argparse
import qrcode
import sys
from PIL import Image
from io import BytesIO
from enum import Enum
from json import loads
from urllib.parse import urlencode
from urllib.request import urlopen, Request

import qrcode
from flask import Flask, render_template, jsonify
from PIL import Image


app = Flask(__name__)
flask_app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
last_status = 0
LAST_STATUS = 0


AppEnum = Enum("AppEnum", {
"web": 1,
"ios": 6,
"115ios": 8,
"android": 9,
"115android": 11,
"115ipad": 14,
"tv": 15,
"qandroid": 16,
"windows": 19,
"mac": 20,
"linux": 21,
"wechatmini": 22,
"alipaymini": 23,
"web": 1,
"ios": 6,
"115ios": 8,
"android": 9,
"115android": 11,
"115ipad": 14,
"tv": 15,
"qandroid": 16,
"windows": 19,
"mac": 20,
"linux": 21,
"wechatmini": 22,
"alipaymini": 23,
})


def get_enum_name(val, cls):
"""
获取值其对应的名称
"""
if isinstance(val, cls):
return val.name
try:
Expand Down Expand Up @@ -130,7 +136,7 @@ def post_qrcode_result(uid, app="web"):
"""
app = get_enum_name(app, AppEnum)
payload = {"app": app, "account": uid}
api = "https://passportapi.115.com/app/1.0/%s/1.0/login/qrcode/" % app
api = f"https://passportapi.115.com/app/1.0/{app}/1.0/login/qrcode/"
return loads(urlopen(Request(api, data=urlencode(payload).encode("utf-8"), method="POST")).read())


Expand All @@ -143,59 +149,72 @@ def get_qrcode(uid: str, /) -> str:

def qrcode_token_url(uid: str, /) -> str:
"""获取二维码图片的扫码链接
:return: 扫码链接
:return: 扫码链接
"""
return "http://115.com/scan/dg-" + uid


def poll_qrcode_status(qrcode_token, qrcode_app):
global last_status
# pylint: disable=W0603
def poll_qrcode_status(_qrcode_token, qrcode_app):
"""
循环等待扫码
"""
global LAST_STATUS
while True:
time.sleep(1)
resp = get_qrcode_status(qrcode_token)
status = resp["data"].get("status")
if status == 2:
resp = post_qrcode_result(qrcode_token["uid"], qrcode_app)
resp = get_qrcode_status(_qrcode_token)
_status = resp["data"].get("status")
if _status == 2:
resp = post_qrcode_result(_qrcode_token["uid"], qrcode_app)
cookie_data = resp['data']['cookie']
cookie_str = "; ".join("%s=%s" % t for t in cookie_data.items())
cookie_str = "; ".join(f"{key}={value}" for key, value in cookie_data.items())
if sys.platform.startswith('win32'):
with open('115_cookie.txt', 'w') as f:
with open('115_cookie.txt', 'w', encoding='utf-8') as f:
f.write(cookie_str)
else:
with open('/data/115_cookie.txt', 'w') as f:
with open('/data/115_cookie.txt', 'w', encoding='utf-8') as f:
f.write(cookie_str)
logging.info('扫码成功, cookie 已写入文件!')
last_status = 1
elif status in [-1, -2]:
LAST_STATUS = 1
elif _status in [-1, -2]:
logging.error('扫码失败')
last_status = 2
LAST_STATUS = 2


@app.route('/')
@flask_app.route('/')
def index():
qrcode_token = get_qrcode_token()["data"]
uid = qrcode_token["uid"]
"""
网页扫码首页
"""
_qrcode_token = get_qrcode_token()["data"]
uid = _qrcode_token["uid"]
qrcode_image_io = get_qrcode(uid)
qrcode_image = Image.open(qrcode_image_io)
buffered = BytesIO()
qrcode_image.save(buffered, format="PNG")
qrcode_image_b64_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
threading.Thread(target=poll_qrcode_status, args=(qrcode_token, args.qrcode_app)).start()
threading.Thread(target=poll_qrcode_status, args=(_qrcode_token, flask_app.config['QRCODE_APP'])).start()
return render_template('index.html', qrcode_image_b64_str=qrcode_image_b64_str)


@app.route('/status')
@flask_app.route('/status')
def status():
if last_status == 1:
"""
扫码状态获取
"""
if LAST_STATUS == 1:
return jsonify({'status': 'success'})
elif last_status == 2:
elif LAST_STATUS == 2:
return jsonify({'status': 'failure'})
else:
return jsonify({'status': 'unknown'})


@app.route('/shutdown_server', methods=['GET'])
@flask_app.route('/shutdown_server', methods=['GET'])
def shutdown():
"""
退出进程
"""
os._exit(0)


Expand All @@ -205,7 +224,8 @@ def shutdown():
parser.add_argument('--qrcode_app', type=str, default='alipaymini', help='扫码绑定设备')
args = parser.parse_args()
if args.qrcode_mode == 'web':
app.run(host='0.0.0.0', port=34256)
flask_app.config['QRCODE_APP'] = args.qrcode_app
flask_app.run(host='0.0.0.0', port=34256)
elif args.qrcode_mode == 'shell':
qrcode_token = get_qrcode_token()["data"]
threading.Thread(target=poll_qrcode_status, args=(qrcode_token, args.qrcode_app,)).start()
Expand All @@ -214,7 +234,7 @@ def shutdown():
qr.make(fit=True)
logging.info('请打开 115网盘 扫描此二维码!')
qr.print_ascii(invert=True, tty=sys.stdout.isatty())
while last_status != 1 and last_status != 2:
while LAST_STATUS != 1 and LAST_STATUS != 2:
time.sleep(1)
os._exit(0)
else:
Expand Down
Loading

0 comments on commit 8e8e5c4

Please sign in to comment.