#!/usr/bin/python3 # -*- coding: UTF-8 -*- #author:Wos #it support proxy(when sometime the server is slow,you can change it) #auto convert youtube playlist link to invidious instance playlist link,then open with surf suckless browser #direct parser youtube and invidious play link to play with mpv import sys import requests import pyperclip import os import base64 import json import urllib import random import time fname = "" vname= "" tag = [18,22] pxy = "" proxies={} #modify the server list that fastest for you #server_list = ['https://invidious.slipfox.xyz','https://video.weiler.rocks','https://invidious.esmailelbob.xyz'] server_list = ['https://video.weiler.rocks'] def GetDesktopPath(): return os.path.join(os.path.expanduser("~"), 'Desktop') #download subtitle def downtxt(vid): try: global fname,vname ensub = "" cnsub = "" fname = "/tmp/"+str(vid)+".srt" url= "https://downsub.com/?url=https://www.youtube.com/watch?v="+str(vid) res = requests.get(url) txt1 = (res.text.split("context=\'")[1].split("'")[0]) txt2 = (str(base64.b64decode(txt1)).split("\"id\":\"")[1].split("\"")[0]) url2 = "https://get-info.downsub.com/"+str(txt2) res2 = requests.get(url2).json() vname = res2['title'] for j in res2['subtitles']: if j['name'] == "English (auto-generated)": ensub = j['url'] st = "[English (auto-generated)] " elif j['name'] == "English": ensub = j['url'] st = "[English] " if ensub == "": for j in res2['subtitlesAutoTrans']: if j['name'] == "English": ensub = j['url'] st = "[English] " url3 = "https://subtitle2.downsub.com/?title="+str(st)+str(vname).strip()+" [DownSub.com]&url="+str(ensub) res_ = requests.get(url3,stream=True) with open(fname, 'wb') as f: for i in res_.iter_content(chunk_size=1024): f.write(i) f.close() except: pass def play(url): try: vid = str(url).split("watch?v=")[1].split("&")[0] except: vid = str(url).split("embed/")[1].split("?")[0] pass downtxt(vid) i = 0 temp = "" premain = random.choice(server_list) print(premain) while i < len(tag): try: url2 = premain+"/latest_version?id="+vid+"&itag="+str(tag[i]) hdr = { 'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:68.0) Gecko/20100101 Goanna/4.8 Firefox/68.0 PaleMoon/29.4.4' } res = requests.get(url2,proxies=proxies,headers=hdr,allow_redirects=False) temp = res.headers['location'] break except: i += 1 continue if temp: link = premain+"/videoplayback?"+temp.split("videoplayback?")[1] link2 = link+"&title="+str(vid) if os.path.exists(fname): cmdline = ("sh -c \'mpv -really-quiet \"%s\" %s --ontop --sub-file=\"%s\" &\'"%(link2,pxy,fname)) else: cmdline = ("sh -c \'mpv -really-quiet \"%s\" %s --ontop &\'"%(link2,pxy)) pyperclip.copy(link2) os.system(cmdline) return else: return play(url) if __name__ == '__main__': pn = os.path.basename(sys.argv[0]) if len(sys.argv) == 2: if sys.argv[1] == '-22': tag = [22,18] elif sys.argv[1] == '-18': tag = [18,22] else: print(pn+" [-22] or [-18]") print("eg:"+pn+" -22") print("[22]:720p,[18]:360p") print("the play link sync to clipboard!") sys.exit() else: print(pn+" [-22] or [-18]") print("eg:"+pn+" -22") print("[22]:720p,[18]:360p") print("the play link will sync to clipboard!") sys.exit() sid = input("enable proxy Y/N(N)") if sid == "Y" or sid == "y": pxy = "--http-proxy=http://127.0.0.1:7890" proxies={'http':'127.0.0.1:7890','https':'127.0.0.1:7890'} else: pxy = "" proxies={} while True: try: os.system("clear") url = pyperclip.paste() print("invidious and youtube parser is running...") if "watch?v=" in str(url): play(url) continue elif "embed/" in str(url): play(url) continue elif str(url).startswith("https://www.youtube.com") or str(url).startswith("www.youtube.com"): premain = random.choice(server_list) url_ = str(url).replace("https://www.youtube.com",str(premain)) cmdline = ('sh -c \"surf -z 1.2 -a -d -s -T -g -n -x \\\"%s\\\" 2>/dev/null &\"'%(url_)) os.system(cmdline) pyperclip.copy("") continue else: time.sleep(3) continue except: sys.exit()