-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathl2g_download.py
executable file
·86 lines (70 loc) · 2.4 KB
/
l2g_download.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#! /usr/bin/env python2
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010 - dasjoe [email protected]
#
# Licensed under GPL Version 3 or later
import os
import urllib
import re
import argparse
import sys
import subprocess
import math
from xml.dom.minidom import parse
class l2gURLopener(urllib.FancyURLopener):
version = "Mozilla 5.0"
def findInPath(prog):
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, prog)
if os.path.exists(exe_file) and os.access(exe_file, os.X_OK):
return exe_file
return False
def main():
parser = argparse.ArgumentParser(description='Download lectures from lecture2go')
parser.add_argument('url', help='URL to the lecture\'s video feed')
parser.add_argument('-l', '--list-cmd', action='store_true', help='Prints list of commands to fetch videos without actually downloading')
parser.add_argument('-n', '--number', action='store_true', help='Name downloaded files in chronological order')
args = parser.parse_args()
if args.url[-8:] != ".mp4.xml":
sys.exit("ERROR: URL is not a video feed")
list_cmd = False
if args.list_cmd:
list_cmd = True
number_files = False
if args.number:
number_files = True
loader = l2gURLopener();
#feed = loader.open(args.url).read()
feed = loader.open(args.url)
dom = parse(feed)
videos = []
for item in dom.getElementsByTagName("item"):
videoName = item.getElementsByTagName("title").item(0).firstChild.nodeValue
videoPage = item.getElementsByTagName("link").item(0).firstChild.nodeValue
videos.append((videoName, videoPage))
videos.sort()
downloads = []
for (videoName, videoPage) in videos:
pageText = loader.open(videoPage).read()
playerParams = re.search(r'showPlayer\(("rtmp",.*)\)',pageText).group(1)
playerParams = playerParams.replace('","', '|').replace('"', '').split('|')
videoURL = 'rtmp://fms.rrz.uni-hamburg.de:1935/vod/mp4:/' + playerParams[3] + '/' + playerParams[4]
downloads.append((videoURL, videoName))
number = 0
padding = int(math.log10(len(videos)))+1
for (videoURL, videoName) in downloads:
number = number + 1
num = ""
if (number_files):
num = "%0*d-"%(padding, number)
fileName = num + videoName + ".flv"
command = [findInPath("rtmpdump")]
command.extend(["-r", videoURL, "-e", "-o", fileName])
if list_cmd:
print ' '.join(command)
else:
print "Getting " + fileName
subprocess.Popen(command, shell=False).wait()
if __name__ == "__main__":
main()