From b5cd47758b43f597cb70c546bfde0dcff789d3cd Mon Sep 17 00:00:00 2001 From: wlt233 <1486185683@qq.com> Date: Thu, 2 Jan 2025 11:38:34 +0800 Subject: [PATCH] feat: config.json file (v1.0.1) --- .gitignore | 2 +- config_template.json | 12 ++++++++++++ requirements.txt | 4 ++++ run.bat | 24 ++++++++++++++++++++++++ twitter.py | 25 +++++++++++++------------ 5 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 config_template.json create mode 100644 requirements.txt create mode 100644 run.bat diff --git a/.gitignore b/.gitignore index 9676e2d..ebd1515 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ - +config.json chromedriver.exe diff --git a/config_template.json b/config_template.json new file mode 100644 index 0000000..ede85e3 --- /dev/null +++ b/config_template.json @@ -0,0 +1,12 @@ +{ + "userid": "foobar123", + "username": "114514@1919.com", + "password": "810810", + "callback_url": "http://localhost:114514/xxx", + "check_interval": 42, + "check_list": { + "1145141919": { + "url": "https://x.com/i/lists/1873731145141919810" + } + } +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fa9c7ab --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +requests +loguru +retry +selenium \ No newline at end of file diff --git a/run.bat b/run.bat new file mode 100644 index 0000000..46ad0b8 --- /dev/null +++ b/run.bat @@ -0,0 +1,24 @@ +@REM @echo off +@REM set SCRIPT_NAME=twitter.py +@REM set SCRIPT_PATH=C:\users\wlt233\Desktop\Overflow\asset\twitter\twitter.py +@REM set CHECK_INTERVAL=30 + +@REM :LOOP +@REM rem 检查 Python 脚本是否正在运行 +@REM wmic process where "caption='python.exe' and commandline like '%%twitter.py%%'" get commandline 2>nul | findstr /I "%SCRIPT_NAME%" >nul +@REM if %errorlevel% neq 0 ( +@REM echo Script not running, starting... +@REM start "" "C:\Python39\python.exe" "%SCRIPT_PATH%" +@REM ) else ( +@REM echo Script running +@REM ) + +@REM rem 等待 CHECK_INTERVAL 秒后再次检查 +@REM timeout /T %CHECK_INTERVAL% >nul +@REM goto LOOP + + +:start +start /wait C:\Python39\python.exe C:\path\to\twitter\twitter.py +echo script dead... restarting +goto start \ No newline at end of file diff --git a/twitter.py b/twitter.py index 20a5e1b..7181345 100644 --- a/twitter.py +++ b/twitter.py @@ -171,31 +171,32 @@ def check_timeline(driver, url): -def main(userid, username, password, config): +def main(config): + userid = config["userid"] # screenid (@后面那个) + username = config["username"] # 登录用户名或邮箱 + password = config["password"] # 密码 driver = login(userid, username, password) while 1: json_data = {} - for group_id, url in config.items(): - new_tweets = check_timeline(driver, url) + check_list = config.get("check_list", []) + for group_id, group_config in check_list.items(): + new_tweets = check_timeline(driver, group_config["url"]) if new_tweets: json_data[group_id] = new_tweets if json_data: pprint(json_data) try: - requests.post("http://localhost:8520/twitter", json=json_data) + requests.post(config["callback_url"], json=json_data) except Exception as e: logger.error(str(e)) - time.sleep(55) + time.sleep(config.get("check_interval", 42)) if __name__ == "__main__": - userid = "" - username = "" - password = "" - config = { - "": "https://x.com/i/lists/<...>", - } - main(userid, username, password, config) + with open("config.json", 'r') as f: + config = json.load(f) + main(config) + # with open("lovelive.json", 'r') as f: pprint(parse_timeline(json.load(f)))