Skip to content

Commit d26b21f

Browse files
Integrated getit with youtube.py
1 parent 8c4884f commit d26b21f

File tree

1 file changed

+133
-12
lines changed
  • Download Youtube Video from cmd using python

1 file changed

+133
-12
lines changed
Lines changed: 133 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,129 @@
11
from bs4 import BeautifulSoup
22
import requests
3-
import urllib.request
43
import json
5-
import time
64
import selenium
75
from selenium import webdriver
86
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
97
from selenium.webdriver.common.keys import Keys
8+
from urllib.request import urlopen
9+
from getopt import GetoptError, getopt
10+
import sys
11+
import os
12+
from getpass import getuser
13+
from time import time, sleep
14+
from colorama import Fore, Style
15+
from colorama import init
16+
init()
1017

1118

19+
def progressBar(length):
20+
show = ''
21+
for i in range(length):
22+
show += "#"
23+
for j in range(30-length):
24+
show += "-"
25+
show = "|"+show+"|"
26+
if length != 30:
27+
if length % 3 == 0:
28+
show = "/ " + show
29+
elif length % 3 == 1:
30+
show = "- " + show
31+
else:
32+
show = "\ " + show
33+
return show
34+
35+
# ================================Transfer Speed Function=================
36+
37+
38+
def transferRate(blocksize):
39+
try:
40+
global speed, rate, transferData
41+
interval = time() - rate
42+
if rate == 0:
43+
transferData += blocksize
44+
rate = time()
45+
elif interval == 0 or interval < 1:
46+
transferData += blocksize
47+
else:
48+
speed = float(transferData/(interval))
49+
transferData = 0
50+
rate = time()
51+
return speed
52+
except Exception:
53+
pass
54+
55+
# ================================Type of Data============================
56+
57+
58+
def dataSize(block):
59+
if block/1024 < 1000:
60+
block = block/1024
61+
sizeType = 'KB'
62+
elif block/1048576 < 1000:
63+
block = block/1048576
64+
sizeType = 'MB'
65+
elif block/1073741824 < 1000:
66+
block = block/1073741824
67+
sizeType = 'GB'
68+
return block, sizeType
69+
70+
# ================================Main Integrating function===============
71+
72+
73+
def reporthook(blocknum, blocksize, total):
74+
size = 0
75+
currentType = ''
76+
length = 30
77+
percentage = 100.00
78+
global speed, speedType, totalsize, sizeType
79+
desc = int(blocknum*blocksize)
80+
if total > 0:
81+
length = int((desc/total)*30)
82+
percentage = float(desc/total*100)
83+
speed = transferRate(blocksize)
84+
speedShow, speedType = dataSize(speed)
85+
if totalsize == 0:
86+
totalsize, sizeType = dataSize(total)
87+
size, currentType = dataSize(desc)
88+
progress = progressBar(length)
89+
90+
if percentage > 100:
91+
percentage = 100
92+
size = totalsize
93+
elif percentage == 100 and total < 0:
94+
totalsize = size
95+
p1 = " %.2f %%" % (percentage)
96+
p2 = " %s" % (progress)
97+
p3 = " %.2f %s / %.2f %s %.2f %s/s " % (
98+
size, currentType, totalsize, sizeType, speedShow, speedType)
99+
sys.stderr.write("\r" +
100+
p1 + Fore.GREEN + p2 + Style.RESET_ALL + p3 + Style.RESET_ALL)
101+
sys.stderr.flush()
102+
103+
# =====================================Globar Variables===================
104+
105+
argv = sys.argv[1:]
106+
url = ''
107+
name = 'default'
108+
opts = ''
109+
args = ''
110+
rate = 0
111+
speed = 0
112+
transferData = 0
113+
totalsize = 0
114+
speedType = ''
115+
sizeType = ''
116+
flag = 0
117+
12118
url = input("Enter the Youtube-url\n")
13119
name = input("Enter the name for the video\n")
14-
name=name+".mp4"
120+
name = name+".mp4"
15121
try:
16122

17123
# Phantom JS
18124

19-
driver = webdriver.PhantomJS(executable_path=r'C:\Users\ankit\Downloads\phantomjs-2.1.1-windows (1)\phantomjs-2.1.1-windows\bin\phantomjs.exe')
125+
driver = webdriver.PhantomJS(
126+
executable_path=r'C:\Users\ankit\Downloads\phantomjs-2.1.1-windows (1)\phantomjs-2.1.1-windows\bin\phantomjs.exe')
20127
driver.set_window_size(1120, 550)
21128

22129
# FireFox
@@ -25,19 +132,33 @@
25132
# driver = webdriver.Firefox(firefox_binary=binary)
26133

27134
driver.get("http://en.savefrom.net")
28-
shURL = driver.find_element_by_xpath('//input[@id="sf_url" and @type="text"]')
135+
shURL = driver.find_element_by_xpath(
136+
'//input[@id="sf_url" and @type="text"]')
29137
shURL.send_keys(url)
30-
time.sleep(2)
31-
shSubmit = driver.find_element_by_xpath('//button[@id="sf_submit" and @class="submit" and @name="sf_submit"]')
138+
sleep(2)
139+
shSubmit = driver.find_element_by_xpath(
140+
'//button[@id="sf_submit" and @class="submit" and @name="sf_submit"]')
32141
shSubmit.click()
33-
time.sleep(10)
142+
sleep(10)
34143
soup = BeautifulSoup(driver.page_source, 'html.parser')
35-
click = soup.find("a", class_="link link-download subname ga_track_events download-icon")
144+
click = soup.find(
145+
"a", class_="link link-download subname ga_track_events download-icon")
36146
url_parse = click.get("href")
37147
driver.quit()
38148
print("Downloading Starts..\n")
39-
# print(url_parse)
40-
urllib.request.urlretrieve(url_parse, name)
41-
print("Download Completed..!!!")
149+
with open(name, 'wb') as out_file:
150+
with urlopen(url_parse) as fp:
151+
x = int(fp.info()['Content-Length'])
152+
i=1
153+
block_size = 1024 * 8
154+
while True:
155+
block = fp.read(block_size)
156+
reporthook(i,block_size,x)
157+
i+=1
158+
if not block:
159+
break
160+
out_file.write(block)
161+
sys.stdout.write("\a")
162+
print("\nDownload Completed..!!!")
42163
except Exception as e:
43164
print(e)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy