From d078655560a5d29c45a98f5819072f63c6b583dd Mon Sep 17 00:00:00 2001 From: neosubhamoy Date: Fri, 30 Aug 2024 11:57:30 +0530 Subject: [PATCH] (feat): added network check before network request --- pytubepp/main.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pytubepp/main.py b/pytubepp/main.py index 1fdf453..2c2f292 100644 --- a/pytubepp/main.py +++ b/pytubepp/main.py @@ -3,7 +3,16 @@ from mutagen.id3 import ID3, APIC, TIT2, TPE1, TALB from tabulate import tabulate from tqdm import tqdm from importlib.metadata import version -import appdirs, ffmpy, requests, re, os, sys, random, shutil, platform, json, argparse, tempfile +import appdirs, ffmpy, requests, re, os, sys, random, shutil, platform, json, argparse, tempfile, subprocess + +def network_available(): + try: + param = '-n' if platform.system().lower() == 'windows' else '-c' + command = ['ping', param, '1', 'youtube.com'] + subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True) + return True + except subprocess.CalledProcessError: + return False def get_version(): try: @@ -232,6 +241,10 @@ def is_valid_url(url): return match def set_global_video_info(link): + if not network_available(): + print('\nRequest timeout! Please check your network and try again...!!') + sys.exit() + if is_valid_url(link): global video, author, title, thumbnail, views, stream, stream_resolutions, maxres link = is_valid_url(link).group(1)