This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.py
146 lines (122 loc) · 4.93 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
import time
import datetime
import urllib.request
import wget
import sys
from urllib.parse import urlparse
import os
import argparse
import re
def getPath():
try:
approot = os.path.dirname(os.path.abspath(__file__))
except NameError: # We are the main py2exe script, not a module
approot = os.path.dirname(os.path.abspath(sys.argv[0]))
return approot
def openUrl(url):
LoginHeader = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 GTB7.1 (.NET CLR 3.5.30729)"}
req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
html = response.read()
return html.decode("utf-8")
def update_progress(progress):
sys.stdout.write('\r[{0}{1}] {2}'.format('#'*(progress/10), ' '*(10-progress/10), progress))
sys.stdout.flush()
def unixtime():
then = datetime.datetime.now()
return int(time.mktime(then.timetuple())*1e3 + then.microsecond/1e3)
def stringbetween(string,startstr,endstr):
start = string.find(startstr)
end = string.find(endstr,start+len(startstr))
return string[start+len(startstr):end]
def downloadvideo(url,name,vformat,path=None):
if path is None:
path = os.path.join(os.path.abspath('download'),'')
else:
path = os.path.join(os.path.abspath(path),'')
html = openUrl('http://convert2mp3.net/c-'+vformat+'.php?url='+url)
convertbefehl = stringbetween(html,'convert(',');')
idstart=convertbefehl.find('"')
idend=convertbefehl.find('"',idstart+1)
keystart=convertbefehl.find('"',idend+1)
keyend=convertbefehl.find('"',keystart+1)
csstart=convertbefehl.find('"',keyend+1)
csend=convertbefehl.find('"',csstart+1)
vid=convertbefehl[idstart+1:idend]
key=convertbefehl[keystart+1:keyend]
cs=convertbefehl[csstart+1:csend]
convertframe=stringbetween(html, '"convertFrame" src="', '"')
openUrl(convertframe)
html2 = '1'
#3 means ready to donwload for the website
while not "3" in html2 :
html2 = openUrl('http://convert2mp3.net/status.php?id='+vid+'&key='+key+'&cs='+cs+'&time='+str(unixtime()))
print ("")
print('Converting...')
time.sleep(1)
print ('Downloading...')
downloadurl='http://cdl'+cs+'.convert2mp3.net/download.php?id='+vid+'&key='+key
if not os.path.exists(path):
os.makedirs(path)
if name!='none':
filename = path + name +"."+vformat
wget.download(downloadurl,filename)
else :
wget.download(downloadurl,path)
return
def getVideoID(url):
url_data = urlparse.urlparse(url)
query = urlparse.parse_qs(url_data.query)
if not "v" in query:
print ('Video Url is not valid!')
sys.exit(1)
video = query["v"][0]
return video
def getPlaylistID(url):
url_data = urlparse(url)
query = urllib.parse.parse_qs(url_data.query)
plid = query["list"][0]
return plid
def unique(a):
seen = set()
return [seen.add(x) or x for x in a if x not in seen]
def getPlaylistVideos(url):
html = openUrl('http://www.youtube.com/playlist?list='+getPlaylistID(url))
links = re.findall('watch\?v=.{16}',html.rstrip('\n'))
for i,entry in enumerate(links):
links[i] = 'http://www.youtube.com/watch?v=' + stringbetween(entry,'watch?v=','&')
if not links:
print ('Couldnt read Playlist. Try again!')
sys.exit(1)
return unique(links)
def checkFormats(parser,x):
formats = ['mp3','m4a','acc','flac','ogg','wma','mp4','avi','wmv','3gp']
if not (x in formats):
parser.error("Format must be: "+' '.join(formats))
else :
return x
def dlplaylist(args):
plvideos = getPlaylistVideos(args.videourl)
prefix = ''
if args.name == 'none':
prefix = getPlaylistID(args.videourl)
else :
prefix = args.name
for i,entry in enumerate(plvideos):
if (i+1>=args.start and i+1<=args.end) :
downloadvideo(entry, str(i+args.startno)+" "+prefix, args.format, args.out)
return
aparser = argparse.ArgumentParser(description='Download Youtube Videos and Playlists over convert2mp3.net.')
aparser.add_argument('-f', '--format', type=lambda x: checkFormats(aparser,x), dest='format', action="store", default="mp3", help='format of the downloaded videos(see convert2mp3.net)')
aparser.add_argument('-n', '--name', dest='name', action="store", default="none", help='name of the downloaded video without fileextension')
aparser.add_argument('videourl', metavar='url', action="store", help='URL of the Video or Playlist')
aparser.add_argument('-pl', '--playlist', dest='playlist', action="store_true", help='downloads playlist')
aparser.add_argument('-no', '--startno', dest='startno', action="store", type=int, default=1, help='starting number of naming, if downloading playlist')
aparser.add_argument('-s', '--start', dest='start', action="store", type=int, default=1, help='starting number, if downloading playlist')
aparser.add_argument('-e', '--end', dest='end', action="store", type=int, default=1000, help='ending number, if downloading playlist')
aparser.add_argument('-o', '--output', dest='out', action="store", default=None, help='output directory')
args = aparser.parse_args()
if args.playlist:
dlplaylist(args)
else:
downloadvideo(args.videourl,args.name,args.format,args.out)