-
Notifications
You must be signed in to change notification settings - Fork 3
/
fileDiagSample.py
296 lines (241 loc) · 8.04 KB
/
fileDiagSample.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# coding: utf-8
# In[2]:
# Name: youParse.py
# Version: 1.3
# Author: pantuts
# Description: Parse URLs in Youtube User's Playlist (Video Playlist not Favorites)
# Use python3 and later
# Agreement: You can use, modify, or redistribute this tool under
# the terms of GNU General Public License (GPLv3).
# This tool is for educational purposes only. Any damage you make will not affect the author.
# Usage: python3 youParse.py youtubeURLhere
# sudo pip install youtube-dl
# download pafy from github
import re
import urllib.request
import urllib.error
import sys
from pafy import pafy
from tkinter import *
from tkinter import ttk
import tkinter.constants as Tkconstants
import tkinter.filedialog as tkFileDialog
import os
import subprocess
from threading import Thread
import queue as Queue
import time
def crawl(url):
sTUBE = ''
cPL = ''
amp = 0
final_url = []
if 'list=' in url:
eq = url.index('=') + 1
cPL = url[eq:]
if '&' in url:
amp = url.index('&')
cPL = url[eq:amp]
else:
print('Incorrect Playlist.')
exit(1)
try:
yTUBE = urllib.request.urlopen(url).read()
sTUBE = str(yTUBE)
except urllib.error.URLError as e:
print(e.reason)
tmp_mat = re.compile(r'watch\?v=\S+?list=' + cPL)
mat = re.findall(tmp_mat, sTUBE)
if mat:
if mat[0] == mat[1]:
mat.remove(mat[0]) #if there is duplicate, remove
for PL in mat:
yPL = str(PL)
if '&' in yPL:
yPL_amp = yPL.index('&')
final_url.append('http://www.youtube.com/' + yPL[:yPL_amp])
i = 0
while i < len(mat):
# sys.stdout.write(final_url[i] + '\n')
i = i + 1
return final_url, len(mat)
else:
print('No videos found.')
exit(1)
# if len(sys.argv) < 2 or len(sys.argv) > 2:
# print('USAGE: python3 youParse.py YOUTUBEurl')
# exit(1)
# else:
# url = sys.argv[1]
# if 'http' not in url:
# url = 'http://' + url
# listParser(url)
dir_opt = options = {}
options['initialdir'] = 'C:/'
options['mustexist'] = False
options['title'] = 'This is a title'
def askdirectory():
"""Returns a selected directoryname."""
newPath = tkFileDialog.askdirectory(**dir_opt)
if(newPath != ""):
savePath.set(newPath)
else:
print("Hick")
# Get the list of videos
def listParser(list_url, q, progressQ):
final_url, l = crawl(list_url)
uset = set(final_url)
print("Playlist fetched. Would retrieve download urls now...")
# print(uset)
i = 0
l = len(uset)
linkArr = []
for u in uset:
url = u
video = pafy.new(url)
s = video.getbest()
fname = re.sub(r'[<>:\"\/\\|\?\*]+', "_", s.title)+"."+s.extension
linkArr.append({
'url':s.url,
'title':s.title,
'ext':s.extension,
'filename':fname
})
i = i+1
progressQ.put(int(i*100/l))
q.put(linkArr)
# Fire IDM for downloading
def downloadVideos():
global allDownComplete
global downGenerator
print("Start Download next 3 video")
if allDownComplete:
if q.empty():
print("Download queue not ready yet.")
return
allDownComplete = False
downGenerator = downloaderCoroutine()
print(next(downGenerator))
else:
print(next(downGenerator))
def downloaderCoroutine():
global downStatus, linkArr
downStatus = 0
root.after(100, update_downStatus)
counter = 0
limit = 4
prcs = []
size = len(linkArr)
done = 0
for l in linkArr:
# C:\Program Files (x86)\Internet Download Manager\IDMan.exe" /n /d <link> /p <path> /f <filename>
comm = '\"C:\Program Files (x86)\Internet Download Manager\IDMan.exe\" /n /d \"' + l['url'] + '\" /p \"' + savePath.get() + '\" /f \"' + l['filename'] + '\"'
# os.system(comm)
prcs.append(subprocess.Popen(comm))
time.sleep(2)
done = done + 1
downStatus = int(done*100/size)
counter = counter + 1
print("Counter: "+str(counter))
print(l['filename'])
if(counter >= limit):
print("Press download again to continue...")
counter = 0
if done == size:
allDownComplete = True
return
yield downStatus
def update_downStatus():
downloadPb["value"] = downStatus
if(downStatus == 100):
return
root.after(100, update_downStatus)
def update_bar(parserThread, tkRoot):
global linkArr, downListChecks
if not progressQ.empty():
p = progressQ.get()
pb["value"] = p
if p == 100:
# Create a checklist for videos
linkArr = q.get()
i = 0
for l in linkArr:
iv = IntVar()
# iv.trace(callback = printChecks, mode = "w")
downListChecks.append(iv)
c = Checkbutton(tkRoot, text = l['title'], variable=iv)
c.grid(column=1, row=6 + i, sticky=W)
i = i+1
parserThread.join()
print("Parsing Complete")
return
root.after(100, update_bar, parserThread, tkRoot)
# Checkboxes
def printChecks(*args):
print(downListChecks[0].get())
print(args)
# Code for downloading
progressQ = Queue.Queue()
q = Queue.Queue()
def process():
print("URL:", listURL.get())
print("SAVE TO:", savePath.get())
url = listURL.get()
if(url != ""):
parserThread = Thread(target = listParser, args = [url, q, progressQ])
parserThread.daemon = True
parserThread.start()
update_bar(parserThread, mainframe)
else:
print("Nothing to get!!!")
# print("Ikes!!!")
root = Tk()
root.resizable(width=FALSE, height=FALSE)
# root.geometry('{}x{}'.format(600,400))
root.title("Youtube Playlist using IDM")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
listURL = StringVar()
savePath = StringVar()
# Default Path:
listURL.set("https://www.youtube.com/playlist?list=PLnKw1txyYzRn2ZZAaL-FC7QrCl66XAylw")
# Default Save path
# savePath.set(os.getcwd())
savePath.set("C:/Users/Bishal/Shared/Youtube/Animating Text - Autodesk Official")
# Labels
ttk.Label(mainframe, text = "Playlist URL: ").grid(column = 1, row=1, sticky = E)
ttk.Label(mainframe, text = "Save Path: ").grid(column = 1, row=2, sticky = E)
ttk.Label(mainframe, text = "Parse Progress: ").grid(column = 1, row=3, sticky = E)
ttk.Label(mainframe, text = "Download Progress: ").grid(column = 1, row=4, sticky = E)
inputWid = 100
# URL Input
url_entry = ttk.Entry(mainframe, textvariable = listURL, width = inputWid)
url_entry.grid(column = 2, row=1, columnspan = 4, sticky = (W, E))
# Path Input
path_entry = ttk.Entry(mainframe,textvariable = savePath)
path_entry.grid(column = 2, row=2, columnspan = 3,sticky = (W, E))
# Progressbar
pb = ttk.Progressbar(mainframe, orient=HORIZONTAL, mode='determinate')
pb.grid(column = 2, row=3, sticky = (W, E), columnspan=3)
pb["value"] = 0
pb["maximum"] = 100
# Progressbar
downloadPb = ttk.Progressbar(mainframe, orient=HORIZONTAL, mode='determinate')
downloadPb.grid(column = 2, row=4, sticky = (W,E), columnspan=3)
downloadPb["value"] = 0
downloadPb["maximum"] = 100
# Buttons
ttk.Button(mainframe, text = "Change Folder", command = askdirectory).grid(column = 5, row=2, sticky = (W,E))
ttk.Button(mainframe, text = "Parse", command = process).grid(column = 5, row=3, sticky = (W,E))
ttk.Button(mainframe, text = "Download", command = downloadVideos).grid(column = 5, row=4, sticky = (W,E))
# Global variables
downStatus = 0
allDownComplete = True
downGenerator = None
linkArr = []
downListChecks = []
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
url_entry.focus()
root.mainloop()