# Author  Ralfs Lāns
# Created 05.03.2024
# Editor Raivis Rampāns
# Modified 11.03.2024

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Firefox()

driver.get("https://myanimelist.net/topanime.php")

# ========== Cookies ==========

cookie = WebDriverWait(driver, 30).until(
    EC.visibility_of_element_located((By.ID, "qc-cmp2-ui"))
)

cookie_close = cookie.find_elements(By.CLASS_NAME, "css-1hy2vtq")[1]

cookie_close.click()

# ========== Find animes ========== 

animes = WebDriverWait(driver, 30).until(
    EC.visibility_of_all_elements_located((By.CLASS_NAME, "ranking-list"))
)

for i, anime in enumerate(animes):
    try:
        animes = WebDriverWait(driver, 30).until(
            EC.visibility_of_all_elements_located((By.CLASS_NAME, "ranking-list"))
        )
        anime=animes[i]

        link = anime.find_element(By.CSS_SELECTOR, ".anime_ranking_h3 .hoverinfo_trigger")
        
        link.click()

    except Exception as e:
        print(f"nav labi: {e}")
        continue

    # ========== Find data ==========

    try:
         title = driver.find_element(By.CLASS_NAME, "title-english").text
    except:
        title = driver.find_element(By.CLASS_NAME, "title-name").text

    side = driver.find_element(By.CLASS_NAME, "leftside")

    img = side.find_element(By.CSS_SELECTOR, "img.lazyloaded").get_attribute("src")

    img = img[len("https://cdn.myanimelist.net/images/anime/"):]

    rows = side.find_elements(By.CLASS_NAME, "spaceit_pad")

    for z, row in enumerate(rows):
        th = rows[z].find_element(By.CLASS_NAME, "dark_text").text
        if th == "Type:":
            type = rows[z].find_element(By.TAG_NAME, "a").text
        if th == "Episodes:":
            episode = rows[z].text.replace("Episodes: ", "")
        if th == "Aired:":
            aired = rows[z].text.replace("Aired: ", "").replace(",", "")
        if th == "Premiered:":
            season = rows[z].find_element(By.TAG_NAME, "a").text.replace(" ", ",")
        if th == "Licensors:":
            licensors = rows[z].text.replace("Licensors: ", "").replace(", add some", "")
        if th == "Studios:":
            studios = rows[z].find_element(By.TAG_NAME, "a").text
        if th == "Source:":
            source = rows[z].text.replace("Source: ", "")
        if th == "Genres:":
            genres = rows[z].text.replace("Genres: ", "")
        if th == "Rating:":
            rating = rows[z].text.replace("Rating: ", "")
        else:
            continue

    desc = driver.find_elements(By.CSS_SELECTOR, "tr td p")[0].text.replace("[Written by MAL Rewrite]", "")

    print(f"{title},{img},{season},{type},{rating},{source},\n{desc},\n{genres},{studios},{licensors},{episode},{aired}")
    print("————————————————————————————————————————————————————————————————————————————————————————————————————————————————————")
        
    driver.back() # Back to animes

driver.quit()