File size: 2,014 Bytes
b59be0d |
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 |
#!/usr/bin/env python3
import os
import re
import requests
import shutil
import zipfile
from bs4 import BeautifulSoup
from processing import clean
def download(url, file_path):
response = requests.get(url)
if response.status_code != 200:
print(f"Download failure: {response.status_code}")
exit(1)
with open(file_path, 'wb') as f:
f.write(response.content)
print(f"Download success: {file_path}")
def unzip(archive_path, extract_path):
with zipfile.ZipFile(archive_path, 'r') as zip_ref:
zip_ref.extractall(extract_path)
print(f"Extracted: {extract_path}")
def check_existence(filename):
if os.path.exists(filename):
print(f"{filename} found.")
exit(0)
else:
print(f"No {filename}. Downloading...")
def get_tekstaro():
tekstaro_website = 'https://tekstaro.com/elshutebla'
tekstaro_version = 'tekstaro_de_esperanto_html_sen_streketoj'
tekstaro_archive = f'{tekstaro_version}.zip'
tekstaro_url = f'{tekstaro_website}/{tekstaro_archive}'
download(tekstaro_url, tekstaro_archive)
unzip(tekstaro_archive, './')
os.remove(tekstaro_archive)
shutil.move(os.path.join(tekstaro_version, 'tekstoj'), 'tekstoj')
shutil.rmtree(tekstaro_version)
shutil.rmtree(os.path.join('tekstoj', 'bildoj'))
def read_tekstaro():
tekstaro = ""
files = [file for file in os.listdir('tekstoj')]
for file in files:
with open(os.path.join('tekstoj', file)) as f:
data = f.read()
gfg = BeautifulSoup(data, "lxml")
tekstaro += gfg.get_text()
shutil.rmtree('tekstoj')
return tekstaro
def write_tekstaro(filename, tekstaro):
with open(filename, 'w') as f:
f.write(tekstaro)
def main():
filename = 'tekstaro.txt'
check_existence(filename)
tekstaro = get_tekstaro()
tekstaro = read_tekstaro()
tekstaro = clean(tekstaro)
write_tekstaro(filename, tekstaro)
if __name__ == "__main__":
main()
|