Source code for cgl.core.utils.web
import logging
import requests
[docs]
def url_exists(url):
"""
Checks to see if the given url exists
Args:
url: url address
"""
try:
request = requests.get(url)
if request.status_code == 200:
logging.info("Web site exists")
return True
else:
logging.info(
"Website returned response code: {code}".format(
code=request.status_code
)
)
return False
except ConnectionError:
logging.info("Web site does not exist")
return False
[docs]
def download_url(url, destination_file):
"""
Downloads the given file from url to destination file path.
Args:
url: url location of a file on the web
destination_file: file to be created
"""
r = requests.get(url, allow_redirects=True)
with open(destination_file, "w+") as f:
f.write(str(r.content))
[docs]
def download_youtube_video(url, destination_file):
"""
Downloads the given youtube video from url to destination file path.
Args:
url: url location of a youtube video
destination_file: file to be created
"""
import pytube
yt = pytube.YouTube(url)
stream = yt.streams.first()
stream.download(destination_file)
if __name__ == "__main__":
download_youtube_video(
"https://youtu.be/Y7geZmqIrs8?si=tOIwSQ2fcch4lnGs",
r"/Users/tmikota/Downloads",
)
print("done")