# Written by retoor@molodetz.nl

# This script enables downloading, processing, and caching web content, including taking website screenshots and repairing links in HTML content.

# Imports used: aiohttp, aiohttp.web for creating web servers and handling async requests; app.cache for caching utilities; BeautifulSoup from bs4 for HTML parsing; imgkit for creating screenshots.

# The MIT License (MIT)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


import asyncio
import pathlib
import uuid
import zlib
from urllib.parse import urljoin

import aiohttp
import imgkit
from app.cache import time_cache_async
from bs4 import BeautifulSoup


async def crc32(data):
    try:
        data = data.encode()
    except:
        pass
    return "crc32" + str(zlib.crc32(data))


async def get_file(name, suffix=".cache"):
    name = await crc32(name)
    path = pathlib.Path(".").joinpath("cache")
    if not path.exists():
        path.mkdir(parents=True, exist_ok=True)
    return path.joinpath(name + suffix)


async def public_touch(name=None):
    path = pathlib.Path(".").joinpath(str(uuid.uuid4()) + name)
    path.open("wb").close()
    return path


async def create_site_photo(url):
    loop = asyncio.get_event_loop()
    if not url.startswith("https"):
        url = "https://" + url
    output_path = await get_file("site-screenshot-" + url, ".png")

    if output_path.exists():
        return output_path
    output_path.touch()

    def make_photo():
        imgkit.from_url(url, output_path.absolute())
        return output_path

    return await loop.run_in_executor(None, make_photo)


async def repair_links(base_url, html_content):
    soup = BeautifulSoup(html_content, "html.parser")
    for tag in soup.find_all(["a", "img", "link"]):
        if tag.has_attr("href") and not tag["href"].startswith("http"):
            tag["href"] = urljoin(base_url, tag["href"])
        if tag.has_attr("src") and not tag["src"].startswith("http"):
            tag["src"] = urljoin(base_url, tag["src"])
    return soup.prettify()


async def is_html_content(content: bytes):
    if not content:
        return False
    try:
        content = content.decode(errors="ignore")
    except:
        pass
    marks = ["<html", "<img", "<p", "<span", "<div"]
    content = content.lower()
    for mark in marks:
        if mark in content:
            return True
    return False


@time_cache_async(120)
async def get(url):
    async with aiohttp.ClientSession() as session:
        response = await session.get(url)
        content = await response.text()
        if await is_html_content(content):
            content = (await repair_links(url, content)).encode()
        return content
