upload
This commit is contained in:
@@ -0,0 +1,215 @@
|
||||
import json
|
||||
import re
|
||||
from datetime import datetime
|
||||
import pymysql
|
||||
import requests
|
||||
|
||||
# ==================== 运行参数(改这里) ====================
|
||||
DB_HOST = '192.168.5.5'
|
||||
DB_PORT = 3306
|
||||
DB_NAME = 'project_proxy'
|
||||
DB_USER = 'root'
|
||||
DB_PWD = 'Aliyun@7646!'
|
||||
API_BASE = 'https://backendapi.quansuip.com/api'
|
||||
MOBILE = '18027147646'
|
||||
PASSWORD = 'ader123'
|
||||
UNIQUE_ID = 'bpwgjeak'
|
||||
# ============================================================
|
||||
|
||||
|
||||
def login_token() -> str:
|
||||
"""账号密码登录,拿 result.token。过期不用手改 Bearer。"""
|
||||
resp = requests.post(
|
||||
f'{API_BASE}/home/account/login',
|
||||
headers={'Content-Type': 'application/json', 'Unique-Id': UNIQUE_ID},
|
||||
json={'mobile': MOBILE, 'password': PASSWORD, 'auto_login': 1},
|
||||
timeout=30,
|
||||
)
|
||||
data = resp.json()
|
||||
token = ''
|
||||
if isinstance(data.get('result'), dict):
|
||||
token = str(data['result'].get('token') or '').strip()
|
||||
if data.get('code') != 200 or not token:
|
||||
raise RuntimeError(f"登录失败 code={data.get('code')} message={data.get('message')}")
|
||||
print(f'[login] {MOBILE} 已换 token')
|
||||
return token
|
||||
|
||||
|
||||
def get_req():
|
||||
token = login_token()
|
||||
headers = {
|
||||
"accept": "*/*",
|
||||
"accept-language": "zh-CN,zh;q=0.9",
|
||||
"authorization": f"Bearer {token}",
|
||||
"content-type": "application/json",
|
||||
"origin": "https://www.yiyouip.com",
|
||||
"priority": "u=1, i",
|
||||
"referer": "https://www.yiyouip.com/",
|
||||
"sec-ch-ua": "\"Not-A.Brand\";v=\"99\", \"Google Chrome\";v=\"157\", \"Chromium\";v=\"157\"",
|
||||
"sec-ch-ua-mobile": "?0",
|
||||
"sec-ch-ua-platform": "\"Windows\"",
|
||||
"sec-fetch-dest": "empty",
|
||||
"sec-fetch-mode": "cors",
|
||||
"sec-fetch-site": "cross-site",
|
||||
"unique-id": UNIQUE_ID,
|
||||
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/157.0.0.0 Safari/537.36"
|
||||
}
|
||||
url = f'{API_BASE}/home/order/nodelist'
|
||||
data = {
|
||||
"page": 1,
|
||||
"page_size": 50,
|
||||
"order_field": "id",
|
||||
"order_type": "desc"
|
||||
}
|
||||
response = requests.post(url, headers=headers, json=data)
|
||||
|
||||
print(response.text)
|
||||
print(response)
|
||||
return response.json()
|
||||
|
||||
|
||||
def clean_city(area_name: str) -> str:
|
||||
name = (area_name or '').strip()
|
||||
if '|' in name:
|
||||
name = name.split('|')[-1].strip()
|
||||
name = re.sub(r'【[^】]*】', '', name)
|
||||
name = re.sub(r'\d+.*$', '', name).strip()
|
||||
return name
|
||||
|
||||
|
||||
def ts_date(ts) -> str:
|
||||
return datetime.fromtimestamp(int(ts)).strftime('%Y-%m-%d')
|
||||
|
||||
|
||||
def prox_json(user: str, pwd: str, ip: str, port: str) -> str:
|
||||
url = f'http://{user}:{pwd}@{ip}:{port}'
|
||||
return json.dumps({'http': url, 'https': url}, ensure_ascii=False)
|
||||
|
||||
|
||||
def _row_payload(item: dict) -> tuple:
|
||||
ip = str(item['apiip']).strip()
|
||||
port = str(item['apiport']).strip()
|
||||
user = str(item['username'])
|
||||
pwd = str(item['password'])
|
||||
return (
|
||||
clean_city(item.get('area_name') or ''),
|
||||
prox_json(user, pwd, ip, port),
|
||||
ip,
|
||||
port,
|
||||
user,
|
||||
pwd,
|
||||
ts_date(item['created_at']),
|
||||
ts_date(item['end_at']),
|
||||
)
|
||||
|
||||
|
||||
def _write_row(cur, index: int, payload: tuple):
|
||||
cur.execute(
|
||||
'''
|
||||
UPDATE proxs_list
|
||||
SET city=%s, prox_data=%s, ip_address=%s, port=%s,
|
||||
acount=%s, pwd=%s, dll=%s, channel=%s
|
||||
WHERE `index`=%s
|
||||
''',
|
||||
(*payload, index),
|
||||
)
|
||||
|
||||
|
||||
def _read_row(cur, index: int) -> tuple:
|
||||
cur.execute(
|
||||
'''
|
||||
SELECT city, CAST(prox_data AS CHAR), ip_address, port, acount, pwd, dll, channel
|
||||
FROM proxs_list WHERE `index`=%s
|
||||
''',
|
||||
(index,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
prox = row[1]
|
||||
if isinstance(prox, (bytes, bytearray)):
|
||||
prox = prox.decode('utf-8')
|
||||
return (row[0], prox, str(row[2]).strip(), str(row[3]).strip(), row[4], row[5], row[6], row[7])
|
||||
|
||||
|
||||
def sync_proxs(items: list):
|
||||
"""按 ip+端口更新;没有则插入。同城多条时,已开通的代理放到最大 index,查找时不会被过期条盖住。"""
|
||||
conn = pymysql.connect(
|
||||
host=DB_HOST,
|
||||
port=DB_PORT,
|
||||
user=DB_USER,
|
||||
password=DB_PWD,
|
||||
database=DB_NAME,
|
||||
charset='utf8mb4',
|
||||
connect_timeout=10,
|
||||
)
|
||||
updated = inserted = 0
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
for item in items:
|
||||
payload = _row_payload(item)
|
||||
ip, port = payload[2], payload[3]
|
||||
cur.execute(
|
||||
'''
|
||||
SELECT `index` FROM proxs_list
|
||||
WHERE REPLACE(REPLACE(ip_address, '\t', ''), ' ', '')=%s
|
||||
AND REPLACE(REPLACE(port, '\t', ''), ' ', '')=%s
|
||||
ORDER BY `index`
|
||||
''',
|
||||
(ip, port),
|
||||
)
|
||||
hits = [r[0] for r in cur.fetchall()]
|
||||
if hits:
|
||||
for index in hits:
|
||||
_write_row(cur, index, payload)
|
||||
updated += len(hits)
|
||||
else:
|
||||
cur.execute(
|
||||
'''
|
||||
INSERT INTO proxs_list
|
||||
(city, prox_data, ip_address, port, acount, pwd, dll, channel)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
||||
''',
|
||||
payload,
|
||||
)
|
||||
inserted += 1
|
||||
# 同城:已开通换到最大 index(列表靠前的后处理,最新一条留在最后)
|
||||
opened = [i for i in items if i.get('status_text') != '已过期']
|
||||
for item in reversed(opened):
|
||||
payload = _row_payload(item)
|
||||
city, ip, port = payload[0], payload[2], payload[3]
|
||||
cur.execute(
|
||||
'SELECT `index` FROM proxs_list WHERE city=%s ORDER BY `index` DESC LIMIT 1',
|
||||
(city,),
|
||||
)
|
||||
top = cur.fetchone()
|
||||
cur.execute(
|
||||
'''
|
||||
SELECT `index` FROM proxs_list
|
||||
WHERE REPLACE(REPLACE(ip_address, '\t', ''), ' ', '')=%s
|
||||
AND REPLACE(REPLACE(port, '\t', ''), ' ', '')=%s
|
||||
ORDER BY `index` LIMIT 1
|
||||
''',
|
||||
(ip, port),
|
||||
)
|
||||
own = cur.fetchone()
|
||||
if not top or not own or top[0] == own[0]:
|
||||
continue
|
||||
top_payload = _read_row(cur, top[0])
|
||||
_write_row(cur, top[0], payload)
|
||||
_write_row(cur, own[0], top_payload)
|
||||
conn.commit()
|
||||
print(f'[db] project_proxy.proxs_list 命中更新 {updated} 行,新增 {inserted} 行,列表 {len(items)} 条')
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def want_prox(item: dict) -> bool:
|
||||
if item.get('status_text') == '已过期':
|
||||
return False
|
||||
return item.get('combo_type_text') == '新长效住宅(定制)'
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
json_tk = get_req()
|
||||
picked = [i for i in json_tk['result']['list'] if want_prox(i)]
|
||||
print(f'[db] 未过期且新长效住宅(定制) {len(picked)} 条')
|
||||
sync_proxs(picked)
|
||||
Reference in New Issue
Block a user