File size: 1,533 Bytes
73dfc9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys

page_start_delimiters = ["The Great Gatsby", "Free eBooks at Planet eBook.com"]

chapter_number = 0
pages = []
current_page = ""
page_number = 0

with open(sys.argv[1]) as f:
    text = f.readlines()
for line in text:
    line = line.strip()
    # Check if the line matches any of the chapter delimiters
    if "chapter" in line.lower():
        # If so, increment the chapter number
        chapter_number += 1
        continue
    # Check if the line matches any of the start delimiters
    if any(start in line for start in page_start_delimiters):
        # If so, append the current page to the list of pages and start a new page
        if current_page:
            page_number += 1
            pages.append({
                "chapter": chapter_number,
                "page": current_page,
                "page_number": page_number
            })
            current_page = ""
        continue
    # Append the current line to the current page
    current_page += line + "\n"

# Append the final page if there is any remaining text
if current_page:
    page_number += 1
    pages.append({
        "chapter": chapter_number,
        "page": current_page,
        "page_number": page_number
    })

# Print the resulting pages
for i, page in enumerate(pages[:5]):
    print(f"Chapter {page['chapter']}, Page {page['page_number']}:\n{page['page'][0:100]}...\n...{page['page'][-100:]}")
    print("-" * 80)

# dump the pages array into a json file
import json
with open('pages.json', 'w') as f:
    json.dump(pages, f)