# Path: common_utils.py
# def fetch_html_page(page_url):
#     user_agent = (
#         "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) "
#         "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36"
#     )
#     headers = {"User-Agent": user_agent}
#     page = requests.get(page_url, headers=headers, timeout=10)
#     return page.text
# 
# def html_parser_from(page_html):
#     return BeautifulSoup(page_html, "html.parser")


import logging
import os
from argparse import ArgumentParser, RawDescriptionHelpFormatter
from datetime import datetime
from pathlib import Path
from subprocess import CalledProcessError
from jinja2 import Environment, FileSystemLoader
from py_executable_checklist.workflow import (
    WorkflowBase,
    notify_me,
    run_command,
    run_workflow,
)
from slug import slug
from common_utils import fetch_html_page, html_parser_from
$ cat <filename>.html | pup 'a attr{href}' >> links.txt

3. Run this script
$ EDITOR=/usr/local/bin/idea ./links_to_hugo.py --links-file .temp/links.txt --post-title "Post title" \
    --blog-directory "<full-path-to-blog-directory"  --open-in-editor

4. Review blog post in the editor and remove any links if necessary

5. Run this script to clean up any images that are left behind due to deleted links
$ ./unused_files.py -s <blog-root>/static/images -t <blog-root>/content -d

6. make deploy from blog directory
7. make commit-all from blog directory
"""




UTF_ENCODING = "utf-8"


# Common functions


def fetch_html(url, post_html_page_file):
    logging.info(f"Fetching HTML title for {url}")
    if post_html_page_file.exists():
        logging.info(f"🌕 Loading page from cache {post_html_page_file}")
        return post_html_page_file.read_text(encoding=UTF_ENCODING)


# Path: common_utils.py
# def fetch_html_page(page_url):
#     user_agent = (
#         "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) "
#         "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36"
#     )
#     headers = {"User-Agent": user_agent}
#     page = requests.get(page_url, headers=headers, timeout=10)
#     return page.text
# 
# def html_parser_from(page_html):
#     return BeautifulSoup(page_html, "html.parser")


# Path: links_to_hugo.py
import logging
import os
from argparse import ArgumentParser, RawDescriptionHelpFormatter
from datetime import datetime
from pathlib import Path
from subprocess import CalledProcessError
from jinja2 import Environment, FileSystemLoader
from py_executable_checklist.workflow import (
    WorkflowBase,
    notify_me,
    run_command,
    run_workflow,
)
from slug import slug
from common_utils import fetch_html_page, html_parser_from

$ cat <filename>.html | pup 'a attr{href}' >> links.txt

3. Run this script
$ EDITOR=/usr/local/bin/idea ./links_to_hugo.py --links-file .temp/links.txt --post-title "Post title" \
    --blog-directory "<full-path-to-blog-directory"  --open-in-editor

4. Review blog post in the editor and remove any links if necessary

5. Run this script to clean up any images that are left behind due to deleted links
$ ./unused_files.py -s <blog-root>/static/images -t <blog-root>/content -d

6. make deploy from blog directory
7. make commit-all from blog directory
"""




UTF_ENCODING = "utf-8"


# Common functions


def fetch_html(url, post_html_page_file):
    logging.info(f"Fetching HTML title for {url}")
    if post_html_page_file.exists():
        logging.info(f"🌕 Loading page from cache {post_html_page_file}")
        return post_html_page_file.read_text(encoding=UTF_ENCODING)


    page_html = fetch_html_page(url)