Skip to content

Instantly share code, notes, and snippets.

@Xnuvers007
Last active November 27, 2023 15:02
Show Gist options
  • Save Xnuvers007/67ab9f62205e67502865f452abbaa892 to your computer and use it in GitHub Desktop.
Save Xnuvers007/67ab9f62205e67502865f452abbaa892 to your computer and use it in GitHub Desktop.
y2mate Xnuvers007 Youtube Downloader with python
import re
import requests
import tkinter as tk
from tkinter import messagebox
import webbrowser
def get_video_id(url):
pattern = re.compile(r'(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=|shorts\/)|youtu\.be\/)([a-zA-Z0-9_-]{11})')
match = pattern.search(url)
return match.group(1) if match else None
def open_browser(url):
webbrowser.open(url)
def format_download_info(info):
return f"Download Link: {info['k']}\nresolusi: {info['q_text']}\nsize: {info['size']}\n"
def analyze_youtube_video(youtube_url):
video_id = get_video_id(youtube_url)
if not video_id:
messagebox.showerror("Error", "Invalid YouTube URL. Could not extract video ID.")
return
cookies = {
'_ga_PSRPB96YVC': 'GS1.1.1701089943.1.1.1701090163.0.0.0',
'_ga': 'GA1.1.321171632.1701089943',
'prefetchAd_3381349': 'true',
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0',
'Accept': '*/*',
'Accept-Language': 'id,en-US;q=0.7,en;q=0.3',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Referer': f'https://www.y2mate.com/youtube/{video_id}',
'X-Requested-With': 'XMLHttpRequest',
'Origin': 'https://www.y2mate.com',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Connection': 'keep-alive',
'Alt-Used': 'www.y2mate.com',
}
data = f'k_query={youtube_url}&k_page=home&hl=en&q_auto=0'
try:
response = requests.post('https://www.y2mate.com/mates/en870/analyzeV2/ajax', cookies=cookies, headers=headers, data=data, timeout=5)
response.raise_for_status()
json_data = response.json()
judul = f"\nTitle: {json_data['title']}"
high = format_download_info(json_data['links']['mp4']['22']).replace(' <span class="label label-primary"><small>m-HD</small></span>', '')
medium = format_download_info(json_data['links']['mp4']['18'])
low = format_download_info(json_data['links']['mp4']['17'])
music = format_download_info(json_data['links']['mp3']['mp3128'])
result_text = judul + '\n' + '\n'.join([high, medium, low, music])
messagebox.showinfo("Video Information", result_text)
tanya = messagebox.askquestion("Download Link", "Do you want to open a download link in your default browser?")
if tanya == 'yes':
awto = json_data['links']['mp4']['auto']['k']
data2 = {"vid": video_id,
"k": awto
}
response = requests.post('https://www.y2mate.com/mates/convertV2/index', cookies=cookies, headers=headers, data=data2, timeout=5)
response.raise_for_status()
json2 = response.json()
webbrowser.open(json2['dlink'])
except requests.exceptions.RequestException as e:
messagebox.showerror("Error", f"An error occurred: {e}")
def on_submit():
youtube_url = entry.get()
analyze_youtube_video(youtube_url)
# GUI setup
root = tk.Tk()
root.title("YouTube Video Downloader")
label = tk.Label(root, text="Enter YouTube Video URL:")
label.pack(pady=10)
entry = tk.Entry(root, width=50)
entry.pack(pady=10)
submit_button = tk.Button(root, text="Submit", command=on_submit)
submit_button.pack(pady=10)
root.mainloop()
from sys import platform
import os
try:
import re, requests, webbrowser
except ModuleNotFoundError:
if platform == "linux" or platform == "linux2":
print("Installing requirements...")
os.system("pip3 install requests webbrowser")
print("Done")
elif platform == "darwin":
print("Installing requirements...")
os.system("pip3 install requests webbrowser")
print("Done")
elif platform == "win32":
print("Installing requirements...")
os.system("pip install requests webbrowser")
print("Done")
else:
print("Unknown OS")
exit()
finally:
import re, requests, webbrowser
def get_video_id(url):
pattern = re.compile(r'(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=|shorts\/)|youtu\.be\/)([a-zA-Z0-9_-]{11})')
match = pattern.search(url)
return match.group(1) if match else None
def analyze_youtube_video(youtube_url):
video_id = get_video_id(youtube_url)
if not video_id:
print("Invalid YouTube URL. Could not extract video ID.")
return
cookies = {
'_ga_PSRPB96YVC': 'GS1.1.1701089943.1.1.1701090163.0.0.0',
'_ga': 'GA1.1.321171632.1701089943',
'prefetchAd_3381349': 'true',
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0',
'Accept': '*/*',
'Accept-Language': 'id,en-US;q=0.7,en;q=0.3',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Referer': f'https://www.y2mate.com/youtube/{video_id}',
'X-Requested-With': 'XMLHttpRequest',
'Origin': 'https://www.y2mate.com',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Connection': 'keep-alive',
'Alt-Used': 'www.y2mate.com',
}
data = f'k_query={youtube_url}&k_page=home&hl=en&q_auto=0'
try:
response = requests.post('https://www.y2mate.com/mates/en870/analyzeV2/ajax', cookies=cookies, headers=headers, data=data, timeout=5)
response.raise_for_status()
json_data = response.json()
judul = f"\nTitle: {json_data['title']}"
high = f"Download Link: {json_data['links']['mp4']['22']['k']}\nresolusi: {json_data['links']['mp4']['22']['q_text']}\nsize: {json_data['links']['mp4']['22']['size']}\n".replace(' <span class="label label-primary"><small>m-HD</small></span>', '')
medium = f"Download Link: {json_data['links']['mp4']['18']['k']}\nresolusi: {json_data['links']['mp4']['18']['q_text']}\nsize: {json_data['links']['mp4']['18']['size']}\n"
low = f"Download Link: {json_data['links']['mp4']['17']['k']}\nresolusi: {json_data['links']['mp4']['17']['q_text']}\nsize: {json_data['links']['mp4']['17']['size']}\n"
awto = f"Download Link: {json_data['links']['mp4']['auto']['k']}ad Link: {json_data['links']['mp4']['auto']['k']}\nresolusi: {json_data['links']['mp4']['auto']['q_text']}\nsize: {json_data['links']['mp4']['auto']['size']}\n"
music = f"Download Link: {json_data['links']['mp3']['mp3128']['k']}\nresolusi: {json_data['links']['mp3']['mp3128']['q_text']}\nsize: {json_data['links']['mp3']['mp3128']['size']}\n"
print(judul)
counter = 0
for i in [high, medium, low, awto, music]:
counter += 1
print(counter,i)
tanya = str(input("Masukan Download Link yang ingin di download: "))
if tanya == "1":
tanya = json_data['links']['mp4']['22']['k']
elif tanya == "2":
tanya = json_data['links']['mp4']['18']['k']
elif tanya == "3":
tanya = json_data['links']['mp4']['17']['k']
elif tanya == "4":
tanya = json_data['links']['mp4']['auto']['k']
elif tanya == "5":
tanya = json_data['links']['mp3']['mp3128']['k']
else:
print("Pilihan tidak ada")
return
try:
data2 = {"vid": video_id,
"k": tanya
}
response = requests.post('https://www.y2mate.com/mates/convertV2/index', cookies=cookies, headers=headers, data=data2, timeout=5)
response.raise_for_status()
json2 = response.json()
print(f"Download Link: {json2['dlink']}")
print(f"Quality: {json2['fquality']}")
simpanatautidak = str(input("Simpan otomatis? (y/n): "))
if simpanatautidak == "y" or simpanatautidak == "Y":
if platform == "linux" or platform == "linux2":
webbrowser.open(json2['dlink'])
elif platform == "darwin":
webbrowser.open(json2['dlink'])
elif platform == "win32":
webbrowser.open(json2['dlink'])
else:
print("Download dibatalkan")
return
except Exception as e:
print(e)
return
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
youtube_url = input("Enter the YouTube video URL: ")
analyze_youtube_video(youtube_url)
@Xnuvers007
Copy link
Author

gambar

gambar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment