<commit_before># -*- coding: utf-8 -*-

# Copyright(C) 2010  <NAME>, <NAME>, <NAME>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


from weboob.capabilities.video import ICapVideo
from weboob.tools.application.repl import ReplApplication
from weboob.tools.application.video_player import VideoPlayer


__all__ = ['Videoob']


class Videoob(ReplApplication):
    APPNAME = 'videoob'
    VERSION = '0.3'
    COPYRIGHT = 'Copyright(C) 2010 <NAME>, <NAME>, <NAME>'
    CAPS = ICapVideo

    nsfw = True
    videos = []

    def __init__(self, *args, **kwargs):
        ReplApplication.__init__(self, *args, **kwargs)
        try:
            self.player = VideoPlayer()
        except OSError:
            self.player = None

    def _get_video(self, _id):
        if self.interactive:
            try:
                video = self.videos[int(_id)]
            except (KeyError,ValueError):
                pass
            else:
                return video
        _id, backend_name = self.parse_id(_id)
        backend_names = (backend_name,) if backend_name is not None else self.enabled_backends
        for backend, video in self.do('get_video', _id, backends=backend_names):
            if video:
                return video

    def _complete_id(self):
        return ['%s@%s' % (video.id, video.backend) for video in self.videos]

    def complete_play(self, text, line, *ignored):
        args = line.split(' ')
        if len(args) == 2:
            return self._complete_id()

    def do_play(self, _id):
        """
        play ID

        Play a video with a found player.
        """
        if not _id:
            print 'This command takes an argument: %s' % self.get_command_help('info', short=True)
            return

        video = self._get_video(_id)
        if not video:
            print 'Video not found: ', _id
            return

        backend = self.weboob.get_backend(video.backend)
        backend.fillobj(video, ['url'])

        if self.player:
            self.player.play(video)
        else:
            print 'No player has been found on this system.'
            print 'The URL of this video is:'
            print '  %s' % video.url

    def complete_info(self, text, line, *ignored):
        args = line.split(' ')
        if len(args) == 2:
            return self._complete_id()

    def do_info(self, _id):
        """
        info ID

        Get information about a video.
        """
        if not _id:
            print 'This command takes an argument: %s' % self.get_command_help('info', short=True)
            return

        video = self._get_video(_id)<commit_msg>use a special and readable formatter for the 'search' command<commit_after># -*- coding: utf-8 -*-

# Copyright(C) 2010  <NAME>, <NAME>, <NAME>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


from weboob.capabilities.video import ICapVideo
from weboob.capabilities.base import NotLoaded
from weboob.tools.application.repl import ReplApplication
from weboob.tools.application.video_player import VideoPlayer
from weboob.tools.application.formatters.iformatter import IFormatter


__all__ = ['Videoob']


class VideoListFormatter(IFormatter):
    count = 0

    def after_format(self, formatted):
        print formatted.encode('utf-8')

    def flush(self):
        self.count = 0
        pass

    def format_dict(self, item):
        self.count += 1
        backend = item['id'].split('@', 1)[1]
        result = u'%s(%d) %s (%s)%s\n' % (ReplApplication.BOLD, self.count, item['title'], backend, ReplApplication.NC)
        result += '            %s' % item['duration']
        if item['author'] is not NotLoaded:
            result += ' - %s' % item['author']
        if item['rating'] is not NotLoaded:
            result += u' (%s/%s)' % (item['rating'], item['rating_max'])
        return result

    def set_header(self, string):
        if self.display_header:
            print string.encode('utf-8')

class Videoob(ReplApplication):
    APPNAME = 'videoob'
    VERSION = '0.3'
    COPYRIGHT = 'Copyright(C) 2010 <NAME>, <NAME>, <NAME>'
    CAPS = ICapVideo
    EXTRA_FORMATTERS = {'video_list': VideoListFormatter}
    COMMANDS_FORMATTERS = {'search':    'video_list'}

    nsfw = True
    videos = []

    def __init__(self, *args, **kwargs):
        ReplApplication.__init__(self, *args, **kwargs)
        try:
            self.player = VideoPlayer()
        except OSError:
            self.player = None

    def _get_video(self, _id, fields=None):
        if self.interactive:
            try:
                video = self.videos[int(_id) - 1]
            except (IndexError,ValueError):
                pass
            else:
                for backend, video in self.do('fillobj', video, fields, backends=[video.backend]):
                    if video:
                        return video
        _id, backend_name = self.parse_id(_id)
        backend_names = (backend_name,) if backend_name is not None else self.enabled_backends
        for backend, video in self.do('get_video', _id, backends=backend_names):
            if video:
                return video

    def _complete_id(self):
        return ['%s@%s' % (video.id, video.backend) for video in self.videos]

    def complete_play(self, text, line, *ignored):
        args = line.split(' ')
        if len(args) == 2:
            return self._complete_id()

    def do_play(self, _id):
        """
        play ID

        Play a video with a found player.
        """
        if not _id:
            print 'This command takes an argument: %s' % self.get_command_help('info', short=True)
            return

        video = self._get_video(_id, ['url'])
        if not video:
            print 'Video not found: ', _id
            return

        backend = self.weboob.get_backend(video.backend)
        backend.fillobj(video, ['url'])

        if self.player:
            self.player.play(video)
        else:
            print 'No player has been found on this system.'
            print 'The URL of this video is:'
            print '  %s' % video.url

    def complete_info(self, text, line, *ignored):
        args = line.split(' ')
        if len(args) == 2:
            return self._complete_id()

    def do_info(self, _id):
        """
        info ID

        Get information about a video.
        """
        if not _id:
            print 'This command takes an argument: %s' % self.get_command_help('info', short=True)
            return

        video = self._get_video(_id)
{'fraction_non_alphanumeric': 0.1035691109669046, 'fraction_numerical': 0.008306294613887086, 'mean_word_length': 2.259729272419628, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4654739', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2168, 'n_words': 730}