Пример #1
0
 def __create_thumbnail(self, episode, file_path):
     time = '00:00:01.000'
     video_manager.create_episode_thumbnail(episode, file_path, time)
     thumbnail_path = '{0}/thumbnails/{1}.png'.format(str(episode.bangumi_id), episode.episode_no)
     thumbnail_file_path = '{0}/{1}'.format(self.base_path, thumbnail_path)
     color = get_dominant_color(thumbnail_file_path)
     width, height = get_dimension(thumbnail_file_path)
     episode.thumbnail_image = Image(file_path=thumbnail_path,
                                     dominant_color=color,
                                     width=width,
                                     height=height)
     episode.thumbnail_color = color
Пример #2
0
 def __create_thumbnail(self, episode, file_path):
     time = '00:00:01.000'
     video_manager.create_episode_thumbnail(episode, file_path, time)
     thumbnail_path = '{0}/thumbnails/{1}.png'.format(str(episode.bangumi_id), episode.episode_no)
     thumbnail_file_path = '{0}/{1}'.format(self.base_path, thumbnail_path)
     color = get_dominant_color(thumbnail_file_path)
     width, height = get_dimension(thumbnail_file_path)
     episode.thumbnail_image = Image(file_path=thumbnail_path,
                                     dominant_color=color,
                                     width=width,
                                     height=height)
     episode.thumbnail_color = color
Пример #3
0
    def update_bangumi(self, bangumi_id=None):
        fr = open('./config/config.yml', 'r')
        config = yaml.load(fr)
        download_dir = config['download']['location'] + '/' + str(bangumi_id)
        files = self.__list_file_recursively(download_dir)

        session = SessionManager.Session()
        eps_list = session.query(Episode).\
            filter(Episode.bangumi_id == bangumi_id).all()

        existed_video_files = session.query(VideoFile).filter(
            VideoFile.bangumi_id == bangumi_id).all()

        episodes = {}
        video_files = []
        for eps in eps_list:
            if self.__episode_has_video_file(existed_video_files, eps):
                continue
            episodes[eps.episode_no] = eps
            for f in files:
                if self.__parse_episode_number(f) == eps.episode_no:
                    eps.status = Episode.STATUS_DOWNLOADED
                    video_files.append(
                        VideoFile(bangumi_id=bangumi_id,
                                  episode_id=eps.id,
                                  file_path=f.decode('utf-8'),
                                  status=VideoFile.STATUS_DOWNLOADED))
                    break
        while True:
            for eps in episodes.values():
                if not eps:
                    continue
                episode_num = str(eps.episode_no)
                file_name = "None"
                for video_file in video_files:
                    if video_file.episode_id == eps.id:
                        file_name = video_file.file_path
                        break
                print(episode_num + ": \t" + file_name)

            print("Right? Y/N")
            x = raw_input(">>> Input: ")
            if x == "Y":
                video_manager = VideoManager()
                video_manager.set_base_path(config['download']['location'])
                for video_file in video_files:
                    for eps in episodes.values():
                        if eps.id == video_file.episode_id:
                            video_manager.create_episode_thumbnail(
                                eps, video_file.file_path, '00:00:01.000')
                            thumbnail_path = '{0}/thumbnails/{1}.png'.format(
                                str(bangumi_id), eps.episode_no)
                            thumbnail_file_path = '{0}/thumbnails/{1}.png'.format(
                                download_dir, eps.episode_no)
                            width, height = get_dimension(thumbnail_file_path)
                            eps.thumbnail_image = Image(
                                file_path=thumbnail_path,
                                dominant_color=get_dominant_color(
                                    thumbnail_file_path),
                                width=width,
                                height=height)
                            meta_dict = video_manager.get_video_meta(
                                u'{0}/{1}/{2}'.format(
                                    video_manager.base_path,
                                    bangumi_id.encode('utf-8'),
                                    video_file.file_path))
                            if meta_dict is not None:
                                video_file.resolution_w = meta_dict['width']
                                video_file.resolution_h = meta_dict['height']
                                video_file.duration = meta_dict['duration']
                                session.add(video_file)
                            break
                session.commit()
                return
            else:
                video_files = []
                for f in files:
                    print f
                    x = raw_input(">>> Episode Num")
                    if not x:
                        continue
                    x = int(x)
                    eps = episodes[x]
                    if not eps:
                        continue
                    eps.status = Episode.STATUS_DOWNLOADED
                    video_files.append(
                        VideoFile(bangumi_id=bangumi_id,
                                  episode_id=eps.id,
                                  file_path=f.decode('utf-8'),
                                  status=VideoFile.STATUS_DOWNLOADED))
