-
Notifications
You must be signed in to change notification settings - Fork 6
/
harvest.py
executable file
·50 lines (42 loc) · 1.31 KB
/
harvest.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
47
48
49
50
#!/usr/bin/env python
import os
import sys
import argparse
import json
import errno
def make_sure_path_exists(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
parser = argparse.ArgumentParser("harvest")
parser.add_argument("archive_dir", action="store",
help="a directory where results are stored")
args = parser.parse_args()
if not os.path.isdir(args.archive_dir):
sys.exit("Directory " + args.archive_dir + " does not exist.")
data_dir = os.path.join(args.archive_dir, "data")
make_sure_path_exists(data_dir)
tweets_dir = os.path.join(data_dir, "tweets")
make_sure_path_exists(tweets_dir)
metadatafile = os.path.join(args.archive_dir, "metadata.json")
try:
with open(metadatafile) as json_data:
metadata = json.load(json_data)
json_data.close()
except:
sys.exit("Cannot read metadata file " + metadatafile)
sys.argv = ["", metadata["search"], tweets_dir]
# find twarc-archive.py on system path
for dirname in os.environ["PATH"].split(os.pathsep):
candidate = os.path.join(dirname, "twarc-archive.py")
print(candidate)
if os.path.isfile(candidate):
break
else:
candidate = ""
try:
execfile(candidate)
except:
sys.exit("Cannot run twarc-archive.py")