| |
|
|
| 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() |
|
|