Пример #4
0
    def add_bangumi(self, content, uid):
        try:
            bangumi_data = json.loads(content)

            bangumi = Bangumi(bgm_id=bangumi_data.get('bgm_id'),
                              name=bangumi_data.get('name'),
                              name_cn=bangumi_data.get('name_cn'),
                              type=bangumi_data.get('type'),
                              summary=bangumi_data.get('summary'),
                              eps=bangumi_data.get('eps'),
                              image=bangumi_data.get('image'),
                              air_date=bangumi_data.get('air_date'),
                              air_weekday=bangumi_data.get('air_weekday'),
                              status=self.__get_bangumi_status(bangumi_data.get('air_date')),
                              created_by_uid=uid,
                              maintained_by_uid=uid)


            # bangumi.dmhy = bangumi_data.get('dmhy')
            # bangumi.acg_rip = bangumi_data.get('acg_rip')
            # bangumi.libyk_so = bangumi_data.get('libyk_so')

            bangumi.eps_no_offset = bangumi_data.get('eps_no_offset')

            session = SessionManager.Session()

            session.add(bangumi)

            bangumi.episodes = []

            for eps_item in bangumi_data['episodes']:
                eps = Episode(bgm_eps_id=eps_item.get('bgm_eps_id'),
                              episode_no=eps_item.get('episode_no'),
                              name=eps_item.get('name'),
                              name_cn=eps_item.get('name_cn'),
                              duration=eps_item.get('duration'),
                              status=Episode.STATUS_NOT_DOWNLOADED)
                if is_valid_date(eps_item.get('airdate')):
                    eps.airdate = eps_item.get('airdate')

                eps.bangumi = bangumi
                bangumi.episodes.append(eps)

            session.commit()

            bangumi_id = str(bangumi.id)
            try:
                (cover_file_path, cover_path) = self.__save_bangumi_cover(bangumi)
                # get dominant color
                bangumi.cover_color = get_dominant_color(cover_file_path)
                (width, height) = get_dimension(cover_file_path)
                bangumi.cover_image = Image(file_path=cover_path,
                                            dominant_color=bangumi.cover_color,
                                            width=width,
                                            height=height)
                session.commit()
            except Exception as error:
                sentry_wrapper.sentry_middleware.captureException()
                logger.warn(error)
                # delete bangumi for download error
                session.delete(bangumi)
                session.commit()
                raise ServerError('Fail to Download Image')

            return json_resp({'data': {'id': bangumi_id}})
        finally:
            SessionManager.Session.remove()
Пример #5
0
     if not os.path.exists(bangumi_cover_path):
         # download bangumi image
         print 'start to download bangumi cover of %s (%s)' % (
             bangumi.name, str(bangumi.id))
         file_downloader.download_file(bangumi.image,
                                       bangumi_cover_path)
     if bangumi.cover_color is None:
         try:
             bangumi.cover_color = get_dominant_color(
                 bangumi_cover_path, 5)
             session.commit()
         except Exception as err:
             print err
     if bangumi.cover_image_id is None:
         try:
             width, height = get_dimension(bangumi_cover_path)
             cover_image = Image(file_path='{0}/cover{1}'.format(
                 str(bangumi.id), extname),
                                 dominant_color=bangumi.cover_color,
                                 width=width,
                                 height=height)
             bangumi.cover_image = cover_image
             session.commit()
         except Exception as err:
             print err
 except OSError as exception:
     if exception.errno == errno.EACCES:
         # permission denied
         raise exception
     else:
         print exception
Пример #6
0
    def add_bangumi(self, content, uid):
        try:
            bangumi_data = json.loads(content)

            bangumi = Bangumi(bgm_id=bangumi_data.get('bgm_id'),
                              name=bangumi_data.get('name'),
                              name_cn=bangumi_data.get('name_cn'),
                              type=bangumi_data.get('type'),
                              summary=bangumi_data.get('summary'),
                              eps=bangumi_data.get('eps'),
                              image=bangumi_data.get('image'),
                              air_date=bangumi_data.get('air_date'),
                              air_weekday=bangumi_data.get('air_weekday'),
                              status=self.__get_bangumi_status(
                                  bangumi_data.get('air_date')),
                              created_by_uid=uid,
                              maintained_by_uid=uid)

            # bangumi.dmhy = bangumi_data.get('dmhy')
            # bangumi.acg_rip = bangumi_data.get('acg_rip')
            # bangumi.libyk_so = bangumi_data.get('libyk_so')

            bangumi.eps_no_offset = bangumi_data.get('eps_no_offset')

            session = SessionManager.Session()

            session.add(bangumi)

            bangumi.episodes = []

            for eps_item in bangumi_data['episodes']:
                eps = Episode(bgm_eps_id=eps_item.get('bgm_eps_id'),
                              episode_no=eps_item.get('episode_no'),
                              name=eps_item.get('name'),
                              name_cn=eps_item.get('name_cn'),
                              duration=eps_item.get('duration'),
                              status=Episode.STATUS_NOT_DOWNLOADED)
                if eps_item.get('airdate') != '':
                    eps.airdate = eps_item.get('airdate')

                eps.bangumi = bangumi
                bangumi.episodes.append(eps)

            session.commit()

            bangumi_id = str(bangumi.id)
            try:
                (cover_file_path,
                 cover_path) = self.__save_bangumi_cover(bangumi)
                # get dominant color
                bangumi.cover_color = get_dominant_color(cover_file_path)
                (width, height) = get_dimension(cover_file_path)
                bangumi.cover_image = Image(file_path=cover_path,
                                            dominant_color=bangumi.cover_color,
                                            width=width,
                                            height=height)
                session.commit()
            except Exception as error:
                sentry_wrapper.sentry_middleware.captureException()
                logger.warn(error)
                # delete bangumi for download error
                session.delete(bangumi)
                session.commit()
                raise ServerError('Fail to Download Image')

            return json_resp({'data': {'id': bangumi_id}})
        finally:
            SessionManager.Session.remove()
