프로젝트 : 내 애니메이션 찾기

Aug 16 2020

특정 애니메이션 (쇼)을 찾아 새로운 에피소드가 있는지 확인하는 프로젝트입니다.

이것은 또한 내가 def이것을 처음 사용 하고이 프로젝트가 어떻게 보이고 어떻게 향상시킬 수 있는지 묻고 싶었습니다.

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

path = "c:/users/admin/appdata/local/programs/python/python38-32/chromedriver.exe"

driver = webdriver.Chrome(path)

chose = int(input("Press 1 for Black Clover \nPress 2 for Fire Force : "))

class BlackClover:
    def __init__(self, title):
        # lets the user chose which anime(show) he or she wants to watch.
        self.title = title  

        # goes to the website that i want .
        self.url = driver.get("https://www.gogoanime.movie/")

        # i let it sleep for a few seconds before it goes to the next task
        time.sleep(3)

        # goes the english speaking anime (shows)
        self.dub = driver.find_element_by_xpath('//*[@id="load_recent_release"]/div[1]/h2/a[2]')
        self.dub.send_keys(Keys.RETURN)

        # will be used later to filter out the episode number .
        self.numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
        self.divider = []

        # the anime name (shows name).
        self.clover = "Black Clover (TV) (Dub)"

    def checker(self):
        # will be used to retrieve the episode of the anime (show).
        global episode

        # will be used to go through the li element (HTML)
        global n
        n = 1
        total = 0

        # checks to see if the first li element is equal to "Black Clover (TV) (Dub)".
        first_anime = WebDriverWait(driver, 3).until(ec.visibility_of_element_located(
            (By.XPATH, '//*[@id="load_recent_release"]/div[2]/ul/li[1]/p[1]/a'))).get_attribute("innerHTML")

        if first_anime == "Black Clover (TV) (Dub)":
            print("first good")
            driver.find_element_by_xpath('//*[@id="load_recent_release"]/div[2]/ul/li[1]/p[1]/a').send_keys(Keys.RETURN)

        # if it is not equal to "Black Clover (TV) (Dub)" it will loop through the next li element until it finds it.
        else:
            while True:

                # the next li element
                name = WebDriverWait(driver,3).until(ec.visibility_of_element_located(
                    (By.XPATH, f'//*[@id="load_recent_release"]/div[2]/ul/li[{n}]/p[1]/a'))).get_attribute("innerHTML")

                if name == self.clover:
                    print("good")

                    # checks which episode it's on and looks if it's a new episode
                    episode = WebDriverWait(driver,2).until(ec.visibility_of_element_located(
                        (By.XPATH, f'//*[@id="load_recent_release"]/div[2]/ul/li[{n}]/p[2]'))).get_attribute("innerHTML")

                    # retrieves the episode number
                    self.episodes()
                    break

                if name != self.clover:
                    print("bad")
                    total += 1
                    n+= 1

                    if total == 20:
                        print("nothing")
                        break

편집 : 사용자가 chose상단에 호출되는 입력 변수에 1 또는 2를 입력하여 확인하려는 애니메이션 (쇼)을 선택하도록했습니다 .

@fabrizzio_gz 덕분에.

    def episodes(self):
        global joiner

        # divider is a empty list
        divider = self.divider

        # copies the the variable "episode"
        k= list(episode)
        splitter = list(episode)

        # gets the last three numbers in "k".
        number = len(k)-3
        last_number = k[number:]
        last_number_2 = k[-1]

        # checks to see if there are any numbers in the the variable "splitter" .
        for i in splitter:

            # if the i in splitter isn't a number it replaces it with a empty string.
            if i not in self.numbers:
                t = splitter.index(i)
                splitter[t] = ""
                print(splitter)

            # if i is a number then it will add to the list with the variablename "divider" .
            if i in self.numbers:
                divider.append(i)

                if divider == last_number and self.title == "Black Clover (TV) (Dub)":

                    # this joins all the numbers together in the list
                    joiner = ''.join(divider)

                    # checks to see if the episode is greater then 129
                    if int(joiner) > 129:
                        time.sleep(2)

                        # when the episode is greater then 129 it clicks the anime (show).
                        click = driver.find_element_by_xpath(f'//*[@id="load_recent_release"]/div[2]/ul/li[{n}]/p[1]/a')
                        click.send_keys(Keys.RETURN)
                        break
                    else :
                        # if the episode isn't greater then 129 then it returns the following .
                        return print("no new episode of : BLACK CLOVER ")

                else:
                    joiner = ''.join(divider)
                    if int(joiner) >= 4:
                        time.sleep(2)

                            # when the episode is greater then 4 it clicks the anime (show).
                        click = driver.find_element_by_xpath(
                        f'//*[@id="load_recent_release"]/div[2]/ul/li[{n}]/p[1]/a')
                        click.send_keys(Keys.RETURN)
                        break
                    else:
                        # if the episode isn't greater then 4 then it returns the following .
                        return print("no new episode of : FIRE FORCE ")


if chose == 1:
    main = BlackClover(title="Black Clover (TV) (Dub)")
    main.checker()

if chose == 2:
    main = BlackClover(title="Enen no Shouboutai: Ni no Shou (Dub)")
    main.checker()

답변

2 fabrizzio_gz Aug 16 2020 at 04:05

보기 좋고 흥미로운 프로젝트입니다. 나는 셀레늄에 익숙하지 않지만 대부분의 코드를 공유하는 각 애니메이션 쇼에 대한 클래스를 만들었으므로 부모 애니메이션 쇼 클래스를 구현하고 각 하위 클래스에 메서드를 상속 할 수 있습니다. 또는 애니메이션 쇼 제목이나 필요한 속성 만 변경하여 각 애니메이션 쇼를 동일한 클래스의 인스턴스로 만들 수도 있습니다.

편집 : 다음과 같이 두 애니메이션 쇼에 동일한 클래스를 사용할 수 있습니다. 두 클래스 대신 AnimeShow동일한 속성 및 메서드 를 사용하여 새 클래스를 만듭니다 . 다른 제목으로 애니메이션 쇼를 초기화 할 수 있도록 함수에 매개 변수 anime_show를 추가합니다 __init__. 또한 필요한 경우 새 속성을 사용하도록 코드를 변경해야 self.title합니다.

class AnimeShow():
    
    # Adding anime_show parameter
    def __init__(self, anime_show):
        # Same attributes as you had before
        # ...
        self.title = anime_show

    # Rest of class methods
    # ...

if __name__ == '__main__':
    # Creating different anime shows with the same class
    clover = AnimeShow("Black Clover (TV) (Dub)")
    fire = AnimeShow("Enen no Shouboutai: Ni no Shou (Dub)")
    clover.checker()
    fire.checker()
```