-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathdeduplicate.py
executable file
·46 lines (38 loc) · 1.16 KB
/
deduplicate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
"""
Given a JSON file, remove any tweets with duplicate IDs.
Optionally, this will extract retweets. (That is, for a retweet
use tweet from retweeted_status and retweet.)
Example usage:
utils/deduplicate.py tweets.jsonl > tweets_deduped.jsonl
"""
from __future__ import print_function
import json
import fileinput
import argparse
def main(files, extract_retweets=False):
seen = {}
for line in fileinput.input(files=files):
tweet = json.loads(line)
if extract_retweets and "retweeted_status" in tweet:
tweet = tweet["retweeted_status"]
id = tweet["id"]
if id not in seen:
seen[id] = True
print(json.dumps(tweet))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--extract-retweets", action="store_true", help="Extract retweets"
)
parser.add_argument(
"files",
metavar="FILE",
nargs="*",
help="files to read, if empty, stdin is used",
)
args = parser.parse_args()
main(
args.files if len(args.files) > 0 else ("-",),
extract_retweets=args.extract_retweets,
)