Esempio n. 1
0
    def download_metadata(self, url: str, process_ie_data: bool):
        self._cache.clean()
        cache_key = f"{url}{process_ie_data}"
        cached_data = self._cache.get(cache_key)

        self._logger.debug(
            "Downloading metadata", url=url, cached=cached_data is not None
        )
        if cached_data:
            return cached_data

        options = {
            # Allow getting the _type value set to URL when passing a playlist entry
            "noplaylist": True,
            "extract_flat": False,
            # Causes ydl to return None on error
            "ignoreerrors": True,
            "quiet": True,
            "progress_hooks": [],
        }
        ydl = YoutubeDL(options)
        with ydl:
            try:
                metadata = ydl.extract_info(
                    url, download=False, process=process_ie_data
                )
                self._cache.register(cache_key, metadata)
                return metadata
            except Exception as e:
                self._logger.error("Downloading metadata error", url=url, error=e)
        return None
Esempio n. 2
0
def zap(session, service, **kwargs):
	errormsg = None
	if service and "http" in service.toString():
		url = service.toString()
		url = url.split(":")
		if len(url) > 9:
			url = url[10]
			if YoutubeDL is not None and url.startswith("YT-DLP%3a//"):
				url = url.replace("YT-DLP%3a//", "")
				url = url.replace("%3a", ":")
				try:
					ydl = YoutubeDL({"format": "b", "no_color": True})
					result = ydl.extract_info(url, download=False)
					result = ydl.sanitize_info(result)
					if result and result.get("url"):
						url = result["url"]
						print("[ChannelSelection] zap / YoutubeDLP result url %s" % url)
						return (url, errormsg)
					else:
						errormsg = "No Link found!"
						print("[ChannelSelection] zap / YoutubeDLP no streams")
				except Exception as e:
					errormsg = str(e)
					print("[ChannelSelection] zap / YoutubeDLP failed %s" % str(e))
					pass
	return (None, errormsg)
Esempio n. 3
0
 def test_post_hooks(self):
     self.params['post_hooks'] = [self.hook_one, self.hook_two]
     ydl = YoutubeDL(self.params)
     ydl.download([TEST_ID])
     self.assertEqual(self.stored_name_1, EXPECTED_NAME,
                      'Not the expected name from hook 1')
     self.assertEqual(self.stored_name_2, EXPECTED_NAME,
                      'Not the expected name from hook 2')
Esempio n. 4
0
 def test_proxy_with_idn(self):
     ydl = YoutubeDL({
         'proxy': f'127.0.0.1:{self.port}',
     })
     url = 'http://中文.tw/'
     response = ydl.urlopen(url).read().decode()
     # b'xn--fiq228c' is '中文'.encode('idna')
     self.assertEqual(response, 'normal: http://xn--fiq228c.tw/')
Esempio n. 5
0
    def test_nocheckcertificate(self):
        ydl = YoutubeDL({'logger': FakeLogger()})
        self.assertRaises(Exception, ydl.extract_info,
                          'https://127.0.0.1:%d/video.html' % self.port)

        ydl = YoutubeDL({'logger': FakeLogger(), 'nocheckcertificate': True})
        r = ydl.extract_info('https://127.0.0.1:%d/video.html' % self.port)
        self.assertEqual(r['entries'][0]['url'],
                         'https://127.0.0.1:%d/vid.mp4' % self.port)
Esempio n. 6
0
    def test_unicode_path_redirection(self):
        # XXX: Python 3 http server does not allow non-ASCII header values
        if sys.version_info[0] == 3:
            return

        ydl = YoutubeDL({'logger': FakeLogger()})
        r = ydl.extract_info('http://127.0.0.1:%d/302' % self.port)
        self.assertEqual(r['entries'][0]['url'],
                         'http://127.0.0.1:%d/vid.mp4' % self.port)
Esempio n. 7
0
 def test_format_note(self):
     ydl = YoutubeDL()
     self.assertEqual(ydl._format_note({}), '')
     assertRegexpMatches(self, ydl._format_note({
         'vbr': 10,
     }), r'^\s*10k$')
     assertRegexpMatches(self, ydl._format_note({
         'fps': 30,
     }), r'^30fps$')
Esempio n. 8
0
 def _run_test(self, **params):
     ydl = YoutubeDL({
         'logger': FakeLogger(),
         # Disable client-side validation of unacceptable self-signed testcert.pem
         # The test is of a check on the server side, so unaffected
         'nocheckcertificate': True,
         **params,
     })
     r = ydl.extract_info('https://127.0.0.1:%d/video.html' % self.port)
     self.assertEqual(r['entries'][0]['url'],
                      'https://127.0.0.1:%d/vid.mp4' % self.port)
Esempio n. 9
0
    def test_proxy(self):
        geo_proxy = f'127.0.0.1:{self.geo_port}'
        ydl = YoutubeDL({
            'proxy': f'127.0.0.1:{self.port}',
            'geo_verification_proxy': geo_proxy,
        })
        url = 'http://foo.com/bar'
        response = ydl.urlopen(url).read().decode()
        self.assertEqual(response, f'normal: {url}')

        req = urllib.request.Request(url)
        req.add_header('Ytdl-request-proxy', geo_proxy)
        response = ydl.urlopen(req).read().decode()
        self.assertEqual(response, f'geo: {url}')
Esempio n. 10
0
    def test_proxy(self):
        geo_proxy = '127.0.0.1:{0}'.format(self.geo_port)
        ydl = YoutubeDL({
            'proxy': '127.0.0.1:{0}'.format(self.port),
            'geo_verification_proxy': geo_proxy,
        })
        url = 'http://foo.com/bar'
        response = ydl.urlopen(url).read().decode('utf-8')
        self.assertEqual(response, 'normal: {0}'.format(url))

        req = compat_urllib_request.Request(url)
        req.add_header('Ytdl-request-proxy', geo_proxy)
        response = ydl.urlopen(req).read().decode('utf-8')
        self.assertEqual(response, 'geo: {0}'.format(url))
Esempio n. 11
0
def init():
    global ydl, log_file
    ydl_opts = {
        'continuedl': True,
        'outtmpl': {
            'default': '%(title)s-%(id)s.%(ext)s'
        },
        'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]',
        'noplaylist': True
    }
    ydl = YoutubeDL(ydl_opts)
    ydl.add_default_info_extractors()

    log_file = open("log.txt", "a")
    os.chdir(VID_PATH)
Esempio n. 12
0
    def get_url(self) -> None:
        """
        Getting the youtube direct link with yt-dlp, intended to be used
        with a QThread. It's guaranteed that either a success signal or a
        fail signal will be emitted.
        """

        with YoutubeDL(self.options) as ytdl:
            try:
                data = ytdl.extract_info(self.query, download=False)
            except Exception as e:
                # Any kind of error has to be caught, so that it doesn't only
                # send the error signal when the download wasn't successful
                # (a DownloadError from yt_dlp).
                logging.info("yt-dlp wasn't able to obtain the video: %s",
                             str(e))
                self.fail.emit()
            else:
                if len(data['entries']) == 0:
                    logging.info("yt-dlp returned no entries")
                    self.fail.emit()
                else:
                    self.success.emit(data)
            finally:
                self.finish.emit()
    def extractMetaData(self, link, qual, name):
        if "hotstar" in link or "sonyliv" in link:
            self.opts['geo_bypass_country'] = 'IN'

        with YoutubeDL(self.opts) as ydl:
            try:
                result = ydl.extract_info(link, download=False)
                if name == "":
                    name = ydl.prepare_filename(result)
                else:
                    name = name
                # noobway hack for changing extension after converting to mp3
                if qual == "audio":
                  name = name.replace(".mp4", ".mp3").replace(".webm", ".mp3")
            except DownloadError as e:
                self.onDownloadError(str(e))
                return
        if result.get('direct'):
            return None
        if 'entries' in result:
            video = result['entries'][0]
            for v in result['entries']:
                if v and v.get('filesize'):
                    self.size += float(v['filesize'])
            # For playlists, ydl.prepare-filename returns the following format: <Playlist Name>-<Id of playlist>.NA
            self.name = name.split(f"-{result['id']}")[0]
            self.vid_id = video.get('id')
            self.is_playlist = True
        else:
            video = result
            if video.get('filesize'):
                self.size = float(video.get('filesize'))
            self.name = name
            self.vid_id = video.get('id')
        return video
    def extractMetaData(self, link, name, get_info=False):

        if get_info:
            self.opts['playlist_items'] = '0'
        with YoutubeDL(self.opts) as ydl:
            try:
                result = ydl.extract_info(link, download=False)
                if get_info:
                    return result
                realName = ydl.prepare_filename(result)
            except DownloadError as e:
                if get_info:
                    raise e
                self.__onDownloadError(str(e))
                return

        if 'entries' in result:
            for v in result['entries']:
                try:
                    self.size += v['filesize_approx']
                except (KeyError, TypeError):
                    pass
            self.is_playlist = True
            if name == "":
                self.name = str(realName).split(f" [{result['id']}]")[0]
            else:
                self.name = name
        else:
            ext = realName.split('.')[-1]
            if name == "":
                self.name = str(realName).split(
                    f" [{result['id']}]")[0] + '.' + ext
            else:
                self.name = f"{name}.{ext}"
    def download_playlist_and_convert(self, link):
        print("\n[+] Reading playlist %s ..." % (link, ))

        try:
            with YoutubeDL(self.ydl_opts_playlists) as ydl:
                playlist_dict = ydl.extract_info(link, download=False)
                title = playlist_dict.get("title", None)
                title = title.replace(":", "")

                newdir = self.directory_original + "\\" + title

                if not os.path.exists(newdir):
                    os.makedirs(newdir)

                for video in playlist_dict["entries"]:
                    if video:
                        id_video = video.get("id")
                        title = video.get("title")
                        link = "https://www.youtube.com/watch?v=" + id_video
                        print("\n[+] Video found : %s - %s" % (
                            title,
                            link,
                        ))
                        filename = self.download_video(link, False)
                        only_name = os.path.basename(filename)
                        savefile = newdir + "\\" + only_name
                        self.convert_to_mp3(self.TEMP_VIDEO, savefile)
                        time.sleep(5)
        except:
            print("\n[-] Error download playlist")
            pass
Esempio n. 16
0
def get_file_ext(url):
    """
    Get the file extension of the audio file that would be extracted from the
    given YouTube video URL.

    :param url: YouTube video URL
    """
    opts = {
        # Always use the best audio quality available
        'format': 'bestaudio/best',
        'forcefilename': True,
        'noplaylist': True
    }
    # Try up to 3 times as youtubedl tends to be flakey
    for _ in range(settings.YOUTUBE_MAX_RETRIES):
        try:
            with YoutubeDL(opts) as ydl:
                info = ydl.extract_info(url, download=False)
                filename = ydl.prepare_filename(info)
                _, file_extension = os.path.splitext(filename)
                return file_extension
        except DownloadError:
            # Allow for retry
            pass
    raise Exception('get_file_ext failed')
Esempio n. 17
0
File: ytdl.py Progetto: monolit/ftgm
async def gget(uri, opts):
    import yt_dlp.utils
    yt_dlp.utils.std_headers[
        'User-Agent'] = '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"'
    with YoutubeDL(opts) as ydl:
        a = ydl.extract_info(uri, download=True)
        nama = ydl.prepare_filename(a)
    return a, nama
Esempio n. 18
0
def download(url, request_options):
    if request_options.get("platform") == "duboku":
        args = get_duboku_args(request_options)
        duboku.main(args['dir'], args['file'], args['from_ep'], args['to_ep'], url, sys.stdout, args['debug'],
                    args['proxy'], args['proxy_local'], args['downloadAll'])
    else:
        with YoutubeDL(get_ydl_options(request_options)) as ydl:
            ydl.download([url])
Esempio n. 19
0
    def test_parse_cmd(self):
        pp = ExecPP(YoutubeDL(), '')
        info = {'filepath': 'file name'}
        cmd = 'echo %s' % compat_shlex_quote(info['filepath'])

        self.assertEqual(pp.parse_cmd('echo', info), cmd)
        self.assertEqual(pp.parse_cmd('echo {}', info), cmd)
        self.assertEqual(pp.parse_cmd('echo %(filepath)q', info), cmd)
Esempio n. 20
0
    def test_parse_cmd(self):
        pp = ExecAfterDownloadPP(YoutubeDL(), '')
        info = {'filepath': 'file name'}
        quoted_filepath = compat_shlex_quote(info['filepath'])

        self.assertEqual(pp.parse_cmd('echo', info), 'echo %s' % quoted_filepath)
        self.assertEqual(pp.parse_cmd('echo.{}', info), 'echo.%s' % quoted_filepath)
        self.assertEqual(pp.parse_cmd('echo "%(filepath)s"', info), 'echo "%s"' % info['filepath'])
Esempio n. 21
0
class YtDlpResolver:
    def __init__(self):
        self.ytdl = YoutubeDL(ytdl_opts)

    def get_url(self, video_id: str):
        try:
            return self.ytdl.extract_info(f'https://www.youtube.com/watch?v={video_id}', download=False)['url']
        except Exception as ex:
            raise PluginError(ex)
Esempio n. 22
0
def list_playlist(url, dl_proxy):
    ydl_opts = {
        'proxy': dl_proxy,
        'quiet': True,
        'dump_single_json': True,
        'extract_flat': True,
    }
    with YoutubeDL(ydl_opts) as ydl:
        return ydl.extract_info(url)
def download_video(url):
    ydl_opts = {
        # 'quiet': True,
        "logger": MyLogger()
    }
    with YoutubeDL(ydl_opts) as ydl:
        logger.debug("Downloading video at %s", url)
        info_dict = ydl.extract_info(url, download=True)
        filename = ydl.prepare_filename(info_dict)
    return filename
Esempio n. 24
0
def downloadVideo(link):
    try:
        li = ["sex"]
        li[0] = link
        with YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(link, download=True)
            return ydl.prepare_filename(info)
    except:
        print("cannot download")
        return 1
