Skip to content

Instantly share code, notes, and snippets.

@Fuwn
Last active June 11, 2021 23:39
Show Gist options
  • Save Fuwn/0d9dc60c299fa693549180c0f2bcda56 to your computer and use it in GitHub Desktop.
Save Fuwn/0d9dc60c299fa693549180c0f2bcda56 to your computer and use it in GitHub Desktop.
# Imports
import re
import requests
import sys
import time
# Constants
YOUTUBE_HEADER_IMAGE_START_URL: str = "yt3.ggpht.com/"
YOUTUBE_HEADER_IMAGE_END_URL: str = "-no-nd-rj"
# Get username of requested channel
youtube_username: str = ""
if sys.argv.__len__() <= 1:
print("no username provided!"); exit(1)
else:
youtube_username = sys.argv[1]
# Get HTML source of requested channel
start = time.time() # Measure time taken to perform action
channel = requests.get("https://www.youtube.com/{}".format(youtube_username))
if channel.status_code == 404:
print("channel does not exist"); exit(1)
html: str = channel.text
print("got html source of channel {} in {} seconds".format(youtube_username, time.time() - start))
# RegEx parse HTML source for valid media URLs
start = time.time() # Measure time taken to perform action
# https://stackoverflow.com/questions/23918996/youtube-channel-banner-image-url/64306688#64306688
pattern = re.compile(YOUTUBE_HEADER_IMAGE_START_URL + "(.*?)" + YOUTUBE_HEADER_IMAGE_END_URL, flags=re.DOTALL)
for match in re.finditer(pattern, html):
image_url = match.group(1)
if image_url.__len__() < 500:
print("https://" + YOUTUBE_HEADER_IMAGE_START_URL + image_url + YOUTUBE_HEADER_IMAGE_END_URL)
print("completed regex search of channel {} in {} seconds".format(youtube_username, time.time() - start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment