diff --git a/config_template.json b/config_template.json index 471b03f..0339daa 100644 --- a/config_template.json +++ b/config_template.json @@ -4,9 +4,12 @@ "password": "810810", "callback_url": "http://localhost:114514/xxx", "check_interval": 42, + "check_interval_slow": 600, + "slow_hours": [0, 1, 2, 3, 4, 5, 6], "check_list": { "1145141919": { "url": "https://x.com/i/lists/1873731145141919810", + "interval": 42, "filter": [ "only_image", "only_origin" diff --git a/twitter.py b/twitter.py index ed3aeb8..313def6 100644 --- a/twitter.py +++ b/twitter.py @@ -1,3 +1,4 @@ +from collections import defaultdict import json import time from datetime import datetime @@ -112,7 +113,7 @@ def parse_entry(entry): def parse_content(content): tweet = content["itemContent"]["tweet_results"]["result"] - if not "rest_id" in tweet: tweet = tweet["tweet"] + while not "rest_id" in tweet: tweet = tweet["tweet"] data = parse_tweet(tweet) if "quoted_status_result" in tweet: data["quoted"] = parse_tweet(tweet["quoted_status_result"]["result"]) @@ -142,6 +143,8 @@ def parse_entities(entity): return data def parse_tweet(tweet): + # with open("tweet.json", "w") as f: json.dump(tweet, f) + while not "rest_id" in tweet: tweet = tweet["tweet"] data = { "rest_id": tweet["rest_id"], "name": tweet["core"]["user_results"]["result"]["legacy"]["name"], @@ -213,13 +216,22 @@ def main(config): password = config["password"] # 密码 driver = login(userid, username, password) + check_list = config.get("check_list", []) + check_interval = config.get("check_interval", 42) + check_interval_slow = config.get("check_interval_slow", 600) + slow_hours = config.get("slow_hours", [0, 1, 2, 3, 4, 5, 6]) + last_check_time = defaultdict(lambda: 0.0) + while 1: json_data = {} - check_list = config.get("check_list", []) for group_id, group_config in check_list.items(): - new_tweets = check_timeline(driver, group_config) - if new_tweets: - json_data[group_id] = new_tweets + group_interval = group_config.get("interval", check_interval) + + if time.time() - last_check_time[group_id] > group_interval: + new_tweets = check_timeline(driver, group_config) + if new_tweets: + json_data[group_id] = new_tweets + last_check_time[group_id] = time.time() if json_data: pprint(json_data) @@ -227,12 +239,15 @@ def main(config): requests.post(config["callback_url"], json=json_data) except Exception as e: logger.error(str(e)) - - time.sleep(config.get("check_interval", 42)) + + if datetime.now().hour in slow_hours: + time.sleep(check_interval_slow) + else: + time.sleep(check_interval) if __name__ == "__main__": 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))) + # with open("lovelive.json", 'r', encoding="utf8") as f: pprint(parse_timeline(json.load(f)))