forked from pgcorpus/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_data.py
executable file
·157 lines (136 loc) · 4.98 KB
/
get_data.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
Project Gutenberg parsing with python 3.
Written by
M. Gerlach & F. Font-Clos
"""
from src.utils import populate_raw_from_mirror, list_duplicates_in_mirror
from src.metadataparser import make_df_metadata
from src.bookshelves import get_bookshelves
from src.bookshelves import parse_bookshelves
import argparse
import os
import subprocess
import pickle
if __name__ == '__main__':
parser = argparse.ArgumentParser(
"Update local PG repository.\n\n"
"This script will download all books currently not in your\n"
"local copy of PG and get the latest version of the metadata.\n"
)
# mirror dir
parser.add_argument(
"-m", "--mirror",
help="Path to the mirror folder that will be updated via rsync.",
default='data/.mirror/',
type=str)
# raw dir
parser.add_argument(
"-r", "--raw",
help="Path to the raw folder.",
default='data/raw/',
type=str)
# metadata dir
parser.add_argument(
"-M", "--metadata",
help="Path to the metadata folder.",
default='metadata/',
type=str)
# pattern matching
parser.add_argument(
"-p", "--pattern",
help="Patterns to get only a subset of books.",
default='*',
type=str)
# update argument
parser.add_argument(
"-k", "--keep_rdf",
action="store_false",
help="If there is an RDF file in metadata dir, do not overwrite it.")
# update argument
parser.add_argument(
"-owr", "--overwrite_raw",
action="store_true",
help="Overwrite files in raw.")
# quiet argument, to supress info
parser.add_argument(
"-q", "--quiet",
action="store_true",
help="Quiet mode, do not print info, warnings, etc"
)
# create the parser
args = parser.parse_args()
print('mirror folder', args.mirror, flush=True)
print('raw folder', args.raw, flush=True)
print('metadata folder', args.metadata, flush=True)
# check that all dirs exist
if not os.path.isdir(args.mirror):
os.makedirs(args.mirror, exist_ok = True)
print('Created mirror folder', args.mirror, flush=True)
# raise ValueError("The specified mirror directory does not exist.")
if not os.path.isdir(args.raw):
os.makedirs(args.raw, exist_ok = True)
print('Created raw folder', args.raw, flush=True)
# raise ValueError("The specified raw directory does not exist.")
if not os.path.isdir(args.metadata):
os.makedirs(args.metadata, exist_ok = True)
print('Created metadata folder', args.metadata, flush=True)
# raise ValueError("The specified metadata directory does not exist.")
# Update the .mirror directory via rsync
# --------------------------------------
# We sync the 'mirror_dir' with PG's site via rsync
# The matching pattern, explained below, should match
# only UTF-8 files.
# pass the -v flag to rsync if not in quiet mode
if args.quiet:
vstring = ""
else:
vstring = "v"
# Pattern to match the + but not the - :
#
# + 12345 . t x t . utf 8
# - 12345 . t x t . utf8 .gzi p
# + 12345 - 0 . t x t
#---------------------------------------------
# [.-][t0][x.]t[x.] * [t8]
sp_args = ["rsync", "-am%s" % vstring,
"--include", "*/",
"--include", "[p123456789][g0123456789]%s[.-][t0][x.]t[x.]*[t8]" % args.pattern,
"--exclude", "*",
"aleph.gutenberg.org::gutenberg", args.mirror
]
subprocess.call(sp_args)
# Get rid of duplicates
# ---------------------
# A very small portion of books are stored more than
# once in PG's site. We keep the newest one, see
# erase_duplicates_in_mirror docstring.
dups_list = list_duplicates_in_mirror(mirror_dir=args.mirror)
# Populate raw from mirror
# ------------------------
# We populate 'raw_dir' hardlinking to
# the hidden 'mirror_dir'. Names are standarized
# into PG12345_raw.txt form.
populate_raw_from_mirror(
mirror_dir=args.mirror,
raw_dir=args.raw,
overwrite=args.overwrite_raw,
dups_list=dups_list,
quiet=args.quiet
)
# Update metadata
# ---------------
# By default, update the whole metadata csv
# file each time new data is downloaded.
make_df_metadata(
path_xml=os.path.join(args.metadata, 'rdf-files.tar.bz2'),
path_out=os.path.join(args.metadata, 'metadata.csv'),
update=args.keep_rdf
)
# Bookshelves
# -----------
# Get bookshelves and their respective books and titles as dicts
BS_dict, BS_num_to_category_str_dict = parse_bookshelves()
with open("metadata/bookshelves_ebooks_dict.pkl", 'wb') as fp:
pickle.dump(BS_dict, fp)
with open("metadata/bookshelves_categories_dict.pkl", 'wb') as fp:
pickle.dump(BS_num_to_category_str_dict, fp)