-
Notifications
You must be signed in to change notification settings - Fork 255
/
filter_date.py
executable file
·46 lines (33 loc) · 1.3 KB
/
filter_date.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 minimum and/or maximum date, filter out all tweets after this date.
For example, if a hashtag was used for another event before the one you're
interested in, you can filter out the old ones.
Example usage:
utils/filter_date.py --mindate 1-may-2014 tweets.jsonl > filtered.jsonl
"""
from __future__ import print_function
import sys
import json
import fileinput
import argparse
import datetime
from dateutil.parser import parse
def filter_input(mindate, maxdate, files):
mindate = parse(mindate) if mindate is not None else datetime.datetime.min
maxdate = parse(maxdate) if maxdate is not None else datetime.datetime.max
for line in fileinput.input(files):
tweet = json.loads(line)
created_at = parse(tweet["created_at"])
created_at = created_at.replace(tzinfo=None)
if mindate < created_at and maxdate > created_at:
print(json.dumps(tweet))
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--mindate", help="the minimum date", default=None)
parser.add_argument("--maxdate", help="the maximum date", default=None)
parser.add_argument("files", nargs="?", default=[])
args = parser.parse_args()
filter_input(args.mindate, args.maxdate, args.files)
if __name__ == "__main__":
main()