-
Notifications
You must be signed in to change notification settings - Fork 13
/
bbc.py
242 lines (178 loc) · 6.2 KB
/
bbc.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
import os
import re
import requests
from bs4 import BeautifulSoup
from configparser import ConfigParser
from BBC_XmlToSrt import toSrt
class bbcExtractor(object):
"""docstring for bbcExtractor"""
def __init__(self, url, testMode):
print("BBC processing")
self.loginRequired = False
self.urlName = url
self.requestsFileName = "iDoNotExistDefinitelyOnThisComputerFolder.html"
self.debug = True
self.testMode = testMode
pass
def getSubtitles(self):
"""
The main function which uses helper functions to get the subtitles
"""
self.createSoupObject()
episodeID = self.getEpisodeID()
if self.debug:
print("Episode ID - ", episodeID)
PIDLink = self.getPIDurl(episodeID)
if self.debug:
print("PIDLINK", PIDLink)
availablePIDs = self.getPID(PIDLink)
if self.debug:
print("Available PIDs ", availablePIDs)
self.getTitle()
if self.debug:
print("Title - ", self.title)
self.getSubtitleURL(availablePIDs)
if self.debug:
print("Subtitle URL - ", self.SubtitleUrl)
medianCheck = self.downloadXMLTranscript()
if medianCheck:
returnValue = self.convertXMLToSrt()
else:
returnValue = 0
self.deleteUnnecessaryfiles()
return returnValue
def createSoupObject(self):
requestObject = requests.get(self.urlName)
# fileHandler = open("requests.txt", "w")
# fileHandler.write(requestObject.text)
# fileHandler.close()
self.soupObject = BeautifulSoup(
requestObject.text, 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 getEpisodeID(self):
"""
This function returns the Episode ID from the URL given.
"""
searchStringList = ["episode/"]
juknkData, episodeName, IDContainer = self.urlName.partition(
searchStringList[0])
episodeID, Slash, Junk = IDContainer.partition("/")
return episodeID
pass
def getPIDurl(self, episodeID):
"""
This function makes the URL which contains the required PID
"""
parser = ConfigParser()
parser.read('config.ini')
programmeUrl = parser.get('BBC', 'programmeurl')
programmeUrl += episodeID
programmeUrl += ".xml"
return programmeUrl
pass
def getSubtitleURL(self, availablePIDs):
"""
This function fetches the subtitl URL based on the PIDs
"""
self.SubtitleUrl = ""
parser = ConfigParser()
parser.read('config.ini')
for pid in availablePIDs:
MediaUrl = parser.get('BBC', 'mediastream')
MediaUrl += pid
if self.debug:
print(MediaUrl)
try:
requestObject = requests.get(MediaUrl)
soup = BeautifulSoup(
requestObject.text, "lxml", from_encoding="utf8")
temp = soup.find("media", attrs={"kind": "captions"})
self.SubtitleUrl = temp.connection['href']
break
except:
pass
pass
def getPID(self, Link):
"""
This function returns the PID of the episode.
"""
requestObject = requests.get(Link)
# fileHandler = open("requests.txt", "w")
# fileHandler.write(requestObject.text)
# fileHandler.close()
pidList = []
self.soup = BeautifulSoup(
requestObject.text, "lxml", from_encoding="utf8")
try:
versionList = self.soup.programme.versions.findAll("version")
except:
print("Unable to get requested page.")
return []
for versions in versionList:
pidList.append(versions.pid.string)
return pidList
pass
def downloadXMLTranscript(self):
"""
This function fetches the captions and writes them into a file in XML
"""
try:
self.requestObjectv = requests.get(self.SubtitleUrl)
self.requestObjectv.encoding = 'utf-8'
print("Creating ~ '%s.xml' ..." % (self.title))
subsFileHandler = open(self.title + ".xml", "w")
if not self.requestObjectv.text:
return 0
self.requestObjectv = BeautifulSoup(self.requestObjectv.text, "lxml", from_encoding="utf8")
subsFileHandler.write(str(self.requestObjectv))
subsFileHandler.close()
return 1
except:
return 0
pass
def convertXMLToSrt(self):
try:
subsFileHandler = open(self.title + ".xml", "r")
xmlString = subsFileHandler.read()
subsFileHandler.close()
srtText = toSrt(xmlString)
subsFileHandler = open(self.title + ".srt", "w")
print("Creating ~ '%s.srt' ..." % (self.title))
subsFileHandler.write(srtText)
subsFileHandler.close()
return 1
except:
print("Couldn't convert to SRT")
return 0
pass
def getTitle(self):
"""
This function returns the title of the video. This is also used for naming the file.
<display_title>
<title>NAME</title>
<subtitle>OPTIONAL SUBTITLE</subtitle>
</display_title>
"""
try:
titleString = self.soup.programme.display_title.title.string
try:
titleString += self.soup.programme.display_title.subtitle.string
except:
pass
titleString = titleString.strip()
self.title = titleString.replace("/", "")
except:
self.title = "BBC_subtitles"
pass
pass
def deleteUnnecessaryfiles(self):
if not self.debug:
try:
os.remove(self.requestsFileName)
except:
pass