#!/usr/bin/env python
import pathlib
import sys
from pdfminer.high_level import extract_text
import time


def pdf_to_txt(pdf_path, txt_path):
    try:
        text = extract_text(pdf_path)
        with txt_path.open("w+", encoding="utf-8") as f:
            f.write(text)
    except Exception as ex:
        raise
    return True


try:
    source_path = sys.argv[1]
except IndexError:
    raise Exception(f"Usage: pdf2text [path].")

source_path = pathlib.Path(source_path)
if not source_path.exists():
    raise Exception(f"{source_path.absolute()} does not exist.")

print("This script will convert all your pdf files to txt files in the same directory.")
if input("Continue? [Y/n]: ").strip().lower() in ["n", "no"]:
    print("Operation cancelled.")
    exit(0)

for file in pathlib.Path(source_path).glob("*.pdf"):

    source_file = file.absolute()
    destination_file = pathlib.Path(str(file.absolute())[:-4] + ".txt")
    if destination_file.exists():
        print(f"Already exists, skipping: {destination_file.absolute()}.")
        continue

    print(f"Creating to {destination_file.absolute()}.")

    start = time.time()
    try:
        pdf_to_txt(source_file, destination_file)
    except Exception as ex:
        print(ex)
        # Just continue, who cares

    finish = time.time()
    duration = finish - start
    print(f"Took {duration}s.")
