# -*- coding:utf-8 -*-
import codecs
import json
import re
from DataOutput import DataOutput
from HtmlDownloader import HtmlDownloader

class HtmlParser():

    def parser_url(self, page_url, response):
        pattern = re.compile(r'(http://movie.mtime.com/(\d+)/)')
        urls = pattern.findall(response)
        if urls != None:
            #url去重
            return list(set(urls))
        else:
            return None

    def paser_json(self, page_url, response):
        '''
        解析Ajax返回的响应
        '''
        pattern = re.compile(r'=(.*?);')
        result = pattern.findall(response)[0]
        #获取Ajax返回响应的json内容
        if result != None:
            value = json.loads(result)
            try:
                isRelease = value.get('value').get('isRelease')
            except Exception as e:
                print(e)
                return None
            if isRelease:
                if value.get('value').get('hotvalue') == None:
                    return self._parser_release(page_url, value)
                else:
                    return self._parser_no_release(page_url, value, isRelease=2)
            else:
                return self._parser_no_release(page_url, value)

    def _parser_release(self, page_url, value):
        '''
        解析已上映影片

        格式如下：
        var result_201611132231493282 = { "value":{"isRelease":true,"movieRating":{"MovieId":108737,"RatingFinal"
        :7.7,"RDirectorFinal":7.7,"ROtherFinal":7,"RPictureFinal":8.4,"RShowFinal":10,"RStoryFinal":7.3,"RTotalFinal"
        :10,"Usercount":4067,"AttitudeCount":4300,"UserId":0,"EnterTime":0,"JustTotal":0,"RatingCount":0,"TitleCn"
        :"","TitleEn":"","Year":"","IP":0},"movieTitle":"奇异博士","tweetId":0,"userLastComment":"","userLastCommentUrl"
        :"","releaseType":1,"boxOffice":{"Rank":1,"TotalBoxOffice":"5.66","TotalBoxOfficeUnit":"亿","TodayBoxOffice"
        :"4776.8","TodayBoxOfficeUnit":"万","ShowDays":10,"EndDate":"2016-11-13 22:00","FirstDayBoxOffice":"8146
        .21","FirstDayBoxOfficeUnit":"万"}},"error":null};var movieOverviewRatingResult=result_201611132231493282

        '''
        try:
            isRelease = 1
            movieRating = value.get('value').get('movieRating')
            boxOffice = value.get('value').get('boxOffice')
            movieTitle = value.get('value').get('movieTitle')

            RPictureFinal = movieRating.get('RPictureFinal')
            RStoryFinal = movieRating.get('RStoryFinal')
            RDirectorFinal = movieRating.get('RDirectorFinal')
            ROtherFinal = movieRating.get('ROtherFinal')
            RatingFinal = movieRating.get('RatingFinal')

            MovieId =  movieRating.get('MovieId')
            Usercount = movieRating.get('Usercount')
            AttitudeCount =  movieRating.get('AttitudeCount')

            TotalBoxOffice =  boxOffice.get('TotalBoxOffice')
            TotalBoxOfficeUnit =  boxOffice.get('TotalBoxOfficeUnit')
            TodayBoxOffice =  boxOffice.get('TodayBoxOffice')
            TodayBoxOfficeUnit =  boxOffice.get('TodayBoxOfficeUnit')

            ShowDays = boxOffice.get('ShowDays')
            try:
                Rank = boxOffice.get('Rank')
            except:
                Rank=0
            #将提取其中的内容进行返回
            return (MovieId,movieTitle,RatingFinal,
                    ROtherFinal,RPictureFinal,RDirectorFinal,
                    RStoryFinal,Usercount,AttitudeCount,
                    TotalBoxOffice+TotalBoxOfficeUnit,
                    TodayBoxOffice+TodayBoxOfficeUnit,
                    Rank,ShowDays,isRelease )
        except Exception as e:
            print(e,page_url,value)
            return None

    def _parser_no_release(self,page_url,value,isRelease = 0):
        try:
            movieRating = value.get('value').get('movieRating')
            movieTitle = value.get('value').get('movieTitle')

            RPictureFinal = movieRating.get('RPictureFinal')
            RStoryFinal = movieRating.get('RStoryFinal')
            RDirectorFinal = movieRating.get('RDirectorFinal')
            ROtherFinal = movieRating.get('ROtherFinal')
            RatingFinal = movieRating.get('RatingFinal')

            MovieId =  movieRating.get('MovieId')
            Usercount = movieRating.get('Usercount')
            AttitudeCount =  movieRating.get('AttitudeCount')
            try:
                Rank = value.get('value').get('hotValue').get('Ranking')
            except:
                Rank = 0
            return (MovieId,movieTitle,RatingFinal,
                    ROtherFinal,RPictureFinal,RDirectorFinal,
                    RStoryFinal, Usercount,AttitudeCount,u'无',
                    u'无',Rank,0,isRelease )
        except Exception as e:
            print(e,page_url,value)
            return None


