from typing import Dict, List

import requests
from bs4 import BeautifulSoup


class Pastebin:
    RAW_URL = "https://pastebin.com/raw"
    ARCHIVE_URL = "https://pastebin.com/archive"

    def __init__(self) -> None:
        self.session = requests.Session()
        self.session.headers.update(
            {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko)"}
        )

    @staticmethod
    def parse_archive_content(html: str) -> List[Dict[str, str]]:
        soup = BeautifulSoup(html, "html.parser")
        pastes = soup.find("table", class_="maintable").find_all("tr")[1:]
        pastes_list = []

        for paste in pastes:
            cells = paste.find_all("td")
            paste_id = cells[0].a.get("href")[1:]
            paste_syntax = cells[2].a.text
            pastes_list.append({"id": paste_id, "syntax": paste_syntax})

        return pastes_list

    def get_archive(self) -> List[Dict[str, str]]:
        r = self.session.get(self.ARCHIVE_URL)
        pastes = self.parse_archive_content(r.text)
        return pastes

    def get_paste(self, paste_id: str) -> str:
        url = f"{self.RAW_URL}/{paste_id}"
        r = self.session.get(url)
        return r.text
