diff --git a/twitter.py b/twitter.py index 72a2475..49369d9 100644 --- a/twitter.py +++ b/twitter.py @@ -95,8 +95,8 @@ def parse_timeline(data): for entry in entries: try: result += parse_entry(entry) - except: - logger.error(f"error when parsing entry: {entry}") + except Exception as e: + logger.error(f"error when parsing entry: {e} {e.args}\n{entry}") result.sort(key=lambda x: x["timestamp"], reverse=True) return result @@ -122,8 +122,8 @@ def parse_content(content): if "retweeted_status_result" in tweet["legacy"]: data["retweeted"] = parse_tweet(tweet["legacy"]["retweeted_status_result"]["result"]) return data - except: - logger.error(f"error when parsing tweet: {tweet}") + except Exception as e: + logger.error(f"error when parsing tweet: {e} {e.args}\n{tweet}") return {} def parse_media(media): @@ -150,10 +150,26 @@ def parse_entities(entity): def parse_card(card): data = {} for v in card["legacy"]["binding_values"]: - if "choice" in v["key"] or v["key"] in ["end_datetime_utc"]: + if "choice" in v["key"] or v["key"] in ["end_datetime_utc", "unified_card"]: value_name = f"{v['value']['type'].lower()}_value" data[v["key"]] = v['value'].get(value_name, "") - return data + + photo = None + if "unified_card" in data: + card_data = json.loads(data["unified_card"]) + del data["unified_card"] + try: + for k, v in card_data["media_entities"].items(): + if "media_url_https" in v: + photo = { + "url": v["media_url_https"] + "?name=orig", + "video": "" + } + break + except: + logger.error(f"error parsing unified_card {card_data}") + + return data, photo def parse_tweet(tweet): # with open("tweet.json", "w") as f: json.dump(tweet, f) @@ -184,7 +200,8 @@ def parse_tweet(tweet): data["entities"].sort(key=lambda x: x["indices"][0]) if "card" in tweet: - data["card"] = parse_card(tweet["card"]) + data["card"], _photo = parse_card(tweet["card"]) + if _photo: data["media"].append(_photo) return data @@ -273,6 +290,6 @@ def main(config): if __name__ == "__main__": with open("config.json", 'r') as f: config = json.load(f) - main(config) + # main(config) - # with open("lovelive.json", 'r', encoding="utf8") as f: pprint(parse_timeline(json.load(f))) + with open("lovelive.json", 'r', encoding="utf8") as f: pprint(parse_timeline(json.load(f)))