<commit_before>package org.lodder.subtools.multisubdownloader.subtitleproviders.addic7ed;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
import org.lodder.subtools.multisubdownloader.subtitleproviders.SubtitleApi;
import org.lodder.subtools.multisubdownloader.subtitleproviders.addic7ed.exception.Addic7edException;
import org.lodder.subtools.multisubdownloader.subtitleproviders.addic7ed.model.Addic7edSubtitleDescriptor;
import org.lodder.subtools.sublibrary.Language;
import org.lodder.subtools.sublibrary.Manager;
import org.lodder.subtools.sublibrary.ManagerException;
import org.lodder.subtools.sublibrary.cache.CacheType;
import org.lodder.subtools.sublibrary.data.Html;
import org.lodder.subtools.sublibrary.model.SubtitleSource;
import org.lodder.subtools.sublibrary.util.OptionalExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.experimental.ExtensionMethod;

@ExtensionMethod({ OptionalExtension.class })
public class JAddic7edApi extends Html implements SubtitleApi {

    private static final Logger LOGGER = LoggerFactory.getLogger(JAddic7edApi.class);
    private final Pattern pattern = Pattern.compile("Version (.+), Duration: ([0-9]+).([0-9])+ ");
    private final static long RATEDURATION = 1; // seconds
    private static final String DOMAIN = "https://www.addic7ed.com";
    private final boolean speedy;
    private LocalDateTime lastRequest = LocalDateTime.now();

    public JAddic7edApi(boolean speedy, Manager manager) {
        super(manager, "Mozilla/5.25 Netscape/5.0 (Windows; I; Win95)");
        this.speedy = speedy;
    }

    public JAddic7edApi(String username, String password, boolean speedy, Manager manager) throws Addic7edException {
        super(manager, "Mozilla/5.25 Netscape/5.0 (Windows; I; Win95)");
        this.speedy = speedy;
        login(username, password);
    }

    public void login(String username, String password) throws Addic7edException {
        Map<String, String> data = Map.of("username", username, "password", password, "remember", "false");
        try {
            this.postHtml(DOMAIN + "/dologin.php", data);
        } catch (ManagerException e) {
            throw new Addic7edException(e);
        }
    }

    private Optional<String> getAddictedSerieName(String name) throws Addic7edException {
        String formattedName = name.replace(":", "").replace("-", "").replace("_", " ").replace(" ", "").trim().toLowerCase();

        return getValue("%s-SerieName-%s".formatted(getSubtitleSource().name(), formattedName))
                .cacheType(CacheType.DISK)
                .optionalSupplier(() -> resultStringForName(name)
                        .map(doc -> doc.select("#season td:not(.c) > a").stream()
                                .map(serieFound -> {
                                    String link = serieFound.attr("href");
                                    String seriename = link.replace("/serie/", "");
                                    return seriename.substring(0, seriename.indexOf("/"));
                                })
                                .filter(seriename -> URLDecoder.decode(seriename, StandardCharsets.UTF_8).replace(":", "").replace("-", "")
                                        .replace("_", " ").replace(" ", "").trim().toLowerCase().equals(formattedName))
                                .findAny().orElse(null)))
                .getOptional();
    }

    private Optional<String> getAddictedMovieName(String name) throws Addic7edException {
        return getValue("%s-MovieName-".formatted(getSubtitleSource().name(), name))
                .cacheType(CacheType.DISK)
                .optionalSupplier(
                        () -> resultStringForName(name).map(doc -> {
                            Elements aTagWithSerie = doc.select("a[debug]");
                            String link = aTagWithSerie.get(0).attr("href");
                            String moviename = link.replace("movie/", "");
                            return moviename.substring(0, moviename.indexOf("/"));
                        }))
                .getOptional();
    }