Esempio n. 25
0
def get_meta_info(url):
    """
    Get metadata info from YouTube video.

    :param url: YouTube video URL
    """
    opts = {
        'format': 'bestaudio/best',
        'forcefilename': True,
        'noplaylist': True
    }
    # Try up to 3 times, as youtubedl tends to be flakey
    for _ in range(settings.YOUTUBE_MAX_RETRIES):
        try:
            with YoutubeDL(opts) as ydl:
                info = ydl.extract_info(url, download=False)
                filename = ydl.prepare_filename(info)

                parsed_artist = ''
                parsed_title = ''

                # Use youtube_title_parse library to attempt to parse the YouTube video title into
                # the track's artist and title.
                result = get_artist_title(info['title'])
                if result:
                    parsed_artist, parsed_title = result

                metadata = {
                    # YT video title
                    'title': info['title'],
                    # YT video uploader
                    'uploader': info['uploader'],
                    # YT video's embedded track artist (some official songs)
                    'embedded_artist':
                    info['artist'] if 'artist' in info else '',
                    # YT video's embedded track title (some official songs)
                    'embedded_title': info['track'] if 'track' in info else '',
                    # Artist name parsed from the YouTube video title
                    'parsed_artist': parsed_artist,
                    # Title parsed from the YouTube video title
                    'parsed_title': parsed_title,
                    # Duration of YouTube video in seconds
                    'duration': info['duration'],
                    # YouTube video URL
                    'url': info['webpage_url'],
                    # Filename (including extension)
                    'filename': filename
                }
                return metadata
        except KeyError:
            pass
        except DownloadError:
            # Allow for retry
            pass
    raise DownloadError('Unable to parse YouTube link')
Esempio n. 26
0
    def test_create_basenames_from_ydl_info_dict_video(self):
        ydl = YoutubeDL()
        result = self.tu.create_basenames_from_ydl_info_dict(
            ydl, info_dict_video)

        expected_result = set([
            'Video and Blog Competition 2017 - Bank Indonesia & '
            'NET TV #BIGoesToCampus [hlG3LeFaQwU]'
        ])

        self.assertEqual(result, expected_result)
Esempio n. 27
0
 def download(self, params, ep):
     params['logger'] = FakeLogger()
     ydl = YoutubeDL(params)
     downloader = HttpFD(ydl, params)
     filename = 'testfile.mp4'
     try_rm(encodeFilename(filename))
     self.assertTrue(downloader.real_download(filename, {
         'url': 'http://127.0.0.1:%d/%s' % (self.port, ep),
     }))
     self.assertEqual(os.path.getsize(encodeFilename(filename)), TEST_SIZE)
     try_rm(encodeFilename(filename))
Esempio n. 28
0
def queryNameFromURL(url):
    """Query video title from URL
    """

    with YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(url, download=False)

    if info is None:
        return -1

    return info['title']
 def __download(self, link):
     try:
         with YoutubeDL(self.opts) as ydl:
             try:
                 ydl.download([link])
             except DownloadError as e:
                 self.onDownloadError(str(e))
                 return
         self.__onDownloadComplete()
     except ValueError:
         LOGGER.info("Download Cancelled by User!")
         self.onDownloadError("Download Cancelled by User!")
Esempio n. 30
0
 def get(
     self,
     url: str,
     extra_info: Optional[Dict[str, Any]] = None,
     process: bool = False,
 ) -> List[Track]:
     if not (url or extra_info):
         raise errors.InvalidArgumentError()
     with YoutubeDL(self._ydl_config) as ydl:
         if not extra_info:
             info = ydl.extract_info(url, process=False)
         else:
             info = extra_info
         info_type = None
         if "_type" in info:
             info_type = info["_type"]
         if info_type == "url" and not info["ie_key"]:
             return self.get(info["url"], process=False)
         elif info_type == "playlist":
             tracks: List[Track] = []
             for entry in info["entries"]:
                 data = self.get("", extra_info=entry, process=False)
                 tracks += data
             return tracks
         if not process:
             return [
                 Track(service=self.name,
                       extra_info=info,
                       type=TrackType.Dynamic)
             ]
         try:
             stream = ydl.process_ie_result(info)
         except Exception:
             raise errors.ServiceError()
         if "url" in stream:
             url = stream["url"]
         else:
             raise errors.ServiceError()
         title = stream["title"]
         if "uploader" in stream:
             title += " - {}".format(stream["uploader"])
         format = stream["ext"]
         if "is_live" in stream and stream["is_live"]:
             type = TrackType.Live
         else:
             type = TrackType.Default
         return [
             Track(service=self.name,
                   url=url,
                   name=title,
                   format=format,
                   type=type)
         ]