Пример #7
0
    def update_bangumi(self, bangumi_id=None):
        fr = open('./config/config.yml', 'r')
        config = yaml.load(fr)
        download_dir = config['download']['location'] + '/' + str(bangumi_id)
        files = self.__list_file_recursively(download_dir)

        session = SessionManager.Session()
        try:
            eps_list = session.query(Episode).\
                filter(Episode.bangumi_id == bangumi_id).all()
            bangumi = session.query(Bangumi).\
                filter(Bangumi.id == bangumi_id).one()
            eps_no_offset = 0
            if bangumi.eps_no_offset is not None:
                eps_no_offset = bangumi.eps_no_offset

            existed_video_files = session.query(VideoFile).filter(
                VideoFile.bangumi_id == bangumi_id).all()

            episodes = {}
            video_files = []
            for eps in eps_list:
                # if self.__episode_has_video_file(existed_video_files, eps):
                #     continue
                episodes[eps.episode_no] = eps
                for f in files:
                    if self.__video_path_already_added(existed_video_files,
                                                       f.decode('utf-8')):
                        continue
                    if self.__parse_episode_number(
                            f) + eps_no_offset == eps.episode_no:
                        eps.status = Episode.STATUS_DOWNLOADED
                        video_files.append(
                            VideoFile(bangumi_id=bangumi_id,
                                      episode_id=eps.id,
                                      file_path=f.decode('utf-8'),
                                      status=VideoFile.STATUS_DOWNLOADED))
                        break
            while True:
                for eps in episodes.values():
                    if not eps:
                        continue
                    episode_num = str(eps.episode_no)
                    line = episode_num + ": \t"
                    file_name = None
                    for video_file in video_files:
                        if video_file.episode_id == eps.id:
                            if file_name is None:
                                line = line + video_file.file_path
                            else:
                                line = line + "\n  \t" + video_file.file_path
                            if video_file.label is not None:
                                line = line + "\t" + video_file.label.decode(
                                    'utf-8')
                            file_name = video_file.file_path
                    if file_name is None:
                        line = line + "None"
                    print(line)

                print("Right? Y/N")
                x = raw_input(">>> Input: ")
                if x == "Y":
                    video_manager = VideoManager()
                    video_manager.set_base_path(config['download']['location'])
                    for video_file in video_files:
                        for eps in episodes.values():
                            if eps.id == video_file.episode_id:
                                video_manager.create_episode_thumbnail(
                                    eps, video_file.file_path, '00:00:01.000')
                                thumbnail_path = '{0}/thumbnails/{1}.png'.format(
                                    str(bangumi_id), eps.episode_no)
                                thumbnail_file_path = '{0}/thumbnails/{1}.png'.format(
                                    download_dir, eps.episode_no)
                                width, height = get_dimension(
                                    thumbnail_file_path)
                                eps.thumbnail_image = Image(
                                    file_path=thumbnail_path,
                                    dominant_color=get_dominant_color(
                                        thumbnail_file_path),
                                    width=width,
                                    height=height)
                                meta_dict = video_manager.get_video_meta(
                                    u'{0}/{1}/{2}'.format(
                                        video_manager.base_path,
                                        bangumi_id.encode('utf-8'),
                                        video_file.file_path))
                                if meta_dict is not None:
                                    video_file.resolution_w = meta_dict[
                                        'width']
                                    video_file.resolution_h = meta_dict[
                                        'height']
                                    video_file.duration = meta_dict['duration']
                                    session.add(video_file)
                                # break
                    session.commit()
                    return
                else:
                    video_files = []
                    for f in files:
                        print f
                        x = raw_input(
                            ">>> Episode Num and Label (separated by comma)")
                        if not x:
                            continue
                        arguments = x.split(',')
                        x = int(arguments[0])
                        label = None
                        if len(arguments) > 1:
                            label = arguments[1]
                        eps = episodes[x]
                        if not eps:
                            continue
                        eps.status = Episode.STATUS_DOWNLOADED
                        video_files.append(
                            VideoFile(bangumi_id=bangumi_id,
                                      episode_id=eps.id,
                                      file_path=f.decode('utf-8'),
                                      label=label,
                                      status=VideoFile.STATUS_DOWNLOADED))
        finally:
            SessionManager.Session.remove()
Пример #8
0
    def update_bangumi(self, bangumi_id=None):
        fr = open('./config/config.yml', 'r')
        config = yaml.load(fr)
        download_dir = config['download']['location'] + '/' + str(bangumi_id)
        files = self.__list_file_recursively(download_dir)

        session = SessionManager.Session()
        try:
            eps_list = session.query(Episode).\
                filter(Episode.bangumi_id == bangumi_id).all()
            bangumi = session.query(Bangumi).\
                filter(Bangumi.id == bangumi_id).one()
            eps_no_offset = 0
            if bangumi.eps_no_offset is not None:
                eps_no_offset = bangumi.eps_no_offset

            existed_video_files = session.query(VideoFile).filter(VideoFile.bangumi_id == bangumi_id).all()

            episodes = {}
            video_files = []
            for eps in eps_list:
                # if self.__episode_has_video_file(existed_video_files, eps):
                #     continue
                episodes[eps.episode_no] = eps
                for f in files:
                    if self.__video_path_already_added(existed_video_files, f.decode('utf-8')):
                        continue
                    if self.__parse_episode_number(f) + eps_no_offset == eps.episode_no:
                        eps.status = Episode.STATUS_DOWNLOADED
                        video_files.append(VideoFile(bangumi_id=bangumi_id,
                                                     episode_id=eps.id,
                                                     file_path=f.decode('utf-8'),
                                                     status=VideoFile.STATUS_DOWNLOADED))
                        break
            while True:
                for eps in episodes.values():
                    if not eps:
                        continue
                    episode_num = str(eps.episode_no)
                    line = episode_num + ": \t"
                    file_name = None
                    for video_file in video_files:
                        if video_file.episode_id == eps.id:
                            if file_name is None:
                                line = line + video_file.file_path
                            else:
                                line = line + "\n  \t" + video_file.file_path
                            if video_file.label is not None:
                                line = line + "\t" + video_file.label.decode('utf-8')
                            file_name = video_file.file_path
                    if file_name is None:
                        line = line + "None"
                    print (line)

                print("Right? Y/N")
                x = raw_input(">>> Input: ")
                if x == "Y":
                    video_manager = VideoManager()
                    video_manager.set_base_path(config['download']['location'])
                    for video_file in video_files:
                        for eps in episodes.values():
                            if eps.id == video_file.episode_id:
                                video_manager.create_episode_thumbnail(eps, video_file.file_path, '00:00:01.000')
                                thumbnail_path = '{0}/thumbnails/{1}.png'.format(str(bangumi_id), eps.episode_no)
                                thumbnail_file_path = '{0}/thumbnails/{1}.png'.format(download_dir, eps.episode_no)
                                width, height = get_dimension(thumbnail_file_path)
                                eps.thumbnail_image = Image(file_path=thumbnail_path,
                                                            dominant_color=get_dominant_color(thumbnail_file_path),
                                                            width=width,
                                                            height=height)
                                meta_dict = video_manager.get_video_meta(u'{0}/{1}/{2}'.format(video_manager.base_path, bangumi_id.encode('utf-8'), video_file.file_path))
                                if meta_dict is not None:
                                    video_file.resolution_w = meta_dict['width']
                                    video_file.resolution_h = meta_dict['height']
                                    video_file.duration = meta_dict['duration']
                                    session.add(video_file)
                                # break
                    session.commit()
                    return
                else:
                    video_files = []
                    for f in files:
                        print f
                        x = raw_input(">>> Episode Num and Label (separated by comma)")
                        if not x:
                            continue
                        arguments = x.split(',')
                        x = int(arguments[0])
                        label = None
                        if len(arguments) > 1:
                            label = arguments[1]
                        eps = episodes[x]
                        if not eps:
                            continue
                        eps.status = Episode.STATUS_DOWNLOADED
                        video_files.append(VideoFile(bangumi_id=bangumi_id,
                                                     episode_id=eps.id,
                                                     file_path=f.decode('utf-8'),
                                                     label=label,
                                                     status=VideoFile.STATUS_DOWNLOADED))
        finally:
            SessionManager.Session.remove()