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)

Revision 295b52f666739352c0bd4ad8bd6c726cd2f047f9

getvideourl.py Raw
1#!/usr/bin/env python3
2
3import contextlib
4from selenium import webdriver
5from selenium.webdriver.support.ui import WebDriverWait
6from selenium.webdriver.support import expected_conditions as EC
7from selenium.common.exceptions import TimeoutException
8from subprocess import Popen
9from sys import argv, stderr, stdin
10from os import environ
11
12if 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