import requests
from bs4 import BeautifulSoup  # BeautifulSoup import

class Movie:
    def __init__(self, n, u, rank, reples):
        self.name = n
        self.url = u
        self.ranking = rank
        self.reples = reples
    def __str__(self):
        return str(self.ranking) + "위\t제목:" + self.name + "\t주소:" + self.url + "\n리플:" + str(self.reples)

class Naver:

    # 기본 정보
    naverHomeUrl = 'https://www.naver.com/'
    movieHomeUrl = 'https://movie.naver.com'
    movieRankingHomeUrl = 'https://movie.naver.com/movie/sdb/rank/rmovie.nhn'
    soupHtmlParser = 'html.parser'

    # 영화 태그패턴
    moviePattern = 'div[class=tit3]'
    replePattern = 'div[class=score_reple]'

    def __init__(self):
        naverResponse = requests.get(self.naverHomeUrl)
        naverHtml = naverResponse.text
        self.naverSoup = BeautifulSoup(naverHtml, self.soupHtmlParser)

        movieResponse = requests.get(self.movieRankingHomeUrl)
        movieHtml = movieResponse.text
        self.movieSoup = BeautifulSoup(movieHtml, self.soupHtmlParser)

    def getMovieReple(self, url):
        movieReples = []
        repleText = requests.get(self.movieHomeUrl + url).text
        repleSoup = BeautifulSoup(repleText, self.soupHtmlParser)
        for tag in repleSoup.select(self.replePattern):
            rep = tag.select('p').pop(0).text
            movieReples.append(rep)
        return movieReples

    def getTitles(self):
        titles = []
        for tag in self.movieSoup.select(self.moviePattern):
            titles.append(tag.text.strip().replace("\n", ""))
        return titles

    def getAlls(self):
        movies = []
        ranking = 1
        for tag in self.movieSoup.select(self.moviePattern):
            n = tag.text.strip().replace("\n", "")
            u = tag.find('a').get('href')
            t = n, u, ranking, self.getMovieReple(u)                #   튜플
            print(t)
            m = Movie(n, u, ranking, self.getMovieReple(u))         #   Movie 클래스
            movies.append(m)
            ranking = ranking + 1
        return movies


    def realTimeSearchWord(self):
        words = []
        ranking = 1
        realTimeWords = self.naverSoup.select('ul[class=ah_l] li[class=ah_item] a')
        for tag in realTimeWords:
            if tag.get('data-ssl'):
                relatedWord = requests.get(tag.get('href')).text
                relatedSoup = BeautifulSoup(relatedWord, self.soupHtmlParser)
                relatedWords = []
                for relatedTag in relatedSoup.select('ul[class=_related_keyword_ul] li a'):
                    relatedWords.append(relatedTag.text)
                words.append((tag.select('span[class=ah_r]')[0].text, tag.select('span[class=ah_k]')[0].text, tag.get('href'), relatedWords))
        return words


# 클래스 사용
naver = Naver()
print(naver.getTitles())
for b in naver.realTimeSearchWord():
    print(b)

for b in naver.getAlls():
    print(b)