Edited at

軽量YouTube Downloader

More than 3 years have passed since last update.

投稿テスト


yotube_download.py

# -*- coding: utf-8 -*-

import urllib
import requests
import re
import sys

def getFileSize(url):
return requests.head(url).headers.get("content-length", None)

def getFileType(url):
return requests.head(url).headers.get("content-type", None).split("/")[-1]

def getTitle(url):
fp = urllib.urlopen(url, None)
source = fp.read()
fp.close()
r = re.compile("<meta name=\"title\" content=\"(.+?)\">")
m = r.search(source)
return m.group(0).replace("<meta name=\"title\" content=\"", "").replace("\">", "").decode("utf-8")

def seek(url):
urlList = []
fp = urllib.urlopen(url)
source = fp.read()
fp.close()
arr = source.split("url=")
for line in arr:
if line.startswith("http") and not line.count("www"):
urlList.append(urllib.unquote(line[0:line.find("\\u0026")]).replace("%2C", ","))
return urlList

def downloadFile(url, fileName):
r = requests.get(url, stream=True)
bufSize = 1024
bufCount = 0
totalSize = int(getFileSize(url))
with open(fileName, 'wb') as f:
for chunk in r.iter_content(chunk_size=bufSize):
bufCount += 1
if chunk:
f.write(chunk)
f.flush()
sys.stdout.write("%.0f %% ( %d KB )\r" % ((bufCount * bufSize * 100) / totalSize, totalSize / 1024))
sys.stdout.flush()

def main(url):
title = getTitle(url)
urlList = seek(url)
numberList = []
for i, line in enumerate(urlList):
if int(getFileSize(line)) != 0 :
print "Number=", i
numberList.append(i)
print "Size=%.2f(MB)" % (float(getFileSize(line)) / (1024 * 1024))
print "Type=" + getFileType(line)
print "---------------"

if len(numberList) == 0:
print "Not Found"
exit()
print "Input Download Number>"
while True:
num = input()
if num == -1:
exit()
elif num in numberList:
break
else:
print "Input Download Number>"

downloadFile(urlList[num], title + "." + getFileType(urlList[num]))
print "Download Complate!"

if __name__ == '__main__':
if len(sys.argv) == 2:
main(sys.argv[1])