Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import json
- def read_json(file_name):
- with open(file_name) as input_file:
- return json.load(input_file)
- def write_json(file_name, tweets):
- with open(file_name, "w") as output_file:
- json.dump(tweets, output_file, sort_keys=True, indent=2, ensure_ascii=False)
- def count_deep(replies):
- total = len(replies)
- if total == 0:
- return total
- for reply in replies:
- inner_replies = reply.get("replies", [])
- if inner_replies:
- total += count_deep(inner_replies)
- return total
- def is_flat(tweet):
- for reply in tweet.get("replies", []):
- if reply.get("replies", None):
- return False
- return True
- def is_nested(tweet, recursive=False):
- replies = tweet.get("replies", [])
- if len(replies) != 1:
- return False
- reply = replies[0]
- while reply:
- replies = reply.get("replies", None)
- if not replies:
- return True
- if len(replies) > 1:
- return False
- reply = replies[0]
- def is_wtf(tweet, recursive=False):
- replies = tweet.get("replies", [])
- if not replies:
- return recursive
- if len(replies) > 1:
- return
- def unnest(tweet):
- tweet_replies = tweet.pop("replies", None)
- if not tweet_replies:
- return tweet
- unnested = []
- for reply in map(unnest, tweet_replies):
- replies: list = reply.pop("replies", [])
- replies.append(reply)
- unnested.extend(replies)
- if unnested:
- unnested.sort(key=lambda r: r["id"])
- tweet["replies"] = unnested
- return tweet
- if __name__ == "__main__":
- tweets = read_json("./threads.json")
- multi_reply = []
- wtf_reply = []
- for tweet in tweets:
- if is_flat(tweet):
- multi_reply.append(tweet)
- continue
- if is_nested(tweet):
- multi_reply.append(unnest(tweet))
- continue
- wtf_reply.append(tweet)
- write_json("./threads_unnested.json", multi_reply)
- write_json("./threads_wtf.json", wtf_reply)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement