Ejemplo n.º 1
0
def load_subs(file_path):
	'''
		- Factory.
	'''
	subtitles = Subtitles();
	subtitles.get_subtitles(file_path)
	subtitles.get_words()
	return subtitles
Ejemplo n.º 2
0
def main():
    source = 'test/Community.S01E01.1080p.BluRay.x264-YELLOWBiRD.mkv'
    subs = 'test/1952727133.srt'
    tmp_dir = 'test/data/'
    # reddit.get_quote_candidates('dzsk34')
    # subtitles.download_subtitles('1467481')

    subtitles = Subtitles(tmp_dir)
    synced_subs = subtitles.sync_subtitles(source, subs)
    extracted_subs = subtitles.search_subtitles(synced_subs, "nicest")

    gif_extractor = GifExtractor(tmp_dir)
    gif_extractor.extract_gif(source, extracted_subs)
Ejemplo n.º 3
0
    def _process(self, url):
        log.info(f'Processing {url}')
        episode = Episode.cached(url)
        if not episode:
            return []
        tmp_audio_path = utils.convert(file_path=episode.audio_path,
                                       extension='.flac')

        subtitles = Subtitles.from_srt(episode.captions_path)

        results = []
        start = None
        accum_text = []
        for line, next_line in pairwise(subtitles.lines):
            # log.info(f'Processing {line}')
            if start is None:
                start = line.start
            accum_text.append(line.text)
            if line.end - start > 10 * 1000:
                self._save_part(start, line.end, tmp_audio_path,
                                ' '.join(accum_text), results)
                accum_text = []
                start = next_line.start

        log.info(f'Processed {url}')
        return results
Ejemplo n.º 4
0
    def openSubs(self):
        """New window allowing user to select a subtitle."""
        file = askopenfilename(initialdir=self.initialDirectory)

        print(file)
        print(isinstance(file, tuple))
        print(os.path.isfile(file))
        if isinstance(file, tuple):
            return
        if os.path.isfile(file):
            self.subs = Subtitles(file)
Ejemplo n.º 5
0
class Video(KivyVideo):
    image = ObjectProperty(None)
    surl = StringProperty(None)

    def on_state(self, instance, value):
        if self.state == 'play':
            self.cover.opacity = 0
            self.color = (1, 1, 1, 1)

        elif self.state == 'stop':
            self.seek(0)
            self.color = (0, 0, 0, 0)
            self.cover.opacity = 1
        return super().on_state(instance, value)

    def on_eos(self, instance, value):
        if value:
            self.state = 'stop'

    def _on_load(self, *largs):
        super()._on_load(largs)
        self.color = (1, 1, 1, 1)

    def on_source(self, instance, value):
        self.color = (0, 0, 0, 0)
        self.subs = Subtitles(self.surl)
        self.sub = None

    def on_position(self, instance, value):
        #print(self.subs.subtitles)
        next_sub = self.subs.next(value)
        if next_sub is None:
            self.clear_subtitle()
        else:
            sub = self.sub
            st = 'startTime'
            if sub is None or sub[st] != next_sub[st]:
                self.display_subtitle(next_sub)

    def clear_subtitle(self):
        if self.slabel.text != "":
            self.sub = None
            self.slabel.text = ""
            self.slabel.bcolor = (0.1, 0.1, 0.1, 0)

    def display_subtitle(self, sub):
        self.sub = sub
        self.slabel.text = sub['content']
        print(self.slabel.text)
        self.slabel.bcolor = (0.1, 0.1, 0.1, .8)

    def on_image(self, instance, value):
        self.cover.opacity = 1
Ejemplo n.º 6
0
 def __init__(self):
     super(Model, self).__init__()
     Gst.init(None)
     self.video = Video()
     self.audio = None
     self.subtitles = Subtitles()
     self.voReference = cVOReference()
     self.scenes = []
     self.ready = False
     # File Names
     self.voFilename = ""
     self.subFilename = ""
     self.peakFilename = ""
     self.projectFilename = ""
     self.audioDuration = 0
Ejemplo n.º 7
0
    def play(self):
        # Create a VideoCapture object and read from input file
        # If the input is the camera, pass 0 instead of the video file name
        cap = cv2.VideoCapture(self.video_file)

        # Check if camera opened successfully
        if cap.isOpened() is False:
            print("Error opening video stream or file")
        index = 0

        # find out the video dimensions
        self.height, self.width, _ = cap.read()[1].shape

        if not self.multi:
            shape = Shape(
                self.width,
                self.height,
                self.paint_x,
                self.paint_y,
                self.speed,
                self.image_paths[index % len(self.image_paths)],
                self.animation,
            )
        else:
            shapes = [
                Shape(
                    self.width,
                    self.height,
                    randint(0, self.width),
                    randint(0, self.height),
                    # int(self.height / 4 * (i + 1)),
                    # int(self.width / (4 * (i + 1))),
                    self.speed,
                    image,
                    "fall",  # must be with fall!!! it's 02:03am
                ) for i, image in enumerate(self.image_paths[:4])
            ]

        effect = Effect(self.color_effect)
        text = Subtitles(self.text,
                         self.text_speed,
                         acceleration=3,
                         font=self.font)
        # text = Subtitles('FAST\nFURIOUS\nFANCY', self.text_speed,
        #                  './data/fonts/Dogfish/Dogfish.ttf')

        title = Subtitles(self.title, self.text_speed, font=self.font)
        title.font_scale = 3
        title.thick = 3
        # Read until video is completed

        # Default resolutions of the frame are obtained.The default resolutions
        # are system dependent. We convert the resolutions from float to
        # integer.
        # frame_width = int(cap.get(3))
        # frame_height = int(cap.get(4))

        # print("width ", frame_width)
        # print("height ", frame_height)
        out = cv2.VideoWriter(self.output, cv2.VideoWriter_fourcc(*'MP4V'), 17,
                              (self.width, self.height))

        while cap.isOpened():
            # Capture frame-by-frame
            ret, frame = cap.read()
            try:
                self.width = frame.shape[1]
                self.height = frame.shape[0]
            except AttributeError:
                break

            if ret is True:
                frame = effect.apply(frame)
                if self.multi:
                    for shape in shapes:
                        shape.paint(frame)
                else:
                    shape.paint(frame)
                    pass

                frame = title.show_title(frame, self.width, self.height)
                # frame = text.show_low(frame, self.width, self.height)
                frame = text.show_price(frame, self.width, self.height)
                # if title.counter == 1:
                #     text.counter = title.counter
                if self.render:
                    cv2.imshow("Frame", frame)
                out.write(frame)
                if not self.multi and shape.end:
                    index += 1
                    if index >= len(self.image_paths):
                        index = 0

                    shape = Shape(
                        self.width,
                        self.height,
                        self.paint_x,
                        self.paint_y,
                        self.speed,
                        self.image_paths[index % len(self.image_paths)],
                        self.animation,
                    )

                # Press Q on keyboard to  exit
                if cv2.waitKey(25) & 0xFF == ord("q"):
                    return
            # Break the loop
            else:
                break

        # When everything done, release the video capture object
        cap.release()

        # Closes all the frames
        cv2.destroyAllWindows()
Ejemplo n.º 8
0
    def _method_get_video_info(self, video_id=None, player_config=None):
        headers = {
            'Host': 'www.youtube.com',
            'Connection': 'keep-alive',
            'User-Agent':
            'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
            'Accept': '*/*',
            'DNT': '1',
            'Referer': 'https://www.youtube.com/tv',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'en-US,en;q=0.8,de;q=0.6'
        }

        if self._access_token:
            headers['Authorization'] = 'Bearer %s' % self._access_token

        params = {
            'hl': self.language,
            'gl': self.region,
            'ssl_stream': '1',
            'el': 'default',
            'html5': '1'
        }

        if player_config is None:
            html = self.get_watch_page(video_id)
            player_config = self.get_player_config(html)

        player_assets = player_config.get('assets', {})
        player_args = player_config.get('args', {})
        player_response = player_args.get('player_response', {})
        playability_status = player_response.get('playabilityStatus', {})
        captions = player_response.get('captions', {})
        js = player_assets.get('js')

        if video_id is None:
            if 'video_id' in player_args:
                video_id = player_args['video_id']

        if video_id:
            params['video_id'] = video_id
            params['eurl'] = 'https://youtube.googleapis.com/v/' + video_id
        else:
            raise YouTubeException('_method_get_video_info: no video_id')

        cipher = None
        if js:
            if not js.startswith('http'):
                js = 'http://www.youtube.com/%s' % js.lstrip('/')
            self._context.log_debug('Cipher: js player: |%s|' % js)
            cipher = Cipher(self._context, java_script_url=js)

        params['sts'] = player_config.get('sts', '')

        params['c'] = player_args.get('c', 'WEB')
        params['cver'] = player_args.get('cver', '1.20170712')
        params['cplayer'] = player_args.get('cplayer', 'UNIPLAYER')
        params['cbr'] = player_args.get('cbr', 'Chrome')
        params['cbrver'] = player_args.get('cbrver', '53.0.2785.143')
        params['cos'] = player_args.get('cos', 'Windows')
        params['cosver'] = player_args.get('cosver', '10.0')

        url = 'https://www.youtube.com/get_video_info'

        result = requests.get(url,
                              params=params,
                              headers=headers,
                              verify=self._verify,
                              allow_redirects=True)

        stream_list = []

        data = result.text
        params = dict(urlparse.parse_qsl(data))

        meta_info = {'video': {}, 'channel': {}, 'images': {}, 'subtitles': []}
        meta_info['video']['id'] = params.get('vid',
                                              params.get('video_id', ''))
        meta_info['video']['title'] = params.get('title', '')
        meta_info['channel']['author'] = params.get('author', '')
        try:
            meta_info['video']['title'] = meta_info['video']['title'].decode(
                'utf-8')
            meta_info['channel']['author'] = meta_info['channel'][
                'author'].decode('utf-8')
        except:
            pass

        meta_info['channel']['id'] = params.get('ucid', '')
        image_data_list = [{
            'from': 'iurlhq',
            'to': 'high'
        }, {
            'from': 'iurlmq',
            'to': 'medium'
        }, {
            'from': 'iurlsd',
            'to': 'standard'
        }, {
            'from': 'thumbnail_url',
            'to': 'default'
        }]
        for image_data in image_data_list:
            image_url = params.get(image_data['from'], '')
            if image_url:
                meta_info['images'][image_data['to']] = image_url

        meta_info['subtitles'] = Subtitles(self._context, video_id,
                                           captions).get_subtitles()

        if (params.get('status', '') == 'fail') or (playability_status.get(
                'status', 'ok').lower() != 'ok'):
            if (not ((playability_status.get('desktopLegacyAgeGateReason', 0)
                      == 1) and not self._context.get_settings().age_gate())
                    and not (
                        (playability_status.get('status', 'ok').lower()
                         == 'content_check_required')
                        and self._context.get_settings().offensive_content())):
                reason = params.get('reason')
                if not reason:
                    reason = playability_status.get('reason')
                    if not reason:
                        reason = 'UNKNOWN'
                        if 'errorScreen' in playability_status and 'playerErrorMessageRenderer' in playability_status[
                                'errorScreen']:
                            reason = playability_status['errorScreen'][
                                'playerErrorMessageRenderer'].get(
                                    'reason', {}).get('simpleText', 'UNKNOWN')
                raise YouTubeException(reason)

        if params.get('live_playback', '0') == '1':
            url = params.get('hlsvp', '')
            if url:
                stream_list = self._load_manifest(url,
                                                  video_id,
                                                  meta_info=meta_info)

        mpd_url = params.get('dashmpd', '')
        use_cipher_signature = 'True' == params.get('use_cipher_signature',
                                                    None)
        if mpd_url:
            mpd_sig_deciphered = True
            if (use_cipher_signature or re.search('/s/[0-9A-F\.]+', mpd_url)
                ) and (not re.search('/signature/[0-9A-F\.]+', mpd_url)):
                mpd_sig_deciphered = False
                if cipher:
                    sig = re.search('/s/(?P<sig>[0-9A-F\.]+)', mpd_url)
                    if sig:
                        signature = cipher.get_signature(sig.group('sig'))
                        mpd_url = re.sub('/s/[0-9A-F\.]+',
                                         '/signature/' + signature, mpd_url)
                        mpd_sig_deciphered = True
                else:
                    raise YouTubeException('Cipher: Not Found')
            if mpd_sig_deciphered:
                video_stream = {'url': mpd_url, 'meta': meta_info}
                if params.get('live_playback', '0') == '1':
                    video_stream['url'] += '&start_seq=$START_NUMBER$'
                    video_stream.update(self.FORMAT.get('9998'))
                else:
                    video_stream.update(self.FORMAT.get('9999'))
                stream_list.append(video_stream)
            else:
                raise YouTubeException('Failed to decipher signature')

        def parse_to_stream_list(stream_map_list):
            for item in stream_map_list:
                stream_map = dict(urlparse.parse_qsl(item))

                url = stream_map.get('url', None)
                conn = stream_map.get('conn', None)
                if url:
                    if 'sig' in stream_map:
                        url += '&signature=%s' % stream_map['sig']
                    elif 's' in stream_map:
                        if cipher:
                            url += '&signature=%s' % cipher.get_signature(
                                stream_map['s'])
                        else:
                            raise YouTubeException('Cipher: Not Found')

                    itag = stream_map['itag']
                    yt_format = self.FORMAT.get(itag, None)
                    if not yt_format:
                        raise Exception('unknown yt_format for itag "%s"' %
                                        itag)

                    if yt_format.get('discontinued', False) or yt_format.get('unsupported', False) or \
                            (yt_format.get('dash/video', False) and not yt_format.get('dash/audio', False)):
                        continue

                    video_stream = {'url': url, 'meta': meta_info}
                    video_stream.update(yt_format)
                    stream_list.append(video_stream)
                elif conn:
                    url = '%s?%s' % (conn, urllib.unquote(
                        stream_map['stream']))
                    itag = stream_map['itag']
                    yt_format = self.FORMAT.get(itag, None)
                    if not yt_format:
                        raise Exception('unknown yt_format for itag "%s"' %
                                        itag)

                    video_stream = {'url': url, 'meta': meta_info}
                    video_stream.update(yt_format)
                    if video_stream:
                        stream_list.append(video_stream)

        # extract streams from map
        url_encoded_fmt_stream_map = params.get('url_encoded_fmt_stream_map',
                                                '')
        if url_encoded_fmt_stream_map:
            url_encoded_fmt_stream_map = url_encoded_fmt_stream_map.split(',')
            parse_to_stream_list(url_encoded_fmt_stream_map)

        adaptive_fmts = params.get('adaptive_fmts', '')
        if adaptive_fmts:
            adaptive_fmts = adaptive_fmts.split(',')
            parse_to_stream_list(adaptive_fmts)

        # last fallback
        if not stream_list:
            raise YouTubeException('No streams found')

        return stream_list
Ejemplo n.º 9
0
    def _method_get_video_info(self, video_id):
        headers = {'Host': 'www.youtube.com',
                   'Connection': 'keep-alive',
                   'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
                   'Accept': '*/*',
                   'DNT': '1',
                   'Referer': 'https://www.youtube.com/tv',
                   'Accept-Encoding': 'gzip, deflate',
                   'Accept-Language': 'en-US,en;q=0.8,de;q=0.6'}

        params = {'video_id': video_id,
                  'hl': self.language,
                  'gl': self.region,
                  'eurl': 'https://youtube.googleapis.com/v/' + video_id,
                  'ssl_stream': '1',
                  'el': 'default',
                  'html5': '1'}

        html = self.get_watch_page(video_id)

        player_config = self.get_player_config(html)

        player_assets = player_config.get('assets', {})
        player_args = player_config.get('args', {})
        player_response = json.loads(player_args.get('player_response', '{}'))
        captions = player_response.get('captions', {})
        js = player_assets.get('js')

        cipher = None
        if js:
            if not js.startswith('http'):
                js = 'http://www.youtube.com/%s' % js
            cipher = Cipher(self._context, java_script_url=js)

        params['sts'] = player_config.get('sts', '')

        params['c'] = player_args.get('c', 'WEB')
        params['cver'] = player_args.get('cver', '1.20170712')
        params['cplayer'] = player_args.get('cplayer', 'UNIPLAYER')
        params['cbr'] = player_args.get('cbr', 'Chrome')
        params['cbrver'] = player_args.get('cbrver', '53.0.2785.143')
        params['cos'] = player_args.get('cos', 'Windows')
        params['cosver'] = player_args.get('cosver', '10.0')

        url = 'https://www.youtube.com/get_video_info'

        result = requests.get(url, params=params, headers=headers, verify=self._verify, allow_redirects=True)

        stream_list = []

        data = result.text
        params = dict(urlparse.parse_qsl(data))

        meta_info = {'video': {},
                     'channel': {},
                     'images': {},
                     'subtitles': []}
        meta_info['video']['id'] = params.get('vid', params.get('video_id', ''))
        meta_info['video']['title'] = params.get('title', '')
        meta_info['channel']['author'] = params.get('author', '')
        try:
            meta_info['video']['title'] = meta_info['video']['title'].decode('utf-8')
            meta_info['channel']['author'] = meta_info['channel']['author'].decode('utf-8')
        except:
            pass
        meta_info['channel']['id'] = params.get('ucid', '')
        image_data_list = [
            {'from': 'iurlhq', 'to': 'high'},
            {'from': 'iurlmq', 'to': 'medium'},
            {'from': 'iurlsd', 'to': 'standard'},
            {'from': 'thumbnail_url', 'to': 'default'}]
        for image_data in image_data_list:
            image_url = params.get(image_data['from'], '')
            if image_url:
                meta_info['images'][image_data['to']] = image_url
                pass
            pass

        meta_info['subtitles'] = Subtitles(self._context, video_id, captions).get_subtitles()

        if params.get('status', '') == 'fail':
            return self._method_watch(video_id, html, reason=params.get('reason', 'UNKNOWN'), meta_info=meta_info)

        if self._context.get_settings().use_dash():
            inputstream_version = xbmcaddon.Addon('inputstream.adaptive').getAddonInfo('version')
            live_dash_supported = LooseVersion(inputstream_version) >= LooseVersion('2.0.12')
        else:
            live_dash_supported = False

        if params.get('live_playback', '0') == '1':
            url = params.get('hlsvp', '')
            if url:
                stream_list = self._load_manifest(url, video_id, meta_info=meta_info)
            if not live_dash_supported:
                return stream_list

        mpd_url = params.get('dashmpd', '')
        use_cipher_signature = 'True' == params.get('use_cipher_signature', None)
        if mpd_url:
            mpd_sig_deciphered = True
            if (use_cipher_signature or re.search('/s/[0-9A-F\.]+', mpd_url)) and (not re.search('/signature/[0-9A-F\.]+', mpd_url)):
                mpd_sig_deciphered = False
                if cipher:
                    sig = re.search('/s/(?P<sig>[0-9A-F\.]+)', mpd_url)
                    if sig:
                        signature = cipher.get_signature(sig.group('sig'))
                        mpd_url = re.sub('/s/[0-9A-F\.]+', '/signature/' + signature, mpd_url)
                        mpd_sig_deciphered = True
                else:
                    raise YouTubeException('Cipher: Not Found')
            if mpd_sig_deciphered:
                video_stream = {'url': mpd_url,
                                'meta': meta_info}
                if params.get('live_playback', '0') == '1':
                    video_stream['url'] += '&start_seq=$START_NUMBER$'
                    video_stream.update(self.FORMAT.get('9998'))
                else:
                    video_stream.update(self.FORMAT.get('9999'))
                stream_list.append(video_stream)
            else:
                raise YouTubeException('Failed to decipher signature')

        # extract streams from map
        url_encoded_fmt_stream_map = params.get('url_encoded_fmt_stream_map', '')
        if url_encoded_fmt_stream_map:
            url_encoded_fmt_stream_map = url_encoded_fmt_stream_map.split(',')
            for item in url_encoded_fmt_stream_map:
                stream_map = dict(urlparse.parse_qsl(item))

                url = stream_map.get('url', None)
                conn = stream_map.get('conn', None)
                if url:
                    if 'sig' in stream_map:
                        url += '&signature=%s' % stream_map['sig']
                    elif 's' in stream_map:
                        if cipher:
                            url += '&signature=%s' % cipher.get_signature(stream_map['s'])
                        else:
                            raise YouTubeException('Cipher: Not Found')

                    itag = stream_map['itag']
                    yt_format = self.FORMAT.get(itag, None)
                    if not yt_format:
                        raise Exception('unknown yt_format for itag "%s"' % itag)

                    if yt_format.get('discontinued', False):
                        continue
                        pass

                    video_stream = {'url': url,
                                    'meta': meta_info}
                    video_stream.update(yt_format)
                    stream_list.append(video_stream)
                    pass
                elif conn:
                    url = '%s?%s' % (conn, urllib.unquote(stream_map['stream']))
                    itag = stream_map['itag']
                    yt_format = self.FORMAT.get(itag, None)
                    if not yt_format:
                        raise Exception('unknown yt_format for itag "%s"' % itag)

                    video_stream = {'url': url,
                                    'meta': meta_info}
                    video_stream.update(yt_format)
                    stream_list.append(video_stream)
                    pass
                pass
            pass

        # last fallback
        if not stream_list:
            raise YouTubeException('No streams found')

        return stream_list
Ejemplo n.º 10
0
    def _method_watch(self, video_id, html, reason=u'', meta_info=None):
        stream_list = []

        player_config = self.get_player_config(html)

        player_assets = player_config.get('assets', {})
        player_args = player_config.get('args', {})
        player_response = json.loads(player_args.get('player_response', '{}'))
        captions = player_response.get('captions', {})
        js = player_assets.get('js')

        _meta_info = {'video': {},
                      'channel': {},
                      'images': {},
                      'subtitles': []}
        meta_info = meta_info if meta_info else _meta_info

        if not meta_info['video'].get('id'):
            meta_info['video']['id'] = player_args.get('video_id', '')
        if not meta_info['video'].get('title'):
            meta_info['video']['title'] = player_args.get('title', '')
        if not meta_info['channel'].get('author'):
            meta_info['channel']['author'] = player_args.get('author', '')
        if not meta_info['channel'].get('id'):
            meta_info['channel']['id'] = player_args.get('ucid', '')
        try:
            meta_info['video']['title'] = meta_info['video']['title'].decode('utf-8')
            meta_info['channel']['author'] = meta_info['channel']['author'].decode('utf-8')
        except:
            pass

        if (not meta_info['images'].get('high')) or (not meta_info['images'].get('medium')) or \
                (not meta_info['images'].get('standard')) or (not meta_info['images'].get('default')):
            _related_args = '{}'
            lead = '\'RELATED_PLAYER_ARGS\': '
            tail = ',\n'
            pos = html.find(lead)
            if pos >= 0:
                html2 = html[pos + len(lead):]
                pos = html2.find(tail)
                if pos:
                    _related_args = html2[:pos]

            try:
                related_args = json.loads(_related_args)
                related_args = dict(urlparse.parse_qsl(related_args.get('rvs')))
            except:
                related_args = dict()

            image_data_list = [
                {'from': 'iurlhq', 'to': 'high'},
                {'from': 'iurlmq', 'to': 'medium'},
                {'from': 'iurlsd', 'to': 'standard'},
                {'from': 'thumbnail_url', 'to': 'default'}]
            for image_data in image_data_list:
                image_url = related_args.get(image_data['from'], '')
                if image_url:
                    meta_info['images'][image_data['to']] = image_url

        if not meta_info['subtitles']:
            meta_info['subtitles'] = Subtitles(self._context, video_id, captions).get_subtitles()

        if player_args.get('hlsvp'):
            return self._load_manifest(player_args.get('hlsvp'), video_id, meta_info=meta_info)

        cipher = None
        if js:
            if not js.startswith('http'):
                js = 'http://www.youtube.com/%s' % js
            cipher = Cipher(self._context, java_script_url=js)

        if player_args.get('url_encoded_fmt_stream_map'):
            url_encoded_fmt_stream_map = player_args.get('url_encoded_fmt_stream_map')
            url_encoded_fmt_stream_map = url_encoded_fmt_stream_map.split(',')

            for value in url_encoded_fmt_stream_map:
                attr = dict(urlparse.parse_qsl(value))

                try:
                    url = attr.get('url', None)
                    conn = attr.get('conn', None)
                    if url:
                        url = urllib.unquote(attr['url'])

                        signature = ''
                        if attr.get('s', ''):
                            signature = cipher.get_signature(attr['s'])
                            pass
                        elif attr.get('sig', ''):
                            signature = attr.get('sig', '')
                            pass

                        if signature:
                            url += '&signature=%s' % signature
                            pass

                        itag = attr['itag']
                        yt_format = self.FORMAT.get(itag, None)

                        if not yt_format:
                            raise Exception('unknown yt_format for itag "%s"' % itag)

                        # this format is discontinued
                        if yt_format.get('discontinued', False):
                            continue
                            pass

                        video_stream = {'url': url,
                                        'meta': meta_info}
                        video_stream.update(yt_format)

                        stream_list.append(video_stream)
                        pass
                    elif conn:  # rtmpe
                        url = '%s?%s' % (conn, urllib.unquote(attr['stream']))
                        itag = attr['itag']
                        yt_format = self.FORMAT.get(itag, None)
                        yt_format['rtmpe'] = True
                        if not yt_format:
                            raise Exception('unknown yt_format for itag "%s"' % itag)
                        video_stream = {'url': url,
                                        'meta': meta_info}
                        video_stream.update(yt_format)

                        stream_list.append(video_stream)
                        pass
                except Exception as ex:
                    pass

        # try to find the reason of this page if we've only got 'UNKNOWN'
        if len(stream_list) == 0 and reason.lower() == 'unknown':
            reason_match = re.search(r'<h1[^>]*>(?P<reason>[^<]+)', html)
            if reason_match:
                reason = reason_match.group('reason').strip()
                pass
            pass

        # this is a reason from get_video_info. We should at least display the reason why the video couldn't be loaded
        if len(stream_list) == 0 and reason:
            raise YouTubeException(reason)

        return stream_list
Ejemplo n.º 11
0
 def on_source(self, instance, value):
     self.color = (0, 0, 0, 0)
     self.subs = Subtitles(self.surl)
     self.sub = None
Ejemplo n.º 12
0
    def _method_watch(self, video_id, reason=u'', meta_info=None):
        stream_list = []

        headers = {
            'Host': 'www.youtube.com',
            'Connection': 'keep-alive',
            'User-Agent':
            'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
            'Accept': '*/*',
            'DNT': '1',
            'Referer': 'https://www.youtube.com',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'en-US,en;q=0.8,de;q=0.6'
        }

        params = {'v': video_id, 'hl': self.language, 'gl': self.region}

        if self._access_token:
            params['access_token'] = self._access_token

        url = 'https://www.youtube.com/watch'

        result = requests.get(url,
                              params=params,
                              headers=headers,
                              verify=self._verify,
                              allow_redirects=True)
        html = result.text
        """
        This will almost double the speed for the regular expressions, because we only must match
        a small portion of the whole html. And only if we find positions, we cut down the html.

        """
        pos = html.find('ytplayer.config')
        if pos >= 0:
            html2 = html[pos:]
            pos = html2.find('</script>')
            if pos:
                html = html2[:pos]
                pass
            pass

        _meta_info = {
            'video': {},
            'channel': {},
            'images': {},
            'subtitles': []
        }
        meta_info = meta_info if meta_info else _meta_info

        re_match_hlsvp = re.search(r'\"hlsvp\"[^:]*:[^"]*\"(?P<hlsvp>[^"]*\")',
                                   html)
        if re_match_hlsvp:
            hlsvp = urllib.unquote(re_match_hlsvp.group('hlsvp')).replace(
                '\/', '/')
            return self._load_manifest(hlsvp, video_id, meta_info=meta_info)

        re_match_js = re.search(r'\"js\"[^:]*:[^"]*\"(?P<js>.+?)\"', html)
        cipher = None
        if re_match_js:
            js = re_match_js.group('js').replace('\\', '').strip('//')
            if not js.startswith('http'):
                js = 'http://www.youtube.com/%s' % js
            cipher = Cipher(self._context, java_script_url=js)
            pass

        meta_info['subtitles'] = Subtitles(self._context, video_id).get()

        re_match = re.search(
            r'\"url_encoded_fmt_stream_map\"[^:]*:[^"]*\"(?P<url_encoded_fmt_stream_map>[^"]*\")',
            html)
        if re_match:
            url_encoded_fmt_stream_map = re_match.group(
                'url_encoded_fmt_stream_map')
            url_encoded_fmt_stream_map = url_encoded_fmt_stream_map.split(',')

            for value in url_encoded_fmt_stream_map:
                value = value.replace('\\u0026', '&')
                attr = dict(urlparse.parse_qsl(value))

                try:
                    url = attr.get('url', None)
                    conn = attr.get('conn', None)
                    if url:
                        url = urllib.unquote(attr['url'])

                        signature = ''
                        if attr.get('s', ''):
                            signature = cipher.get_signature(attr['s'])
                            pass
                        elif attr.get('sig', ''):
                            signature = attr.get('sig', '')
                            pass

                        if signature:
                            url += '&signature=%s' % signature
                            pass

                        itag = attr['itag']
                        yt_format = self.FORMAT.get(itag, None)

                        if not yt_format:
                            raise Exception('unknown yt_format for itag "%s"' %
                                            itag)

                        # this format is discontinued
                        if yt_format.get('discontinued', False):
                            continue
                            pass

                        video_stream = {'url': url, 'meta': meta_info}
                        video_stream.update(yt_format)

                        stream_list.append(video_stream)
                        pass
                    elif conn:  # rtmpe
                        url = '%s?%s' % (conn, urllib.unquote(attr['stream']))
                        itag = attr['itag']
                        yt_format = self.FORMAT.get(itag, None)
                        yt_format['rtmpe'] = True
                        if not yt_format:
                            raise Exception('unknown yt_format for itag "%s"' %
                                            itag)
                        video_stream = {'url': url, 'meta': meta_info}
                        video_stream.update(yt_format)

                        stream_list.append(video_stream)
                        pass
                except Exception as ex:
                    pass

        # try to find the reason of this page if we've only got 'UNKNOWN'
        if len(stream_list) == 0 and reason.lower() == 'unknown':
            reason_match = re.search(r'<h1[^>]*>(?P<reason>[^<]+)', html)
            if reason_match:
                reason = reason_match.group('reason').strip()
                pass
            pass

        # this is a reason from get_video_info. We should at least display the reason why the video couldn't be loaded
        if len(stream_list) == 0 and reason:
            raise YouTubeException(reason)

        return stream_list
