-
Notifications
You must be signed in to change notification settings - Fork 13
/
hulu.py
321 lines (239 loc) · 9.45 KB
/
hulu.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import os
import re
import requests
from bs4 import BeautifulSoup
from random import randint
class huluExtractor(object):
"""docstring for huluExtractor"""
def __init__(self, url, testMode):
print("Detected Hulu\nProcessing....\n")
self.loginRequired = False
self.urlName = url
self.debug = True
self.testMode = testMode
self.requestsFileName = "iDoNotExistDefinitelyOnThisComputerFolder.html"
pass
def getSubtitles(self):
"""
The main function which uses helper functions to get the subtitles
"""
self.createSoupObject()
self.getTitle()
if self.debug:
print("Title -", self.title)
self.contentID = self.getContentID1() # Method-1
try:
self.contentID = int(self.contentID)
except:
print("Trying an alternative method to fetch Content ID")
self.contentID = self.getContentID2() # Method-2
try:
self.contentID = int(self.contentID)
except:
print("Unable to fetch the contentID.")
self.deleteUnnecessaryfiles()
return 0
if self.debug:
print("Content ID -", self.contentID)
smiLink = self.getSmiSubtitlesLink()
if not smiLink:
print("Unable to fetch the subtitles. No subtitles present.")
self.deleteUnnecessaryfiles()
return 0
if self.debug:
print("SMI LINK - ", smiLink)
vttLink = self.transformToVtt(smiLink)
if self.debug:
print("VTT LINK - ", vttLink)
self.createVttSubtitleFile(vttLink)
self.convertVttToSrt()
self.deleteUnnecessaryfiles()
return 1
def createSoupObject(self):
requestObject = requests.get(self.urlName)
# fileHandler = open("requests.txt", "w")
# fileHandler.write(requestObject.text)
# fileHandler.close()
self.soupObject = BeautifulSoup(
requestObject.text, "lxml", from_encoding="utf8")
# soupObject1 = BeautifulSoup(requestObject.text,"lxml")
# print(self.soupObject.original_encoding)
fh = open(self.requestsFileName, "w")
fh.write(str(self.soupObject))
fh.close()
pass
def getContentID1(self):
"""This is one of the methodologies to get the content ID. If this fails the alternative method will be called
In the Beautiful soup text it can be found that every video has this paramter.
\"content_id\": \"60535322\"
So we first use '"'(quotes) as the delimetter and split the text. Then access the content ID from the returned list.
"""
listedSoup = str(self.soupObject).split('"')
contentCounter = 0
for counter in range(len(listedSoup)):
if "content_id" in listedSoup[counter]:
contentCounter = counter + 2
break
# print(listedSoup[contentCounter])
contentId = ""
for i in listedSoup[contentCounter]:
if i.isdigit():
contentId += i
return contentId
def getContentID2(self):
"""
This is the alternative method to obtain the contentID.
Sample line 1) - <link href="http://ib3.huluim.com/video/60585710?region=US&size=220x124"
Sample line 2) - <link href="http://ib3.huluim.com/movie/60535322?region=US&size=220x318"
Required content ID's are 60585710 & 60535322 respectively.
Partition technique is used to obtain the content ID.
"""
fh = open(self.requestsFileName, "r")
listOfOptions = ["video/", "movie/"]
foundContent = False
contentId = ""
for line in fh:
for option in listOfOptions:
junkText, separator, contentIdContainer = line.partition(
option)
# The Content Id has been found.
if contentIdContainer:
foundContent = True
break
# The Content ID has been found. No need to read the file anymore.
# Get the Content ID from the container
if foundContent:
contentId, separator, junkText = contentIdContainer.partition(
"?")
if separator:
break
else:
foundContent = False
return contentId
pass
def getSmiSubtitlesLink(self, optionChoice=randint(1, 2)):
"""
This function returns the SMI subtitle link based on the contentID.
Currently, the link resides in the xmlLinkTemplate variable
The XML Link for any subtitle video is - "http://www.hulu.com/captions.xml?content_id=CONTENTID"
Where, CONTENTID is the unique content_ID of the video.
"""
smiLink = ""
xmlLinkTemplate = "http://www.hulu.com/captions.xml?content_id="
xmlLink = xmlLinkTemplate + str(self.contentID)
xmlRequest = requests.get(xmlLink)
if self.debug:
print(xmlRequest.text)
smiSoup = BeautifulSoup(xmlRequest.text, "lxml", from_encoding="utf8")
li = smiSoup.find("transcripts")
listOfLanguages = li.findChildren()
if self.debug:
print(listOfLanguages)
# If more than one language subtitles are present, the user can choose
# the desired language.
if len(listOfLanguages) > 1:
print(
"<<<------ Choose the corressponding number for selecting the language ----->>>")
for languages in range(len(listOfLanguages)):
print("<%d> - %s" %
(languages + 1, listOfLanguages[languages].name))
if not self.testMode:
optionChoice = input()
try:
optionChoice = int(optionChoice)
except:
print(
"You have entered an invalid option. Application will exit.")
exit()
if self.debug:
print(
smiSoup.find(listOfLanguages[optionChoice - 1].name).string)
try:
smiLink = smiSoup.find(
listOfLanguages[optionChoice - 1].name).string
except:
print(
"You have entered an invalid option. Application will exit.")
exit()
else:
if smiSoup.en:
smiLink = smiSoup.en.string
return smiLink
pass
def transformToVtt(self, smiLink):
"""
This function takes an smiLink and returns the corressponding subtitles in VTT format(a link)
Source - http://stream-recorder.com/forum/hulu-subtitles-t20120.html
http://assets.huluim.com/"captions"/380/60601380_US_en_en."smi" ----->
http://assets.huluim.com/"captions_webvtt"/380/60601380_US_en_en."vtt"
captions --> captions_webvtt
smi --> vtt
"""
# print(smiLink)
vttLink = ""
replaceDict = {"captions": "captions_webvtt", "smi": "vtt"}
for keys in replaceDict:
smiLink = smiLink.replace(keys, replaceDict[keys])
vttLink = smiLink
# print(vttLink)
return vttLink
pass
def createVttSubtitleFile(self, vttLink):
"""
This function fetches the captions and writes them into a file in VTT format
"""
requestObjectv = requests.get(vttLink)
# print(requestObjectv.text)
subsFileHandler = open(self.title + ".vtt", "w")
subsFileHandler.write(requestObjectv.text)
subsFileHandler.close()
pass
def convertVttToSrt(self):
"""
This function converts the VTT subtitle file into SRT format.
Credits - http://goo.gl/XRllyy for the conversion method.
"""
f = open(self.title + ".vtt", "r")
fh = open(self.title + ".srt", "w")
print("Creating ~ '%s.srt' ..." % (self.title))
count = 1
# Removing WEBVTT Header line.
for line in f.readlines():
if line[:6] == 'WEBVTT':
continue
# Substituting '.' with ',' in the time-stamps
line = re.sub(r'(:\d+)\.(\d+)', r'\1,\2', line)
# Printing the header number in each line. This is required for the
# SRT format.
if line == '\n':
fh.write("\n" + str(count) + "\n")
count += 1
else:
fh.write(line.strip() + "\n")
f.close()
fh.close()
def getTitle(self):
"""
This function returns the title of the video. This is also used for naming the file.
<meta name="twitter:title" value="Interstellar"/> --> Extracting the value from here
"""
# print(self.soupObject.title.string)
try:
s = self.soupObject.find("meta", attrs={"name": "twitter:title"})
try:
self.title = str(s['value'])
except:
self.title = str(s['content'])
self.title = self.title.strip()
if not self.title:
s = int("deliberateError")
except:
self.title = "DownloadedSubtitles"
pass
def deleteUnnecessaryfiles(self):
if not self.debug:
try:
os.remove(self.requestsFileName)
os.remove(self.title + ".vtt")
except:
pass