    private Optional<Document> resultStringForName(String name) throws Addic7edException {<commit_msg>Revert "fixed addicted url"<commit_after>package org.lodder.subtools.multisubdownloader.subtitleproviders.addic7ed;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
import org.lodder.subtools.multisubdownloader.subtitleproviders.SubtitleApi;
import org.lodder.subtools.multisubdownloader.subtitleproviders.addic7ed.exception.Addic7edException;
import org.lodder.subtools.multisubdownloader.subtitleproviders.addic7ed.model.Addic7edSubtitleDescriptor;
import org.lodder.subtools.sublibrary.Language;
import org.lodder.subtools.sublibrary.Manager;
import org.lodder.subtools.sublibrary.ManagerException;
import org.lodder.subtools.sublibrary.cache.CacheType;
import org.lodder.subtools.sublibrary.data.Html;
import org.lodder.subtools.sublibrary.model.SubtitleSource;
import org.lodder.subtools.sublibrary.util.OptionalExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.experimental.ExtensionMethod;

@ExtensionMethod({ OptionalExtension.class })
public class JAddic7edApi extends Html implements SubtitleApi {

    private static final Logger LOGGER = LoggerFactory.getLogger(JAddic7edApi.class);
    private final Pattern pattern = Pattern.compile("Version (.+), Duration: ([0-9]+).([0-9])+ ");
    private final static long RATEDURATION = 1; // seconds
    private static final String DOMAIN = "https://www.addic7ed.com";
    private final boolean speedy;
    private LocalDateTime lastRequest = LocalDateTime.now();

    public JAddic7edApi(boolean speedy, Manager manager) {
        super(manager, "Mozilla/5.25 Netscape/5.0 (Windows; I; Win95)");
        this.speedy = speedy;
    }

    public JAddic7edApi(String username, String password, boolean speedy, Manager manager) throws Addic7edException {
        super(manager, "Mozilla/5.25 Netscape/5.0 (Windows; I; Win95)");
        this.speedy = speedy;
        login(username, password);
    }

    public void login(String username, String password) throws Addic7edException {
        Map<String, String> data = Map.of("username", username, "password", password, "remember", "false");
        try {
            this.postHtml(DOMAIN + "/dologin.php", data);
        } catch (ManagerException e) {
            throw new Addic7edException(e);
        }
    }

    private Optional<String> getAddictedSerieName(String name) throws Addic7edException {
        String formattedName = name.replace(":", "").replace("-", "").replace("_", " ").replace(" ", "").trim().toLowerCase();

        return getValue("%s-SerieName-".formatted(getSubtitleSource().name(), formattedName))
                .cacheType(CacheType.DISK)
                .optionalSupplier(() -> resultStringForName(name)
                        .map(doc -> doc.select("#season td:not(.c) > a").stream()
                                .map(serieFound -> {
                                    String link = serieFound.attr("href");
                                    String seriename = link.replace("/serie/", "");
                                    return seriename.substring(0, seriename.indexOf("/"));
                                })
                                .filter(seriename -> URLDecoder.decode(seriename, StandardCharsets.UTF_8).replace(":", "").replace("-", "")
                                        .replace("_", " ").replace(" ", "").trim().toLowerCase().equals(formattedName))
                                .findAny().orElse(null)))
                .getOptional();
    }

    private Optional<String> getAddictedMovieName(String name) throws Addic7edException {
        return getValue("%s-MovieName-".formatted(getSubtitleSource().name(), name))
                .cacheType(CacheType.DISK)
                .optionalSupplier(
                        () -> resultStringForName(name).map(doc -> {
                            Elements aTagWithSerie = doc.select("a[debug]");
                            String link = aTagWithSerie.get(0).attr("href");
                            String moviename = link.replace("movie/", "");
                            return moviename.substring(0, moviename.indexOf("/"));
                        }))
                .getOptional();
    }

    private Optional<Document> resultStringForName(String name) throws Addic7edException {
{'fraction_non_alphanumeric': 0.12326898866974402, 'fraction_numerical': 0.008812421317666807, 'mean_word_length': 2.80103668261563, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '425828', 'n_tokens_mistral': 2750, 'n_tokens_neox': 2670, 'n_words': 485}