getvideourl.py
                        
                             · 1.3 KiB · Python
                        
                    
                    
                      
                        Raw
                      
                      
                        
                          
                        
                    
                    
                
                
            #!/usr/bin/env python3
import contextlib
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from subprocess import Popen
from sys import argv, stderr, stdin
from os import environ
if len(argv) > 1:
    if argv[1] == "-":
        urls = [line for line in stdin]
    else:
        urls = argv[1:]
    with contextlib.closing(webdriver.Firefox()) as driver:
        for i in urls:
            while True:
                try:
                    driver.get(i)
                    WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_tag_name('video'))
                    video = driver.find_element_by_tag_name("video").get_attribute("src")
                    show, title = driver.current_url.split("/")[-2:]
                    filename = "{}.{}.mp4".format(show, title[:title.index("?")])
                    driver.get("about:blank")
                    print("Getting {}".format(filename), file=stderr)
                    wget = Popen(["wget", "-O", filename, video])
                    wget.wait()
                except TimeoutException: # refreshes after ten seconds to try again
                    continue
                break
                | 1 | #!/usr/bin/env python3 | 
| 2 | |
| 3 | import contextlib | 
| 4 | from selenium import webdriver | 
| 5 | from selenium.webdriver.support.ui import WebDriverWait | 
| 6 | from selenium.webdriver.support import expected_conditions as EC | 
| 7 | from selenium.common.exceptions import TimeoutException | 
| 8 | from subprocess import Popen | 
| 9 | from sys import argv, stderr, stdin | 
| 10 | from os import environ | 
| 11 | |
| 12 | if len(argv) > 1: | 
| 13 | if argv[1] == "-": | 
| 14 | urls = [line for line in stdin] | 
| 15 | else: | 
| 16 | urls = argv[1:] | 
| 17 | with contextlib.closing(webdriver.Firefox()) as driver: | 
| 18 | for i in urls: | 
| 19 | while True: | 
| 20 | try: | 
| 21 | driver.get(i) | 
| 22 | WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_tag_name('video')) | 
| 23 | video = driver.find_element_by_tag_name("video").get_attribute("src") | 
| 24 | show, title = driver.current_url.split("/")[-2:] | 
| 25 | filename = "{}.{}.mp4".format(show, title[:title.index("?")]) | 
| 26 | driver.get("about:blank") | 
| 27 | print("Getting {}".format(filename), file=stderr) | 
| 28 | wget = Popen(["wget", "-O", filename, video]) | 
| 29 | wget.wait() | 
| 30 | except TimeoutException: # refreshes after ten seconds to try again | 
| 31 | continue | 
| 32 | break |