-
Notifications
You must be signed in to change notification settings - Fork 13
/
netflix.py
293 lines (214 loc) · 8.45 KB
/
netflix.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
import os
import re
import requests
from bs4 import BeautifulSoup
import json
from configparser import ConfigParser
from selenium import webdriver
import time
import codecs
import math
import Netflix_XmlToSrt
class netflixExtractor(object):
"""docstring for netflixExtractor"""
def __init__(self, url, testMode):
print("Detected Netflix\nProcessing....\n")
self.loginRequired = False
self.urlName = url
self.debug = True
self.testMode = testMode
self.requestsFileName = "iDoNotExistDefinitelyOnThisComputerFolder.html"
# Parameters requireed for Obtaining the URL
pass
def getSubtitles(self):
"""
The main function which uses helper functions to get the subtitles
"""
# self.createSoupObject()
check = self.loginNetflix()
self.title = "NetflixCaptions"
# self.getTitle()
# if self.debug:
# print(self.title)
if self.debug:
print("Resource List -\n", self.resourceList)
if not check:
self.deleteUnnecessaryfiles()
return 0
returnValue = self.standardFunctionCalls()
return returnValue
def createSoupObject(self, source):
# requestObject = requests.get(self.urlName)
# fileHandler = open("requests.txt", "w")
# fileHandler.write(requestObject.text)
# fileHandler.close()
self.soupObject = BeautifulSoup(source, "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 getSubtitleURL(self):
"""
The json content looks like this -
https://ipv4_1-lagg0-c018.1.sea001.ix.nflxvideo.net/?o=AQFtvJqRtkpGGH0jaYbAeHDGEm3tLGTZN8tCWrNrzICT9fS14rFqdUC3QcK4ubNZ9F3wIYkfEFWsRfSUnJjwk5xe1gzkZvg52clcbA_tI5ZRZRKeT8tIMgImep2skGqb8C8laZdAkTrGFDFSIJKP&v=3&e=1467941170&t=nKoR2V91TssNcDr5AGDUUPJ_AmA
"""
# If it is a movie, we use this methodology -
try:
SubsURL = ""
for names in self.resourceList:
if 'name' in names.keys():
if "/?o" in names['name']:
SubsURL = names['name']
return SubsURL
except:
print(
"Failed to fetch subtitles resource. Please ensure that subtitles are switched on by default for your account")
pass
pass
def downloadDfxpTranscript(self, SubsLink):
"""
This function fetches the captions and writes them into a file in VTT format
"""
try:
subRequestObject = requests.get(SubsLink)
# print(subRequestObject.text)
subsFileHandler = open(self.title + ".xml", "w")
print("Creating ~ '%s.xml' ..." % (self.title))
subsFileHandler.write(subRequestObject.text)
subsFileHandler.close()
return 1
except:
return 0
pass
def getTitle(self):
"""
This function returns the title of the video. This is also used for naming the file.
<meta name="twitter:title" content="Interstellar"/> --> Extracting the value from here
"""
# print(self.soupObject.title.string)
try:
s = self.soupObject.find("meta", attrs={"name": "twitter:title"})
self.title = str(s['content'])
self.title = self.title.strip()
if not self.title:
s = int("deliberateError")
except:
self.title = "Netflixsubtitles"
pass
def deleteUnnecessaryfiles(self):
if not self.debug:
try:
os.remove(self.requestsFileName)
os.remove(self.title + ".xml")
except:
pass
def standardFunctionCalls(self):
SubtitlesURL = self.getSubtitleURL()
if self.debug:
print("Subtitle URL - ", SubtitlesURL)
self.standardCheck(SubtitlesURL)
returnValue = self.downloadDfxpTranscript(SubtitlesURL)
self.standardCheck(returnValue)
self.convertXMLToSrt()
self.deleteUnnecessaryfiles()
return returnValue
pass
def standardCheck(self, variableToCheck):
if not variableToCheck:
print("Unable to fetch the subtitles.")
self.deleteUnnecessaryfiles()
exit()
def loginNetflix(self):
# Initialising the parser
userParser = ConfigParser()
userParser.read('userconfig.ini')
userParser.optionxform = str
parsingDictionary = {"service": "NETFLIX"}
# Required variables for filling in config file
baseurl = userParser.get(parsingDictionary['service'], 'url')
username = userParser.get(parsingDictionary['service'], 'username')
password = userParser.get(parsingDictionary['service'], 'password')
parser = ConfigParser()
parser.read('config.ini')
parser.optionxform = str
paths = {'usernameBox': "email",
'passwordBox': "password",
'submitButton': "login-button"
}
# firefox_profile = webdriver.FirefoxProfile()
# firefox_profile.set_preference('permissions.default.stylesheet', 2)
# firefox_profile.set_preference('permissions.default.image', 2)
# firefox_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so',
# 'false')
# netflixDriver = webdriver.Firefox(firefox_profile=firefox_profile)
netflixDriver = webdriver.Chrome()
netflixDriver.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:36.0) Gecko/20100101 Firefox/36.0 WebKit'
netflixDriver.cookiesEnabled = True
netflixDriver.javascriptEnabled = True
netflixDriver.get(baseurl)
mutliPageLogin = False
# Clearing Username TextBox
netflixDriver.find_element_by_name(paths['usernameBox']).clear()
# Typing in the username as obtained from config file
netflixDriver.find_element_by_name(
paths['usernameBox']).send_keys(username)
# Clearing password field
try:
netflixDriver.find_element_by_name(paths['passwordBox']).clear()
# It is a double page login. So we first need to click on "Next" and
# then send the password
except:
netflixDriver.find_element_by_class_name(
paths['submitButton']).click()
mutliPageLogin = True
time.sleep(1)
if mutliPageLogin:
netflixDriver.find_element_by_name(paths['passwordBox']).clear()
# Typing in the password
netflixDriver.find_element_by_name(
paths['passwordBox']).send_keys(password)
# Clicking on Submit button
netflixDriver.find_element_by_class_name(paths['submitButton']).click()
# temp = input()
netflixDriver.get(self.urlName)
pageSource = netflixDriver.page_source
self.createSoupObject(pageSource)
returnStatus = self.getResources(netflixDriver)
netflixDriver.close()
return returnStatus
def getResources(self, driver):
self.resourceList = []
print("Searching for subtitle source")
Found = False
Counter = 0
while not Found:
self.resourceList = driver.execute_script(
"return window.performance.getEntries();")
time.sleep(1)
print(".", end="")
for i in self.resourceList:
if 'name' in i.keys():
if "/?o" in i['name']:
Found = True
break
if len(self.resourceList) >= 60 or Counter >= 100:
print("Timed out. Trying anyway")
break
# return 0
Counter += 1
pass
return 1
def convertXMLToSrt(self):
"""
Taken from - https://github.com/isaacbernat/netflix-to-srt
"""
filename = self.title + ".xml"
outputFile = self.title + ".srt"
if self.debug:
print("Creating ~ '%s.srt' ..." % (self.title))
with codecs.open(filename, 'rb', "utf-8") as f:
text = f.read()
with codecs.open(outputFile, 'wb', "utf-8") as f:
f.write(Netflix_XmlToSrt.to_srt(text))