Ejemplo n.º 13
0
    def _method_get_video_info(self, video_id):
        headers = {
            'Host': 'www.youtube.com',
            'Connection': 'keep-alive',
            'User-Agent':
            'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
            'Accept': '*/*',
            'DNT': '1',
            'Referer': 'https://www.youtube.com/tv',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'en-US,en;q=0.8,de;q=0.6'
        }
        params = {
            'video_id': video_id,
            'hl': self.language,
            'gl': self.region,
            'eurl': 'https://youtube.googleapis.com/v/' + video_id,
            'ssl_stream': '1',
            'ps': 'default',
            'el': 'default'
        }

        if self._access_token:
            params['access_token'] = self._access_token
            pass

        url = 'https://www.youtube.com/get_video_info'

        result = requests.get(url,
                              params=params,
                              headers=headers,
                              verify=self._verify,
                              allow_redirects=True)

        stream_list = []

        data = result.text
        params = dict(urlparse.parse_qsl(data))

        meta_info = {'video': {}, 'channel': {}, 'images': {}, 'subtitles': []}
        meta_info['video']['id'] = params.get('vid',
                                              params.get('video_id', ''))
        meta_info['video']['title'] = params.get('title', '')
        meta_info['channel']['author'] = params.get('author', '')
        try:
            meta_info['video']['title'] = meta_info['video']['title'].decode(
                'utf-8')
            meta_info['channel']['author'] = meta_info['channel'][
                'author'].decode('utf-8')
        except:
            pass
        meta_info['channel']['id'] = 'UC%s' % params.get('uid', '')
        image_data_list = [{
            'from': 'iurlhq',
            'to': 'high'
        }, {
            'from': 'iurlmq',
            'to': 'medium'
        }, {
            'from': 'iurlsd',
            'to': 'standard'
        }, {
            'from': 'thumbnail_url',
            'to': 'default'
        }]
        for image_data in image_data_list:
            image_url = params.get(image_data['from'], '')
            if image_url:
                meta_info['images'][image_data['to']] = image_url
                pass
            pass

        if params.get('status', '') == 'fail':
            return self._method_watch(video_id,
                                      reason=params.get('reason', 'UNKNOWN'),
                                      meta_info=meta_info)

        if params.get('live_playback', '0') == '1':
            url = params.get('hlsvp', '')
            if url:
                return self._load_manifest(url, video_id, meta_info=meta_info)
            pass
        """
        fmt_list = params.get('fmt_list', '')
        if fmt_list:
            fmt_list = fmt_list.split(',')
            for item in fmt_list:
                data = item.split('/')
                size = data[1].split('x')
                pass
            pass
        """

        # read adaptive_fmts
        """
        adaptive_fmts = params['adaptive_fmts']
        adaptive_fmts = adaptive_fmts.split(',')
        for item in adaptive_fmts:
            stream_map = dict(urlparse.parse_qsl(item))

            if stream_map['itag'] != '140' and stream_map['itag'] != '171':
                video_stream = {'url': stream_map['url'],
                                'yt_format': itag_map[stream_map['itag']]}
                stream_list.append(video_stream)
                pass
            pass
        """

        settings = self._context.get_settings()
        use_dash = settings.use_dash()

        if use_dash:
            if settings.dash_support_addon(
            ) and not self._context.addon_enabled('inputstream.adaptive'):
                if self._context.get_ui().on_yes_no_input(
                        self._context.get_name(),
                        self._context.localize(30579)):
                    use_dash = self._context.set_addon_enabled(
                        'inputstream.adaptive')
                else:
                    use_dash = False

            if use_dash:
                mpd_url = params.get('dashmpd', None)
                use_cipher_signature = 'True' == params.get(
                    'use_cipher_signature', None)
                if mpd_url:
                    if use_cipher_signature or re.search(
                            '/s/[0-9A-F\.]+', mpd_url):
                        # in this case we must call the web page
                        self._context.log_info(
                            'Unable to use mpeg-dash for %s, unable to decipher signature. Attempting fallback play method...'
                            % video_id)
                        return self._method_watch(video_id,
                                                  meta_info=meta_info)

                    meta_info['subtitles'] = Subtitles(self._context,
                                                       video_id).get()
                    video_stream = {
                        'url': mpd_url,
                        'title': meta_info['video'].get('title', ''),
                        'meta': meta_info
                    }
                    video_stream.update(self.FORMAT.get('9999'))
                    stream_list.append(video_stream)
                    return stream_list

        added_subs = False  # avoid repeat calls from loop or cipher signature
        # extract streams from map
        url_encoded_fmt_stream_map = params.get('url_encoded_fmt_stream_map',
                                                '')
        if url_encoded_fmt_stream_map:
            url_encoded_fmt_stream_map = url_encoded_fmt_stream_map.split(',')
            for item in url_encoded_fmt_stream_map:
                stream_map = dict(urlparse.parse_qsl(item))

                url = stream_map.get('url', None)
                conn = stream_map.get('conn', None)
                if url:
                    if 'sig' in stream_map:
                        url += '&signature=%s' % stream_map['sig']
                    elif 's' in stream_map:
                        # in this case we must call the web page
                        return self._method_watch(video_id,
                                                  meta_info=meta_info)

                    itag = stream_map['itag']
                    yt_format = self.FORMAT.get(itag, None)
                    if not yt_format:
                        raise Exception('unknown yt_format for itag "%s"' %
                                        itag)

                    if yt_format.get('discontinued', False):
                        continue
                        pass

                    if not added_subs:
                        added_subs = True
                        meta_info['subtitles'] = Subtitles(
                            self._context, video_id).get()
                    video_stream = {'url': url, 'meta': meta_info}
                    video_stream.update(yt_format)
                    stream_list.append(video_stream)
                    pass
                elif conn:
                    url = '%s?%s' % (conn, urllib.unquote(
                        stream_map['stream']))
                    itag = stream_map['itag']
                    yt_format = self.FORMAT.get(itag, None)
                    if not yt_format:
                        raise Exception('unknown yt_format for itag "%s"' %
                                        itag)
                    yt_format['video']['rtmpe'] = True
                    if not added_subs:
                        added_subs = True
                        meta_info['subtitles'] = Subtitles(
                            self._context, video_id).get()
                    video_stream = {'url': url, 'meta': meta_info}
                    video_stream.update(yt_format)
                    stream_list.append(video_stream)
                    pass
                pass
            pass

        # last fallback
        if not stream_list:
            return self._method_watch(video_id)

        return stream_list
Ejemplo n.º 14
0
# Paths & files
SHOWFILE = "shows.cfg"
LOGFILE = "tvshowlog.txt"
SUBFILE = "subpen.txt"
SUBFOLDER = "/storage/USB/Descargas/Subtitles"


if __name__ == '__main__':
    # pylint: disable-msg=C0103
    
    # Initialize log
    log = Log (LOGFILE)

    # Initialize subtitler
    subtitler = Subtitles (SUBFILE, SUBFOLDER, log)
    subtitler.loadPendingSubs()

    # Initial log traces
    log.write("Runned at " + time.strftime("%d/%m/%Y %H:%M:%S"), 0)
    log.write("", 0)
    log.write("Checking for new episodes...", 0)
    
    # Initialize chapterer
    chapterer = Chapters (SHOWFILE, subtitler, log)
    chapterer.loadShows()

    # Check for new shows
    chapterer.checkShows()

    log.write("", 0)