示例#1
0
 def _download_segment(self, f):
     url = make_url(self._file_playlist.url, f['file'])
     name = 'seg_' + next(tempfile._get_candidate_names())
     path = os.path.join(self.path, name)
     d = self._download_page(url, path, f)
     if self.n_segments_keep != 0:
         file = open(path, 'wb')
         d.addCallback(lambda x: file.write(x))
         d.addBoth(lambda _: file.close())
         d.addCallback(lambda _: path)
         d.addErrback(self._got_file_failed)
         d.addCallback(self._got_file, url, f)
     else:
         d.addCallback(lambda _: (None, path, f))
     return d
示例#2
0
	def _download_segment(self, f):
		url = make_url(self._file_playlist.url, f['file'])
		name = 'seg_' + next(tempfile._get_candidate_names())
		path = os.path.join(self.path, name)
		d = self._download_page(url, path, f)
		if self.n_segments_keep != 0:
			file = open(path, 'wb')
			d.addCallback(lambda x: file.write(x))
			d.addBoth(lambda _: file.close())
			d.addCallback(lambda _: path)
			d.addErrback(self._got_file_failed)
			d.addCallback(self._got_file, url, f)
		else:
			d.addCallback(lambda _: (None, path, f))
		return d
示例#3
0
	def update(self, content):
		# update this "constructed" playlist,
		# return wether it has actually been updated
		self.has_new_files = False
		if self._last_content and content == self._last_content:
			self._update_tries += 1
			return False

		self._update_tries = 0
		self._last_content = content

		def get_lines_iter(c):
			c = c.decode("utf-8-sig")
			for l in c.split('\n'):
				if l.startswith('#EXT'):
					yield l
				elif l.startswith('#'):
					pass
				else:
					yield l

		self._lines = get_lines_iter(content)
		first_line = self._lines.next()
		if not first_line.startswith('#EXTM3U'):
			printl('Invalid first line:\n%r' % content,self,'E')
			raise Exception('Invalid first line: %r' % first_line)

		self.target_duration = None
		discontinuity = False
		allow_cache = None
		i = 0
		for l in self._lines:
			if l.startswith('#EXT-X-STREAM-INF'):
				def to_dict(l):
					i = re.findall('(?:[\w-]*="[\w\.\,]*")|(?:[\w-]*=[\w]*)', l)
					d = {v.split('=')[0]: v.split('=')[1].replace('"','') for v in i}
					return d
				d = to_dict(l[18:])
				uri = self._lines.next()
				if uri.startswith('./'): uri = uri[2:]
				d['uri'] = uri
				self._add_playlist(d)
			elif l.startswith('#EXT-X-TARGETDURATION'):
				self.target_duration = int(l[22:])
			elif l.startswith('#EXT-X-MEDIA-SEQUENCE'):
				self.media_sequence = int(l[22:])
				i = self.media_sequence
			elif l.startswith('#EXT-X-DISCONTINUITY'):
				discontinuity = True
			elif l.startswith('#EXT-X-PROGRAM-DATE-TIME'):
				print l
			elif l.startswith('#EXT-X-BYTE-SIZE'):
				print l
			elif l.startswith('#EXT-X-BYTERANGE'):
				print l
			elif l.startswith('#EXT-X-ALLOW-CACHE'):
				allow_cache = l[19:]
			elif l.startswith('#EXT-X-KEY'):
				i = l[18:].find(',')
				if i > 0:
					self._encryption_method = l[18:18+i].strip()
				else:
					raise Exception("No Encryption method found")

				i=l.find('URI="')
				if i > 0:
					j=l[i+5:].find('"')
					self._key_url = l[i+5:j+i+5]

					j += i+5
					i=l[j:].find('IV=')
					if i > 0:
						self._iv = l[j+i+5:-1].strip().decode("hex")

					url = make_url(self.url, self._key_url)
					opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self._cookies))
					response = opener.open(url)
					self._key = response.read()
					response.close()
				elif self._encryption_method != 'NONE':
					raise Exception("No Encryption Key-URI found")
			elif l.startswith('#EXTINF'):
				v = l[8:].split(',')
				while True:
					f = self._lines.next().strip()
					if not f.startswith('#'):
						break
				if f.startswith('./'): f = f[2:]
				d = dict(file=f,
						title=v[1].strip(),
						duration=math.trunc(float(v[0])),
						sequence=i,
						discontinuity=discontinuity,
						allow_cache=allow_cache)
				discontinuity = False
				i += 1
				self._set_file(i, d)
				if i > self._last_sequence:
					self._last_sequence = i
			elif l.startswith('#EXT-X-ENDLIST'):
				if i > 0:
					self._files[i]['endlist'] = True
				self._endlist = True
				self._end_sequence = self._last_sequence - 1
				print l
			elif len(l.strip()) != 0:
				print l

		if not self.has_programs() and not self.target_duration:
			raise Exception("Invalid HLS stream: no programs & no duration")

		return True