-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.py
156 lines (111 loc) · 4.64 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
# Author: Gary McGovern
# functions to grab youtube audio links
# youtube.py
import requests
import re
import urllib.parse
import wget
from bs4 import BeautifulSoup
from OAuth import *
from functions import endPoints
def youtubeSearch(query, name, part='id', maxRes=20):
query = urllib.parse.quote("{0} {1}".format(query, name))
searchRes = requests.get(
'{0}search?part={1}&maxResults={2}&q={3}&type=video&videoCategoryId=10&key={4}'.format
(
endPoints("youtube"),
part,
maxRes,
query,
ytApiKey
),
)
jsonData = searchRes.json()
try:
# grab first result id only
videoID = jsonData['items'][0]['id']['videoId']
except KeyError:
# grab second result id only
videoID = jsonData['items'][1]['id']['videoId']
return videoID
#Temporary workaround, using external site(s)
def grabProtURL(ytID):
baseUrl = 'http://genyoutube.com/watch?v='
request = requests.get('{0}{1}'.format(baseUrl,ytID))
html = BeautifulSoup(request.text, "lxml")
pagelink = html.findAll('a', attrs={'id': 'getlink'})
#site changes, need to add extra step for link
for page in pagelink:
dl = re.search('{0}(.+?){1}'.format('href="', '"'), str(page))
if dl:
downlLink = dl.group(1).replace('amp;', '')
#grab audio link
request = requests.get(downlLink)
html = BeautifulSoup(request.text, "lxml")
links = html.findAll('a', href=True)
for link in links:
audio = str(link).find('itag=36')
if audio != -1:
l = re.search('{0}(.+?){1}'.format('href="','"'), str(link))
if l:
audioLink = l.group(1).replace('amp;','')
break
return audioLink
### wip, need to figure out how to decipher protected video signatures ###
### Currently grabs unprotected videos ###
def grabUrl(ytID):
baseUrl = 'http://www.youtube.com/watch?v='
url = requests.get('{0}{1}'.format(baseUrl,ytID))
soup = BeautifulSoup(url.text, "lxml")
div = soup.find("div", {"id" : "player-mole-container"})
scripts = div.findAll("script")[1].contents
links = str(scripts).split('u0026url=')
for link in links:
audio = link.find('mime%3Daudio%252Fwebm')
if audio != -1:
audioUrlUnformatted = link
#clean up url, remove unnecessary stuff
#link is unpredictable could end with slash or comma
urlSlash = link.find(r'\\')
urlComma = link.find(',')
if urlSlash < urlComma:
urlEnd = urlSlash
else:
urlEnd = urlComma
urlEncoded = link[0: urlEnd]
audioUrl = urllib.parse.unquote(urlEncoded)
break
def findString(string, start, end, start2 = ''):
s = re.search('{0}(.+?){1}'.format(start, end), string)
if s:
#This is mainly for grabbing the signature, it's tag is dependant
if len(s.group(1)) < 10:
s = re.search('{0}(.+?){1}'.format(start2, end), string)
comma = str(s.group(1)).find(',')
if comma != -1:
subString = str(s.group(1))[0: comma]
else:
subString = s.group(1)
else:
subString = s.group(1)
return subString
#host = findString(str(audioUrlUnformatted), 'https%3A%2F%2F', '.googlevideo') #'r5---sn-uigxxpx-q0cl'
#signature = findString(str(audioUrlUnformatted), 's=', r'\\', 'u0026s=')
#unSecSig = findString(str(audioUrl), 'signature%3D', '%')
#ei = findString(str(audioUrlUnformatted),'ei%3D', '%') #'Ri95WcLICdKe1gL0rakY'
#expire = findString(str(audioUrlUnformatted),'expire%3D', '%')
#id = findString(str(audioUrlUnformatted),'id%3', '%')
#dur = findString(str(audioUrlUnformatted),'dur%3D', '%')
#title = findString(str(scripts),'"title":"', '",').replace(' ', '+')
#ip = findString(str(audioUrlUnformatted),'ip%3D', '%')
#mn = findString(str(audioUrlUnformatted),'mn%3', '%')
#mt = findString(str(audioUrlUnformatted),'mt%3D', '%')
#lmt = findString(str(audioUrlUnformatted),'lmt%3D', '%')
#clen = findString(str(audioUrlUnformatted),'clen%3D', '%')
#initcwndbps = findString(str(audioUrlUnformatted),'initcwndbps%3D', '%')
try:
# Grab protected link
protUrl = grabProtURL(ytID)
return protUrl
except requests.exceptions.ConnectionError:
return audioUrl