ssebastianoo / yt-dlp-telegram

Use yt-dlp on Telegram
https://t.me/SatoruBot
MIT License
35 stars 13 forks source link

Using a .netrc file #9

Closed Linguiniotta closed 8 months ago

Linguiniotta commented 8 months ago

What is the proper way to use a .netrc file? I saw on the YoutubeDL options that there is a way to enable it, but I can't seem to do it properly.

The devscripts/cli_to_api.py does not seem to work with usenetrc, netrc, netrc_location, in the following command:

cli_to_api.py usenetrc netrc_location ./.netrc
cli_to_api.py netrc netrc_location ./.netrc

# Output of both commands:
The arguments passed translate to:

{}

Combining these with the CLI defaults gives:

{'extract_flat': 'discard_in_playlist',
 'fragment_retries': 10,
 'ignoreerrors': 'only_download',
 'postprocessors': [{'key': 'FFmpegConcat',
                     'only_multi_video': True,
                     'when': 'playlist'}],
 'retries': 10}

What is done

main.py Replace this line https://github.com/ssebastianoo/yt-dlp-telegram/blob/f13c58bfd9d251c77555aa14333a2fa20b60ab4a/main.py#L66 with: ``` ydl_opts = { 'usenetrc': True, # either of this does not work 'netrc': True, # either of this does not work 'netrc_location': './.netrc', 'format': format_id, 'outtmpl': f'outputs/{video_title}.%(ext)s', 'progress_hooks': [progress], 'postprocessors': [{ # Extract audio using ffmpeg 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', }] if audio else [], 'max_filesize': config.max_filesize } with yt_dlp.YoutubeDL(ydl_opts) as ydl: ```

.netrc

machine twitter.com login <username> password <password>

What is wrong

Errors when a URL from a private account/NSFW tweet is provided. WARNING: [twitter] Failed to parse .netrc: [Errno 2] No such file or directory: '/home/user/.netrc' [twitter] Extracting URL: https://twitter.com/user/status/00000000000000000?t=1AAAA2BBBBBBBBB&s=19 [twitter] 00000000000000000: Downloading guest token [twitter] 00000000000000000: Downloading GraphQL JSON ERROR: [twitter] 00000000000000000: NSFW tweet requires authentication. Use --cookies, --cookies-from-browser, --username and --password, --netrc-cmd, or --netrc (twitter) to provide account credentials 2024-02-06 16:29:06,409 (__init__.py:1021 MainThread) ERROR - TeleBot: "Infinity polling exception: ERROR: [twitter] 00000000000000000: NSFW tweet requires authentication. Use --cookies, --cookies-from-browser, --username and --password, --netrc-cmd, or --netrc (twitter) to provide account credentials" 2024-02-06 16:29:06,410 (__init__.py:1023 MainThread) ERROR - TeleBot: "Exception traceback: Traceback (most recent call last): File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/yt_dlp/YoutubeDL.py", line 1587, in wrapper return func(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/yt_dlp/YoutubeDL.py", line 1722, in __extract_info ie_result = ie.extract(url) ^^^^^^^^^^^^^^^ File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/yt_dlp/extractor/common.py", line 718, in extract ie_result = self._real_extract(url) ^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/yt_dlp/extractor/twitter.py", line 1371, in _real_extract status = self._extract_status(twid) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/yt_dlp/extractor/twitter.py", line 1349, in _extract_status status = self._graphql_to_legacy(self._call_graphql_api(self._GRAPHQL_ENDPOINT, twid), twid) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/yt_dlp/extractor/twitter.py", line 1226, in _graphql_to_legacy self.raise_login_required('NSFW tweet requires authentication') File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/yt_dlp/extractor/common.py", line 1178, in raise_login_required raise ExtractorError(msg, expected=True) yt_dlp.utils.ExtractorError: [twitter] 00000000000000000: NSFW tweet requires authentication. Use --cookies, --cookies-from-browser, --username and --password, --netrc-cmd, or --netrc (twitter) to provide account credentials During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/telebot/__init__.py", line 1016, in infinity_polling self.polling(non_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout, File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/telebot/__init__.py", line 1104, in polling self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout, File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/telebot/__init__.py", line 1179, in __threaded_polling raise e File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/telebot/__init__.py", line 1141, in __threaded_polling self.worker_pool.raise_exceptions() File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/telebot/util.py", line 149, in raise_exceptions raise self.exception_info File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/telebot/util.py", line 92, in run task(*args, **kwargs) File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/telebot/__init__.py", line 7860, in _run_middlewares_and_handler result = handler['function'](message) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/yt-dlp-telegram/data/app/main.py", line 198, in handle_private_messages download_video(message, text) File "/home/user/yt-dlp-telegram/data/app/main.py", line 78, in download_video info = ydl.extract_info(url, download=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/yt_dlp/YoutubeDL.py", line 1576, in extract_info return self.__extract_info(url, self.get_info_extractor(key), download, extra_info, process) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/yt_dlp/YoutubeDL.py", line 1605, in wrapper self.report_error(str(e), e.format_traceback()) File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/yt_dlp/YoutubeDL.py", line 1054, in report_error self.trouble(f'{self._format_err("ERROR:", self.Styles.ERROR)} {message}', *args, **kwargs) File "/home/user/yt-dlp-telegram/data/app/.tg-dl/lib/python3.11/site-packages/yt_dlp/YoutubeDL.py", line 993, in trouble raise DownloadError(message, exc_info) yt_dlp.utils.DownloadError: ERROR: [twitter] 00000000000000000: NSFW tweet requires authentication. Use --cookies, --cookies-from-browser, --username and --password, --netrc-cmd, or --netrc (twitter) to provide account credentials " ^C2024-02-06 16:29:18,498 (__init__.py:1027 MainThread) ERROR - TeleBot: "Infinity polling: polling exited" 2024-02-06 16:29:18,498 (__init__.py:1029 MainThread) ERROR - TeleBot: "Break infinity polling"

I apologize if there are grammatical mistakes in here, as English is not my first language. Please do not hesitate to correct and clarify things with me. Thank you very much.

ssebastianoo commented 8 months ago

This issue is related directly to yt-dlp, this repo is just an adaptation for telegram, I'm guessing you just mistyped the .netrc location

Linguiniotta commented 8 months ago

After some hours of figuring this out, restarting my pc seemed to have made it work? Here is my final modification.

ydl_opts = {
    'usenetrc': True,
    'netrc_location': "./.netrc",
    'format': format_id,
    'outtmpl': f'outputs/{video_title}.%(ext)s',
    'progress_hooks': [progress],
    'postprocessors': [{  # Extract audio using ffmpeg
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
    }] if audio else [], 'max_filesize': config.max_filesize
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl: