Last active 1458535125

A script that loads the given (list of) urls, waits for there to be a video tag on the page, then starts downloading the video using the page url as a filename (e.g http://example.com/my-show/episode-1 will produce my-show.episode-1.mp4)

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