File size: 9,539 Bytes
9bdf6a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import os
import sys 
from pathlib import Path
import datetime

import tarfile
import xml.etree.ElementTree as ET 
from tqdm import tqdm
import re 
from itertools import chain, islice
import requests
import time

import shutil

import arxiv 

import langdetect
from langdetect import detect

from utils import Loader as Loader
from utils import make_archive

def batch_loader(seq, size):
    """
    Iterator that takes in a list `seq` and returns
    chunks of size `size` 
    """
    return [seq[pos:pos + size] for pos in range(0, len(seq), size)]


def _delete_files_except_pattern(path, pattern, transform = lambda x: None, verbose=False):
    """
    recursively
    """
    for f in os.listdir(path):
        f_path = os.path.join(path, f)
        if verbose: 
            print(f_path)
        if os.path.isfile(f_path): 
            if not re.search(pattern, f):
                os.chmod(f_path, 0o755)
                os.remove(f_path)
            else: 
                transform(f_path)
        elif os.path.isdir(f_path):
            try: 
                print(f_path)
            except UnicodeEncodeError: 
                new_path = f_path.encode("utf-8", 'replace').decode()
                os.system(f"mv \"{f_path}\" \"{new_path}\"")
                f_path = new_path

            _delete_files_except_pattern(f_path, pattern, transform=transform, verbose=verbose)

def _download_with_progress_bar(url):
    response = requests.get(url, stream=True)
    total_size_in_bytes = int(response.headers.get("content-length", 0))
    block_size = 1024  # 1 Kibibyte
    progress_bar = tqdm(total=total_size_in_bytes, unit="iB", unit_scale=True)
    to_return = bytearray()
    for data in response.iter_content(block_size):
        progress_bar.update(len(data))
        to_return += data
    progress_bar.close()
    if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes:
        raise AssertionError("ERROR, something went wrong")

    return to_return

def get_math_ids(resumption_token="init"): 
    with Loader(f"fetching metadata shard {resumption_token}..."):
        if resumption_token=="init": 
            resp = requests.get("https://export.arxiv.org/oai2?verb=ListIdentifiers&set=math&metadataPrefix=oai_dc")
        else: 
            time.sleep(5)
            resp = requests.get(f"https://export.arxiv.org/oai2?verb=ListIdentifiers&resumptionToken={resumption_token}")
     
    root = ET.fromstring(resp.content.decode("utf-8"))
    articles = root[2]

    math_ids = {}
    for article in articles: 
        if article.tag == "{http://www.openarchives.org/OAI/2.0/}resumptionToken": 
            if article.text: 
                return math_ids | get_math_ids(resumption_token=article.text)
            else: 
                return math_ids

        db_id = article[0].text
        eyed = db_id[db_id.rindex(":")+1:]
        math_ids[eyed] = True 

def clean_tex_file(path): 
    with open(path, encoding="utf-8") as f: 
        try: 
            src = f.read()
        except (UnicodeDecodeError, UnicodeError): 
            print(f"Decoding error at {path} with utf-8. Trying latin-1")
            try: 
                with open(path, encoding="latin-1") as fle: 
                    src = fle.read()
                    #print("latin-1 successful\n")
            except (UnicodeDecodeError, UnicodeError): 
                #print(f"Decoding error at {path} with latin-1. Trying utf-16")
                try: 
                    with open(path, encoding="utf-16") as fl: 
                        src = fl.read()
                        #print("utf-16 successful\n")
                except (UnicodeDecodeError, UnicodeError): 
                    #print(f"Decoding error at {path} with utf-16. Trying utf-32")
                    try: 
                        with open(path, encoding="utf-32") as f: 
                            src = f.read()
                    except (UnicodeDecodeError, UnicodeError): 
                        print(f"Decoding error at {path} with all of utf-8, 16, 32 and latin-1. Deleting this file")
                        print("This issue should only occur with a handful of quite old files. Continuing...\n")
                        return 

    end = re.search(r"\\end\{document\}", src)
    if end: 
        src = src[:end.span()[1]]

    bib = re.search(r"\\Refs|\\begin\{thebibliography\}", src)
    if bib:
        src = src[:bib.span()[0]]
    
    os.chmod(path, 0o755)
    with open(path, "w", encoding="utf-8") as f: 
        f.write(src)

def clean_tex_file_some_more(path): 
    with open(path) as f:
        text = f.read()

    text = re.sub(r"(?<!\\)%.*", "", text)

    match_obj = re.search(r"\\begin\{document\}", text)
    if match_obj: 
        text = text[match_obj.span()[0]:]

    match_obj = re.search(r"\\begin\{references\}", text)
    if match_obj: 
        text = text[:match_obj.span()[0]]

    text = text.strip()

    os.remove(path)
    if len(text)>280: 
        try: 
            print(path) 
        except UnicodeEncodeError: 
            path = path.encode('utf-8', 'replace').decode()

        try: 
            lang = detect(text)
        except langdetect.lang_detect_exception.LangDetectException: 
            # no linguistic features to analyze, delete
            return 
        
        if lang=="en": 
            with open(path, "w") as f: 
                f.write(text)
        else: 
            print("HIT NONENGLISH ARTICLE")

def process_tarball_old_scheme(tarball_name, save_dir): 
    tarball_path = os.path.join(save_dir, tarball_name)
    os.system("tar -xf " + tarball_path + " -C " + save_dir)

    last_ = tarball_name.rfind("_")
    second_last_ = tarball_name.rfind("_", 0, last_)
    subdir = tarball_name[second_last_+1:last_]
    
    subpath = os.path.join(save_dir, subdir)
    zipped_names = os.listdir(subpath)

    for zipped_name in zipped_names: 
        if zipped_name[-len(".gz"):]==".gz": 
            zipped_path = os.path.join(subpath, zipped_name)
            if re.match(r"math", zipped_name): 
                eyed = zipped_name[:-len(".gz")]
                if tarfile.is_tarfile(zipped_path): 
                    article_dir = os.path.join(subpath, eyed)
                    Path(article_dir).mkdir()
                    os.system("tar -xzf " + zipped_path + " -C " + article_dir)
                    os.remove(zipped_path)
                else: 
                    os.system("gzip -d " + zipped_path)
                    unzipped_path = os.path.join(subpath, eyed)
                    os.rename(unzipped_path, unzipped_path + ".tex")
            else: 
                os.remove(zipped_path)

    _delete_files_except_pattern(subpath, r".*\.tex", transform=clean_tex_file)
    os.remove(tarball_path)

def process_tarball(tarball_name, save_dir, math_ids): 
    tarball_path = os.path.join(save_dir, tarball_name)
    untar_cmd = "tar -xf " + tarball_path + " -C " + save_dir
    os.system(untar_cmd)
    
    last_ = tarball_name.rfind("_")
    second_last_ = tarball_name.rfind("_", 0, last_)
    subdir = tarball_name[second_last_+1:last_]
    
    subpath = os.path.join(save_dir, subdir)
    listdir = os.listdir(subpath)

    ids = [x[:-3] for x in listdir if x[-3:]==".gz"]
 
    for eyed in ids: 
        if eyed in math_ids: 
            zipped_path = os.path.join(subpath, eyed + ".gz")

            if tarfile.is_tarfile(zipped_path): 
                article_dir = os.path.join(subpath, eyed)
                Path(article_dir).mkdir()
                os.system("tar -xzf " + zipped_path + " -C " + article_dir)
                os.remove(zipped_path)
            else: 
                os.system("gzip -d " + zipped_path)
                unzipped_path = os.path.join(subpath, eyed)
                os.rename(unzipped_path, unzipped_path + ".tex")
    
    _delete_files_except_pattern(subpath, r".*\.tex", transform=clean_tex_file)
    os.remove(tarball_path)

def main(): 
    """
    Warning: this code is *extremely* brittle
    """
    math_ids = get_math_ids()

    save_dir = "arxiv_1"
    Path(save_dir).mkdir(exist_ok=True) 
    manifest_path = os.path.join(save_dir, "manifest.xml")
    
    os.system(f"s3cmd get s3://arxiv/src/arXiv_src_manifest.xml --requester-pays {manifest_path}") 

    tree = ET.parse(manifest_path)
    root = tree.getroot()
    
    shards_and_dates = []
    for child in root: 
        if child.tag == "file":
            shard = child[1].text # the index of filename
            yymm = child[9].text # the index of yymm
            shards_and_dates.append((shard, yymm))
 
    format_cutoff = datetime.datetime(2007, 3, 1) # arXiv switches from old to new format
    for shard, yymm in tqdm(shards_and_dates): 
        print("SHARD: ", shard)
        os.system(f"s3cmd get s3://arxiv/" + shard + \
                " --requester-pays " + save_dir) 
        tarball_name=shard[shard.rindex("/")+1:]
        
        # nb this code will stop working in 2051 ;) 
        year = int("19" + yymm[:2]) if int(yymm[:2])>50 else int("20"+yymm[:2])
        if datetime.datetime(year, int(yymm[2:]), 1)<=format_cutoff: 
            process_tarball_old_scheme(tarball_name, save_dir)
        else: 
            process_tarball(tarball_name, save_dir, math_ids) 

    os.remove(manifest_path)

if __name__=="__main__": 
    #main()
    #_delete_files_except_pattern("arxiv_1", r".*\.tex$", transform=clean_tex_file_some_more)
    for f in tqdm(os.listdir("arxiv")):
        f_path = os.path.join("arxiv", f)
        make_archive(f_path)