-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyPirateDownloader.py
executable file
·162 lines (139 loc) · 5.19 KB
/
pyPirateDownloader.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Encoding: UTF-8
import getopt, sys, os
from misc import (usage, onError,
defaultXmlSource)
from parseInput import dlListPart, generateDownloads
from convert import convertVideo
from download import finish
from parsePage import parseURL
url = ""
dlList = ""
urlsOnly = False
name = ""
bashOutFile = ""
setQuality = ""
convertTo = ""
videoInFile = ""
bestQuality = False
downloadAll = False
parseText = False
fileInfo = False
reEncode = False
listOnly = False
verbose = False
keepOld = False
reDownload = False
checkDuration = True
xmlSource = defaultXmlSource
##### handle arguments #####
try:
myopts, args = getopt.getopt(sys.argv[1:], 'u:l:L:o:b:q:c:f:x:HapiRskrnvh' ,
['url=',
'list=',
'urllist=',
'outfile=',
'bashfile=',
'quality=',
'convert=',
'file=',
'xmlsource='
'highest',
'all',
'parsetext',
'info',
'reencode',
'show',
'keepold',
'redownload',
'noduration',
'verbose'])
except getopt.GetoptError as e:
onError(1, str(e))
if len(sys.argv) == 1: # no options passed
onError(2, "No options given")
for option, argument in myopts:
if option in ('-u', '--url'):
url = argument
elif option in ('-l', '--list'):
dlList = argument
if not os.path.isfile(dlList):
onError(4, "%s is not a file" % dlList)
elif option in ('-L', '--urlList'):
dlList = argument
urlsOnly = True
if not os.path.isfile(dlList):
onError(51, "%s is not a file" % dlList)
elif option in ('-o', '--outfile'):
name = argument
elif option in ('-b', '--bashfile'):
bashOutFile = argument
checkDuration = False
elif option in ('-q', '--quality'):
setQuality = int(argument)
elif option in ('-c', '--convert'):
convertTo = argument.lower()
elif option in ('-f', '--file'):
videoInFile = argument
elif option in ('-x', '--xmlsource'):
xmlSource = argument.lower()
elif option in ('-H', '--highest'):
bestQuality = True
elif option in ('-a', '--all'):
downloadAll = True
fileInfo = True
elif option in ('-p', '--parsetext'):
parseText = True
elif option in ('-i', '--info'):
fileInfo = True
elif option in ('-R', '--reencode'):
reEncode = True
elif option in ('-s', '--show'):
listOnly = True
checkDuration = False
elif option in ('-k', '--keepold'):
keepOld = True
elif option in ('-r', '--redownload'):
reDownload = True
elif option in ('-n', '--noduration'):
checkDuration = False
elif option in ('-v', '--verbose'):
verbose = True
elif option in ('-h', '--help'):
usage(0)
if bestQuality and setQuality:
setQuality = ""
onError(48, "You can't set both -h and -q\nPrioritizing -h")
if bestQuality and downloadAll:
bestQuality = False
onError(49, "You can't set both -h and -a\nPrioritizing -a")
if not url and not dlList and not convertTo:
onError(3, "No program part chosen")
if url and not name and not parseText:
#onError(5, "Option -u also requires setting option -o or -p")
name = "null"
onError(50, "Assuming you want to download single link with option -o")
if url and parseText and not name:
onError(6, "Option -u and -p also requires setting option -o")
if reDownload and keepOld:
onError(11, "You can't select both --keepold (-k) and --redownload (-r)")
if not xmlSource in ('pirateplay', 'local', 'python'):
onError(63, "'%s' is not a valid option for -x, --xmlsource\nSetting it to %s" % (xmlSource, defaultXmlSource))
if url and not convertTo and not parseText:
downloads = generateDownloads(url, name, fileInfo, downloadAll, setQuality, bestQuality, checkDuration, verbose)
finish(downloads, keepOld, reDownload, checkDuration, listOnly, convertTo, bashOutFile, verbose)
elif dlList and not convertTo and not parseText:
downloads = dlListPart(dlList, urlsOnly, setQuality, checkDuration, fileInfo, bestQuality, downloadAll, verbose)
finish(downloads, keepOld, reDownload, checkDuration, listOnly, convertTo, bashOutFile, verbose)
elif url and parseText:
parseURL(url, name, verbose)
elif convertTo:
if not videoInFile:
onError(12, "You didn't set -f <video file>")
elif not os.path.isfile(videoInFile):
onError(13, "%s does not exist" % videoInFile)
elif os.path.islink(videoInFile):
onError(14, "%s is a link" % videoInFile)
else:
convertVideo(videoInFile, convertTo, reEncode, verbose)