DailyMotion/dump_keyword_title.py

85 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
用法示例
--------
# 用默认 level=1
python3 dump_keyword_title.py
# 指定 level=3
python3 dump_keyword_title.py -l 3
"""
import json, time, pymysql, redis
import argparse
# ======= 配置区 =======
MYSQL_CONFIG = {
"host": "192.144.230.75", "port": 3306,
"user": "db_vidcon", "password": "rexdK4fhCCiRE4BZ",
"database": "db_vidcon", "charset": "utf8mb4",
"cursorclass": pymysql.cursors.DictCursor
}
REDIS_CONFIG = {
"host": "192.144.230.75", "port": 6379,
"password": "qwert@$123!&", "decode_responses": True
}
LIST_KEY = "video_kw_queue"
BATCH_SIZE = 1000
SQL = """
SELECT
k.keyword,
k.rn,
t.title AS v_name,
ANY_VALUE(t.level) AS level
FROM
sh_dm_keyword k
LEFT JOIN
sh_dm_title t ON k.title = t.title
WHERE
k.status = 1
AND t.status = 1
AND NOT EXISTS (
SELECT 1 FROM sh_dm_black_keyword b WHERE b.title = t.title
)
AND t.level = %s
GROUP BY k.keyword, k.rn
"""
def parse_args():
parser = argparse.ArgumentParser(
description="Dump keyword/title rows into Redis list."
)
parser.add_argument("-l", "--level", type=int, default=99,
help="value for t.level (default: 99)")
parser.add_argument("-u", "--urgent", type=int, default=0,
help="加急标记")
return parser.parse_args()
def main():
args = parse_args()
batch_ts = int(time.time())
conn = pymysql.connect(**MYSQL_CONFIG)
cur = conn.cursor()
cur.execute(SQL, (args.level,))
r = redis.Redis(**REDIS_CONFIG)
pipe = r.pipeline()
total = 0
start = time.time()
global LIST_KEY
if args.urgent == 1:
LIST_KEY = "video_urgent_queue"
for row in cur:
row["batch"] = batch_ts
pipe.lpush(LIST_KEY, json.dumps(row, ensure_ascii=False))
total += 1
if total % BATCH_SIZE == 0:
pipe.execute()
if pipe.command_stack:
pipe.execute()
print(f"✔ 推送 {total}level={args.level}, batch={batch_ts})→ Redis '{LIST_KEY}',耗时 {time.time()-start:.2f}s")
if __name__ == "__main__":
main()