-
Notifications
You must be signed in to change notification settings - Fork 25
/
youtube.py
178 lines (155 loc) · 4.74 KB
/
youtube.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
from bs4 import BeautifulSoup
import requests
import json
import selenium
import argparse
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.keys import Keys
from urllib.request import urlopen
import sys
import os
from getpass import getuser
from time import time, sleep
from colorama import Fore, Style
from colorama import init
init()
def progressBar(length):
show = ''
for i in range(length):
show += "#"
for j in range(30-length):
show += "-"
show = "|"+show+"|"
if length != 30:
if length % 3 == 0:
show = "/ " + show
elif length % 3 == 1:
show = "- " + show
else:
show = "\ " + show
return show
# ================================Transfer Speed Function=================
def transferRate(blocksize):
try:
global speed, rate, transferData
interval = time() - rate
if rate == 0:
transferData += blocksize
rate = time()
elif interval == 0 or interval < 1:
transferData += blocksize
else:
speed = float(transferData/(interval))
transferData = 0
rate = time()
return speed
except Exception:
pass
# ================================Type of Data============================
def dataSize(block):
if block/1024 < 1000:
block = block/1024
sizeType = 'KB'
elif block/1048576 < 1000:
block = block/1048576
sizeType = 'MB'
elif block/1073741824 < 1000:
block = block/1073741824
sizeType = 'GB'
return block, sizeType
# ================================Main Integrating function===============
def reporthook(blocknum, blocksize, total):
size = 0
currentType = ''
length = 30
percentage = 100.00
global speed, speedType, totalsize, sizeType
desc = int(blocknum*blocksize)
if total > 0:
length = int((desc/total)*30)
percentage = float(desc/total*100)
speed = transferRate(blocksize)
speedShow, speedType = dataSize(speed)
if totalsize == 0:
totalsize, sizeType = dataSize(total)
size, currentType = dataSize(desc)
progress = progressBar(length)
if percentage > 100:
percentage = 100
size = totalsize
elif percentage == 100 and total < 0:
totalsize = size
p1 = " %.2f %%" % (percentage)
p2 = " %s" % (progress)
p3 = " %.2f %s / %.2f %s %.2f %s/s " % (
size, currentType, totalsize, sizeType, speedShow, speedType)
sys.stderr.write("\r" +
p1 + Fore.GREEN + p2 + Style.RESET_ALL + p3 + Style.RESET_ALL)
sys.stderr.flush()
# =====================================Globar Variables===================
argv = sys.argv[1:]
url = ''
name = 'default'
opts = ''
args = ''
rate = 0
speed = 0
transferData = 0
totalsize = 0
speedType = ''
sizeType = ''
flag = 0
url = input("Enter the Youtube-url\n")
name = input("Enter the name for the video\n")
name = name+".mp4"
try:
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--driver", type=str, default="phantomjs",
help="which driver to use [option: phantomjs, firefox, chrome]")
args = vars(ap.parse_args())
choice = args["driver"]
driver = ""
if choice == "firefox":
binary = FirefoxBinary('firefox')
driver = webdriver.Firefox(firefox_binary=binary)
elif choice == "chrome":
driver = webdriver.Chrome();
elif choice == "phantomjs":
driver = webdriver.PhantomJS(
executable_path=r'phantomjs')
else:
print("Invalid Choice");
sys.exit(1);
driver.set_window_size(1120, 550)
driver.get("http://en.savefrom.net")
shURL = driver.find_element_by_xpath(
'//input[@id="sf_url" and @type="text"]')
shURL.send_keys(url)
sleep(2)
shSubmit = driver.find_element_by_xpath(
'//button[@id="sf_submit" and @class="submit" and @name="sf_submit"]')
shSubmit.click()
sleep(10)
soup = BeautifulSoup(driver.page_source, 'html.parser')
click = soup.find(
"a", class_="link link-download subname ga_track_events download-icon")
url_parse = click.get("href")
driver.quit()
print("Downloading Starts..\n")
with open(name, 'wb') as out_file:
with urlopen(url_parse) as fp:
x = int(fp.info()['Content-Length'])
i=1
block_size = 1024 * 8
while True:
block = fp.read(block_size)
reporthook(i,block_size,x)
i+=1
if not block:
break
out_file.write(block)
sys.stdout.write("\a")
print("\nDownload Completed..!!!")
except Exception as e:
print(e)