Ejemplo n.º 1
0
 def setUpClass(cls):
     # update query to file objects with different field states
     query = COLLECTION.find_one()
     query['_id'] = 'testing'
     COLLECTION.insert_one(query)
     copy = COLLECTION.find_one({'_id': 'testing'})
     cls.videoObj = VideoSearch.query_result_to_video(copy)
Ejemplo n.º 2
0
    def write_nfo(self) -> None:
        """ Create an nfo file and write to path"""

        i = COLLECTION.find_one({'_id': self.video_id}, {
            '_id': True,
            'uploader': True,
            'title': True,
            'description': True,
            'upload_date': True
        })

        try:
            with open(os.path.join(APP_PATH, 'template.nfo'), 'r') as fl:
                template = Template(fl.read())
        except FileNotFoundError as e:
            logger.error(e)

        out_template = template.substitute(unique_id=i['_id'],
                                           studio=i['uploader'],
                                           title=i['title'],
                                           plot=i['description'],
                                           date_prem=i['upload_date'])
        path = os.path.join(self.get_path()['path'], 'tvshow.nfo')
        logger.info('writing nfo to path %s', path)
        with open(path, 'w') as fl:
            fl.write(out_template)
Ejemplo n.º 3
0
 def get_thumbnail(self) -> None:
     # TODO fix path to use same as video_downloader
     """Download thumbnail to path
     """
     url = COLLECTION.find_one({'_id': self.video_id},
                               {'thumbnail': True})['thumbnail']
     data = requests.get(url, stream=True)
     image_data = Image.open(io.BytesIO(data.content))
     path = self.get_path()['path']
     logger.info('writing thumbnail to path %s', path)
     image_data.save(os.path.join(path, 'thumbnail.jpg'), 'jpeg')
Ejemplo n.º 4
0
    def delete_video(self, check: bool = False) -> Union[Tuple, str]:
        """Remove the current object from the database
        along with any files or folders

        Args:
            check (bool): a value to be explicitly set
            in order for the video to be deleted

        Returns:
           tuple: (int: 0, str: '') - the document was not found
           tuple: (int: 1, str: '') - the document was found and deleted
            but had not path key
           tuple: (int: 2, str: '') - the document and path were found
            both were deleted
           str: check did not pass
        """

        if self._mode == 'test':
            return 'Data cannot be deleted in test mode'

        if not check:
            return ''

        result = COLLECTION.find_one({'_id': self.video_id}, {
            '_id': True,
            'path': True
        })

        if not result:
            res = (0, '')
        elif 'path' not in result.keys():
            COLLECTION.delete_one({'_id': self.video_id})
            res = (1, '')
            logger.info('video_id %s deleted - no folder found', self.video_id)
        else:
            COLLECTION.delete_one({'_id': self.video_id})
            path: str = result['path']
            try:
                shutil.rmtree(os.path.split(path)[0], ignore_errors=True)
            except FileNotFoundError as e:
                logger.error(e)
            res = (2, path)
            logger.info('video_id %s deleted - folder %s deleted',
                        self.video_id, path)

        return res
Ejemplo n.º 5
0
    def get_next_unprocessed(mode: str = '') -> Union[None, Video]:
        """Find the next unprocessed video

        Args:
            mode (str): mode flag to pass to Video

        Returns:
            Video: A Video instance
            None: no value found
        """
        res = COLLECTION.find_one({'Processed': False})

        if res:
            ret = VideoSearch.query_result_to_video(res, mode)
        else:
            ret = None

        return ret
Ejemplo n.º 6
0
    def exact_find_video(**kwarg: str) -> Union[Video, None]:
        """ match a value exactly

        Args:
            kwarg (dict): kv corresponding to value in db

        Return:
            Video: an instance of Video
            None: no value found

        """

        search = COLLECTION.find_one(kwarg)
        if search:
            ret = VideoSearch.query_result_to_video(search)
        else:
            ret = None

        return ret
Ejemplo n.º 7
0
    def get_path(self) -> dict:
        """ Build a path from the VIDEO_DIR, tags and title

        Returns:
            dict: {path: True || False} if path does or doesn't exist

        Raises:
            TypeError: supplied tags not type str or list
        """

        tags: list = COLLECTION.find_one({'_id': self.video_id},
                                         {'tags': True})['tags']

        sub_dir = '/'.join(tags)

        path: str = os.path.join(VIDEO_DIR, sub_dir, self.video_folder_name())
        if os.path.exists(path):
            result = {'path': path, 'exists': True}
        else:
            result = {'path': path, 'exists': False}

        return result