Last active
November 13, 2022 02:48
-
-
Save shrimo/93585ec584b98e931600fca9a8b9ab40 to your computer and use it in GitHub Desktop.
Random YouTube Video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Random YouTube Video | |
It's just for fun, generate a random string | |
and try to find a youtube video with that string's id | |
''' | |
import random | |
import string | |
import requests | |
def separated(youtube_url, key='?v='): | |
''' Split address with id extraction ''' | |
return youtube_url.split(key) | |
def check_video_url(youtube_url): | |
''' Check address using native service ''' | |
video_id = separated(youtube_url) | |
checker_url = "https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=" | |
video_url = checker_url + video_id[1] | |
request = requests.get(video_url) | |
return request.status_code == 200 | |
def random_url(url, key='?v='): | |
''' Generate random id ''' | |
separated_url = separated(url) | |
string_size = len(separated_url[1]) | |
characters = string.ascii_letters + string.digits | |
rand_string = ''.join(random.choice(characters) for i in range(string_size)) | |
return separated_url[0]+key+rand_string | |
if __name__ == "__main__": | |
line = 'https://www.youtube.com/watch?v=qLuBhzx17w8' | |
print('Real url: ', line) | |
print(check_video_url(line)) | |
rand_line = random_url(line) | |
print('\nRand url: ', rand_line) | |
print(check_video_url(rand_line)) | |
sep_line = separated(line) | |
sep_rand_line = separated(line) | |
print('Id Size Comparison: ', len(sep_line[1]) == len(sep_rand_line[1])) | |
while True: | |
rand_youtube = random_url(line) | |
print(rand_youtube) | |
if check_video_url(rand_youtube): | |
print('*', check_video_url(rand_youtube)) | |
break | |
break | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment