示例#1
0
def r_getPage(url, *args, **kwargs):
    if FuturesSession != None:
        session = FuturesSession()
        method = kwargs.pop("method", "GET")
        user_agent = kwargs.pop("agent", None)
        if user_agent:
            headers = kwargs.pop("headers", {})
            headers["User-Agent"] = user_agent
            kwargs["headers"] = headers

        def cb_err_ret(err):
            return err

        d = Deferred()
        d.addErrback(cb_err_ret)
        try:

            def bg_cb(sess, resp):
                c = resp.content
                d.callback(c)

            if method == "GET":
                session.get(url, background_callback=bg_cb, *args, **kwargs)
            elif method == "POST":
                kwargs["data"] = kwargs.pop("postdata")
                session.post(url, background_callback=bg_cb, *args, **kwargs)
            return d
        except Exception, err:
            printl("Error: " + str(err), None, "E")
            d.errback(failure.Failure())
 def d_print(self, *args):
     global MDEBUG
     if MDEBUG:
         s = ''
         for arg in args:
             s += str(arg)
         printl(s)
	def getDLurl(self, url):
		try:
			content = self.getUrl(url)
			match = re.findall('flashvars.playlist = \'(.*?)\';', content)
			if match:
				for url in match:
					url = 'http://ua.canna.to/canna/'+url
					content = self.getUrl(url)
					match = re.findall('<location>(.*?)</location>', content)
					if match:
						for url in match:
							url = 'http://ua.canna.to/canna/'+url
							req = mechanize.Request('http://ua.canna.to/canna/single.php')
							response = mechanize.urlopen(req)
							req = mechanize.Request(url)
							req.add_header('User-Agent', ' Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
							response = mechanize.urlopen(req)
							response.close()
							code=response.info().getheader('Content-Location')
							url='http://ua.canna.to/canna/avzt/'+code
							return url

		except urllib2.HTTPError, error:
			printl(error,self,"E")
			message = self.session.open(MessageBoxExt, ("Fehler: %s" % error), MessageBoxExt.TYPE_INFO, timeout=3)
			return False
	def reqError(self, err, request):
		printl('[HLSProxy] Error:\n'+str(err),self,'E')
		if not request._disconnected:
			request.setResponseCode(400)
			request.write(str(err))
			request.finish()
		return NOT_DONE_YET
	def getDLurl(self, url):
		try:
			content = self.getUrl(url)
			match = re.findall('flashvars.playlist = \'(.*?)\';', content)
			if match:
				for url in match:
					url = 'http://ua.canna.to/canna/'+url
					content = self.getUrl(url)
					match = re.findall('<location>(.*?)</location>', content)
					if match:
						for url in match:
							req = mechanize.Request('http://ua.canna.to/canna/single.php')
							response = mechanize.urlopen(req)
							url = 'http://ua.canna.to/canna/'+url
							req = mechanize.Request(url)
							req.add_header('User-Agent', canna_agent)
							response = mechanize.urlopen(req)
							response.close()
							code=response.info().getheader('Content-Location')
							url='http://ua.canna.to/canna/avzt/'+code
							return url

		except urllib2.HTTPError, error:
			printl(error,self,"E")
			message = self.session.open(MessageBoxExt, (_("Error: %s") % error), MessageBoxExt.TYPE_INFO, timeout=3)
			return False
 def reqError(self, err, request):
     printl('[HLSProxy] Error:\n' + str(err), self, 'E')
     if not request._disconnected:
         request.setResponseCode(400)
         request.write(str(err))
         request.finish()
     return NOT_DONE_YET
示例#7
0
	def loadM3UList(self, m3ufile):
		self.ltype = 'sl_m3ulist'
		self.m3u_title = m3ufile.split('/')[-1]
		self['ContentTitle'].setText(self.m3u_title)
		self['F4'].show()
		self.filelist = []
		extm3u = extinf = False
		self.sp_option = title = path = ""
		try:
			with open(m3ufile, "rU") as inf:
				for line in inf:
					line = line.strip()
					if not extm3u and '#EXTM3U' in line:
						extm3u = True
						if '$MODE' in line:
							self.sp_option = line.split('=')[-1].strip()

						if self.sp_option != "IPTV":
							self.sp_option = "MP3"
					elif  extm3u and not extinf and line.startswith('#EXTINF:'):
						extinf = True
						m = re.search('\s*[-]*\d+(,|\s)(.+)', line[8:])
						if m:
							title = m.group(2).split(',')[-1].strip()
					elif extm3u and extinf and line:
						path = line

					if extinf and path:
						self.filelist.append((title, path, ''))
						title = path = ""
						extinf = False
		except IOError, e:
	def d_print(self,*args):
		global MDEBUG
		if MDEBUG:
			s = ''
			for arg in args:
				s += str(arg)
			printl(s)
 def updateChanList(self, blocking=False):
     if mutex.acquire(blocking):
         try:
             self.ml.setList(map(self.simpleListTVGListEntry, self.filelist))
         except:
             printl("Unexpected exception", self, "E")
         finally:
             mutex.release()
示例#10
0
 def updateChanList(self, blocking=False):
     if mutex.acquire(blocking):
         try:
             self.ml.setList(map(self.simpleListTVGListEntry,
                                 self.filelist))
         except:
             printl('Unexpected exception', self, 'E')
         finally:
             mutex.release()
	def get_program_playlist(self, program_id=None, bitrate=None):
		# return the (uri, dict) of the best matching playlist
		if not self.has_programs():
			printl("PL has not programs!",self,'E')
			raise Exception('PL has not programs!')

		_, best = min((abs(int(x['BANDWIDTH']) - bitrate), x)
				for x in self._programs)
		return best['uri'], best
	def saveCache(self):
		if not self.ismodified: return
		try:
			import cPickle as pickle
			picklefile = open(self.cachepath, 'w')
			pickle.dump(self.cache, picklefile)
			picklefile.close()
			self.ismodified = False
		except:
			printl('Error: saving cache "%s"' % self.cachepath,self,'E')
 def saveCache(self):
     if not self.ismodified: return
     try:
         import cPickle as pickle
         picklefile = open(self.cachepath, 'w')
         pickle.dump(self.cache, picklefile)
         picklefile.close()
         self.ismodified = False
     except:
         printl('Error: saving cache "%s"' % self.cachepath, self, 'E')
	def sendSegm(self, s_cache_item=None, r_range=None):
		self.playerReady = endlist = False
		while True:
			if s_cache_item != None:
				filepath = s_cache_item['filepath']
			else: filepath = ""

			if not self._request or (self._streamqueue.empty() and not s_cache_item):
				break
			if not filepath:
				filepath, endlist = self._streamqueue.get()
				if endlist:
					self._request.setResponseCode(200)
					self.set_seek_headers = True
			else:
				endlist = False
			try:
				size = os.path.getsize(filepath)
			except IOError, e:
				printl('File i/o error:'+str(e),self,'E')
				raise Exception(e)
				break
			else:
				with open(filepath, 'rb') as f:
					count = 0
					if self.set_seek_headers:
						if r_range == None:
							bytes_range = self.bytes_sent
						else: bytes_range = r_range
						self._request.setHeader('Content-Range', 'bytes=%d-%d/*' % (bytes_range, bytes_range+size-1))
						if endlist:
							self.content_size = str(bytes_range+size)
						self._clean_seek_headers = True
					elif self._clean_seek_headers:
						self._request.removeHeader('Content-Range')
						self._request.setResponseCode(200)
						self._clean_seek_headers = False

					self._request.write(f.read())
					count = size

				if s_cache_item == None:
					self._proxy_infos['seek_cache'].append({})
					self._proxy_infos['seek_cache'][-1]['bytes_range'] = self.bytes_sent
					if len(self._proxy_infos['seek_cache']) > 2:
						del self._proxy_infos['seek_cache'][0]
					self.bytes_sent += size
					self._proxy_infos['seek_cache'][-1]['bytes_size'] = size
					self._proxy_infos['seek_cache'][-1]['filepath'] = filepath
					self._on_about_to_finish()
				else:
					s_cache_item = None
				
				if self._seeking and not endlist:
					self.play()
 def readCache(self):
     try:
         import cPickle as pickle
         picklefile = open(self.cachepath, 'r')
         self.cache = pickle.load(picklefile)
         picklefile.close()
         self.resize(self.size)
         self.ismodified = False
     except:
         printl('Error: reading cache "%s"' % self.cachepath, self, 'E')
         self.cache = []
	def readCache(self):
		try:
			import cPickle as pickle
			picklefile = open(self.cachepath, 'r')
			self.cache = pickle.load(picklefile)
			picklefile.close()
			self.resize(self.size)
			self.ismodified = False
		except:
			printl('Error: reading cache "%s"' % self.cachepath,self,'E')
			self.cache = []
		def getKey(l):
			#print l
			self._key = None
			a = l[18:].find(',')
			if a > 0:
				self._encryption_method = l[18:18+a].strip()
				if self._encryption_method == 'AES-128':
					a=l.find('URI="')
					if a > 0:
						j=l[a+5:].find('"')
						self._key_url = l[a+5:j+a+5]
						j += a+5
						a=l[j:].find('IV=')
						if a > 0:
							self._iv = l[j+a+5:].strip().decode("hex")
						url = urlparse.urljoin(self.url, self._key_url)
						opener = requests.session()
						try:
							response = opener.get(url, cookies=self._cookies, headers=self._headers, timeout=30)
							self._key = response.content
						except:
							printl("No Encryption Key-URI found",self,'E')
					else:
						printl("No Encryption Key-URI found",self,'E')
				else:
					printl("No valid Encryption Method found",self,'E')
			else:
				self._encryption_method = l[18:].strip()
			if not self._encryption_method:
				printl("No Encryption method found",self,'E')
示例#18
0
	def showCoverFile(self, picPath):
		print "showCoverFile:"
		if fileExists(picPath):
			self._cover.instance.setPixmap(gPixmapPtr())
			scale = AVSwitch().getFramebufferScale()
			size = self._cover.instance.size()
			self.picload.setPara((size.width(), size.height(), scale[0], scale[1], False, 1, "#FF000000"))
			if self.picload.startDecode(picPath, 0, 0, False) == 0:
				ptr = self.picload.getData()
				if ptr != None:
					self._cover.instance.setPixmap(ptr)
					self._cover.show()
		else:
			printl("Coverfile not found: %s" % picPath, self, "E")

		if self._callback:
			self._callback()
	def showCoverFile(self, picPath, showNoCoverart=True):
		if fileExists(picPath):
			self._cover.instance.setPixmap(gPixmapPtr())
			scale = AVSwitch().getFramebufferScale()
			size = self._cover.instance.size()
			if mp_globals.fakeScale:
				self.picload.setPara((size.width(), size.height(), scale[0], scale[1], False, 1, "#00000000"))
			else:
				self.picload.setPara((size.width(), size.height(), scale[0], scale[1], False, 1, "#FF000000"))
			self.updateCover(picPath)
		else:
			printl("Coverfile not found: %s" % picPath, self, "E")
			if showNoCoverart and picPath != self._no_picPath:
				self.showCoverFile(self._no_picPath)

		if self._callback:
			self._callback()
示例#20
0
	def __getData(self, url, decode=False, postdata=None):
		if not url:
			printl("No URL given", self, "E")
			return None

		data = self.getData(url, postdata)
		if data:
			self.__parseCKS(data)

			if decode:
				js = re.findall('<script language=javascript>c="(.*?)";.*?x\("(.*?)"', data)
				if js:
					c = js[0][0]
					x = js[0][1]
					data = self.__decodeX(c,x)

		return data
示例#21
0
    def showCoverFile(self, picPath):
        print "showCoverFile:"
        if fileExists(picPath):
            self._cover.instance.setPixmap(gPixmapPtr())
            scale = AVSwitch().getFramebufferScale()
            size = self._cover.instance.size()
            self.picload.setPara((size.width(), size.height(), scale[0],
                                  scale[1], False, 1, "#FF000000"))
            if self.picload.startDecode(picPath, 0, 0, False) == 0:
                ptr = self.picload.getData()
                if ptr != None:
                    self._cover.instance.setPixmap(ptr)
                    self._cover.show()
        else:
            printl("Coverfile not found: %s" % picPath, self, "E")

        if self._callback:
            self._callback()
class CannaLink:
	def __init__(self, session):
		self.session = session
		self._errback = None
		self._callback = None

	def getLink(self, cb_play, cb_err, sc_title, sc_artist, sc_album, url, imgurl):
		self._errback = cb_err
		self._callback = cb_play

		stream_url = self.getDLurl(url)
		if stream_url:
			cb_play(sc_title, stream_url, album=sc_album, artist=sc_artist, imgurl=imgurl)
		else:
			cb_err(_('No URL found!'))

	def getDLurl(self, url):
		try:
			content = self.getUrl(url)
			match = re.findall('flashvars.playlist = \'(.*?)\';', content)
			if match:
				for url in match:
					url = 'http://ua.canna.to/canna/'+url
					content = self.getUrl(url)
					match = re.findall('<location>(.*?)</location>', content)
					if match:
						for url in match:
							req = mechanize.Request('http://ua.canna.to/canna/single.php')
							response = mechanize.urlopen(req)
							url = 'http://ua.canna.to/canna/'+url
							req = mechanize.Request(url)
							req.add_header('User-Agent', canna_agent)
							response = mechanize.urlopen(req)
							response.close()
							code=response.info().getheader('Content-Location')
							url='http://ua.canna.to/canna/avzt/'+code
							return url

		except urllib2.HTTPError, error:
			printl(error,self,"E")
			message = self.session.open(MessageBoxExt, (_("Error: %s") % error), MessageBoxExt.TYPE_INFO, timeout=3)
			return False

		except urllib2.URLError, error:
			printl(error.reason,self,"E")
			message = self.session.open(MessageBoxExt, (_("Error: %s") % error), MessageBoxExt.TYPE_INFO, timeout=3)
			return False
示例#23
0
def isSupportedHoster(linkOrHoster, check=False):
    if not check:
        return False
    if not linkOrHoster:
        return False

    printl("check hoster: %s" % linkOrHoster, '', "S")

    host = linkOrHoster.lower().strip()
    if re.search(mp_globals.hosters[0], host):
        printl("match1: %s" % linkOrHoster, '', "H")
        return True
    elif re.search(mp_globals.hosters[1], host):
        printl("match2: %s" % linkOrHoster, '', "H")
        return True

    printl("hoster not supported", '', "H")
    return False
示例#24
0
def isSupportedHoster(linkOrHoster, check=False):
	if not check:
		return False
	if not linkOrHoster:
		return False

	printl("check hoster: %s" % linkOrHoster,'',"S")

	host = linkOrHoster.lower().strip()
	if re.search(mp_globals.hosters[0], host):
		printl("match1: %s" % linkOrHoster,'',"H")
		return True
	elif re.search(mp_globals.hosters[1], host):
		printl("match2: %s" % linkOrHoster,'',"H")
		return True

	printl("hoster not supported",'',"W")
	return False
	def print_stats(self, reset_stats=False):
		printl( "Main thread was hung %s times" % self.hang_count,self,'W')

		# Don't print useless stuff below if there are no problems
		if self.hang_count == 0:
			return

		# This could be expensive
		bad_functions_list = self.bad_functions.items()
		bad_functions_list.sort(key=lambda x: x[1], reverse=True)

		printl( "Offending functions:",self,'W')
		for func, count in bad_functions_list:
			printl( "%s %s in %s:%s" % (count, func[0], func[1], func[2]),self,'W')

		if reset_stats:
			self.reset_stats()
示例#26
0
    def print_stats(self, reset_stats=False):
        printl("Main thread was hung %s times" % self.hang_count, self, 'W')

        # Don't print useless stuff below if there are no problems
        if self.hang_count == 0:
            return

        # This could be expensive
        bad_functions_list = self.bad_functions.items()
        bad_functions_list.sort(key=lambda x: x[1], reverse=True)

        printl("Offending functions:", self, 'W')
        for func, count in bad_functions_list:
            printl("%s %s in %s:%s" % (count, func[0], func[1], func[2]), self,
                   'W')

        if reset_stats:
            self.reset_stats()
示例#27
0
	def getData(self, url, postdata=None, headers=None):

		data = None
		if not url:
			printl("No URL given", self, "E")
			return data

		if postdata:
			postdata = urlencode(postdata)
		request = Request(url, data=postdata)
		if headers:
			for header, val in headers:
				request.add_header(header, val)
		if self.referer:
			request.add_header('Referer', self.referer)
		ck = self.joinCookies()
		if ck:
			request.add_header('Cookie', ck)
		try:
			response = self.opener.open(request)
			data = response.read()
			printl("Data length: %s" % len(data), self)
		except HTTPError, error:
			printl('HTTPError: %s' % error, self, "E")
示例#28
0
 def dataErrorP(self, error):
     print "dataErrorP:"
     printl(error, self)
     self.showCoverNone()
示例#29
0
 def dataErrorP(self, error):
     print 'dataErrorP:'
     printl(error, self)
     self.showCoverNone()
 def gotPLError(self, result):
     printl(str(result), self, "E")
     self.filelist.append((_("Can't download playlist"), "", "", None))
     self.ml.setList(map(self._defaultlistleft, self.filelist))
	def update(self, content):
		# update this "constructed" playlist,
		# return wether it has actually been updated
		if self._last_content and content == self._last_content:
			self._update_tries += 1
			return False

		start_time = time()
		self._update_tries = 0
		self._last_content = content

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

		def getKey(l):
			#print l
			self._key = None
			a = l[18:].find(',')
			if a > 0:
				self._encryption_method = l[18:18+a].strip()
				if self._encryption_method == 'AES-128':
					a=l.find('URI="')
					if a > 0:
						j=l[a+5:].find('"')
						self._key_url = l[a+5:j+a+5]
						j += a+5
						a=l[j:].find('IV=')
						if a > 0:
							self._iv = l[j+a+5:].strip().decode("hex")
						url = urlparse.urljoin(self.url, self._key_url)
						opener = requests.session()
						try:
							response = opener.get(url, cookies=self._cookies, headers=self._headers, timeout=30)
							self._key = response.content
						except:
							printl("No Encryption Key-URI found",self,'E')
					else:
						printl("No Encryption Key-URI found",self,'E')
				else:
					printl("No valid Encryption Method found",self,'E')
			else:
				self._encryption_method = l[18:].strip()
			if not self._encryption_method:
				printl("No Encryption method found",self,'E')

		extinf_start = content.find('#EXTINF')
		if extinf_start < 0: content += '\n'
		self._lines = get_lines_iter(content[:extinf_start])
		try:
			first_line = self._lines.next()
		except:
			return False
		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
		i = 0
		last_bw = '-1'
		extinf_list = []
		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 'googlevideo.com' in uri:
					a = uri.find('/itag/')
					if a > 0:
						b = uri.find('/',a+6,a+10)
						itag = int(uri[a+6:b]) if b > 0 else 0
						if itag not in (91,92,93,94,95,96,132,151,300): continue
				if d['BANDWIDTH'] != last_bw:
					last_bw = d['BANDWIDTH']
					uri = urlparse.urljoin(self.url, uri)
					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'):
				i = self.media_sequence = int(l[22:])
				if self._last_f != None:
					y = content.rfind(self._last_f, extinf_start)
					if y > 0:
						y = content.find('#EXTINF', y)
						if y > 0:
							extinf_start = y
							i = self._last_sequence
						else:
							extinf_start = 0
					else:
						self._first_sequence = 0
			elif l.startswith('#EXT-X-VERSION'):
				self.version = int(l[15:])
			elif l.startswith('#EXT-X-PROGRAM-DATE-TIME'):
				#print l
				pass
			elif l.startswith('#EXT-X-BYTE-SIZE'):
				#print l
				pass
			elif l.startswith('#EXT-X-BYTERANGE'):
				#print l
				pass
			elif l.startswith('#EXT-X-DATERANGE'):
				#print l
				pass
			elif l.startswith('#EXT-X-CUEPOINT'):
				#print l
				pass
			elif l.startswith('#EXT-X-PROGRAM-DATE-TIME'):
				#print l
				pass
			elif l.startswith('#EXT-X-INDEPENDENT-SEGMENTS'):
				#print l
				pass
			elif l.startswith('#EXT-X-PLAYLIST-TYPE'):
				#print l
				pass
			elif l.startswith('#EXT-X-ALLOW-CACHE'):
				pass
			elif l.startswith('#EXT-X-KEY'):
				getKey(l)
			elif len(l.strip()) != 0:
				print l

		if extinf_start > 0:

			def proc_extinf_list(extinf_list):
				_len = len(extinf_list)
				a = _len - 5 if not self._endlist and _len >= 5 else 0
				for item in extinf_list[a:]:
					_i, _l, _f = item
					if _f == None:
						self._files[_i]['endlist'] = True
					else:
						if _f.startswith('./'): _f = _f[2:]
						v = _l[8:].split(',')
						d = dict(file=_f,
								title=v[1].strip(),
								duration=math.trunc(float(v[0])) if self.version < 3 else float(v[0]),
								sequence=_i)
						_i += 1
						self._set_file(_i, d)
				if _i > self._last_sequence:
					self._last_f = _f
					self._last_sequence = _i
				else: self._last_f = None

				if self._endlist:
					self._end_sequence = self._last_sequence - 1

			self._lines = get_lines_iter(content[extinf_start:])
			for l in self._lines:
				if l.startswith('#EXTINF'):
					while True:
						f = self._lines.next().strip()
						if not f.startswith('#'):
							break
					extinf_list.append((i,l,f))
					i += 1
				elif l.startswith('#EXT-X-DISCONTINUITY'):
					#print l
					pass
				elif l.startswith('#EXT-X-ENDLIST'):
					self._endlist = True
					extinf_list.append((i,None,None))
					#print l
				elif len(l.strip()) != 0:
					print l

			proc_extinf_list(extinf_list)
			del extinf_list[:]
			result = True
		elif extinf_start == 0:
			result = False
			self._update_tries += 1
		else: result = True

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

		del content
		self.load_time = time() - start_time
		return result
示例#32
0
 def errorload(self, error):
     printl('[streams]: ' + str(error), '', 'E')
     message = self.session.open(MessageBoxExt,
                                 _("Unknown error, check MP logfile."),
                                 MessageBoxExt.TYPE_INFO,
                                 timeout=5)
示例#33
0
class UrllibHelper(object):

	DEBUGLEVEL = 0

	headers = [
		('Accept', ('text/html,application/xhtml+xml,'
					'application/xml;q=0.9,*/*;q=0.8')),
		('User-Agent', ('Mozilla/5.0 (Windows NT 6.1; WOW64) '
						'AppleWebKit/537.36 (KHTML, like Gecko) '
						'Chrome/27.0.1453.15 Safari/537.36')),
		('Accept-Language', 'de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4')
	]

	handlers = [
		urllib2.HTTPHandler(debuglevel=DEBUGLEVEL),
		#urllib2.HTTPCookieProcessor(cj)
    ]

	def __init__(self, referrer=None):
		self.referer = referrer
		self.cookie = Cookie.SimpleCookie()
		self.rcookie = None
		self.opener = urllib2.build_opener(*self.handlers)
		self.opener.addheaders = self.headers

	def setCookie(self, name, value, expires=None, path=None, domain=None, secure=None):
		self.cookie[name] = value
		if expires:
			expiration = datetime.datetime.now() + datetime.timedelta(minutes=int(expires))
			self.cookie[name]['expires'] = expiration.strftime("%a, %d-%b-%Y %H:%M:%S")
		if path:
			self.cookie[name]['path'] = path
		if domain:
			self.cookie[name]['domain'] = domain
		if secure:
			self.cookie[name]['secure'] = secure

	def joinCookies(self):
		ck = ''
		if self.rcookie:
			ck = self.rcookie[0]
			for s in self.rcookie[1:]:
				ck += '\r\n%s' % s
		if self.cookie:
			if ck:
				ck += '\r\n'
			ck += self.cookie.output(header='').strip()
		return ck

	def getData(self, url, postdata=None, headers=None):

		data = None
		if not url:
			printl("No URL given", self, "E")
			return data

		if postdata:
			postdata = urlencode(postdata)
		request = Request(url, data=postdata)
		if headers:
			for header, val in headers:
				request.add_header(header, val)
		if self.referer:
			request.add_header('Referer', self.referer)
		ck = self.joinCookies()
		if ck:
			request.add_header('Cookie', ck)
		try:
			response = self.opener.open(request)
			data = response.read()
			printl("Data length: %s" % len(data), self)
		except HTTPError, error:
			printl('HTTPError: %s' % error, self, "E")
		except URLError, error:
			printl('URLError: %s' % error, self, "E")
 def importFini(self, msg, session, err=False):
     self["F4"].setText(_("EPG import finished"))
     printl(str(msg), self)
     msg = msg.replace("[MP EPG] ", "")
     session.open(MessageBoxExt, str(msg), type=MessageBoxExt.TYPE_INFO)
示例#35
0
	def dataErrorP(self, error):
		print "dataErrorP:"
		printl(error,self,"E")
		self.showCoverNone()
示例#36
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
 def mh_dataError(self, error):
     self.d_print("mh_dataError:")
     printl(error, self, "E")
示例#38
0
    def loadM3UList(self, m3ufile):
        self._stopUpdateLoop()
        self.ltype = 'sl_m3ulist'
        self.m3u_file = m3ufile
        self.m3u_title = m3ufile.split('/')[-1]
        ctitle = self.m3u_title + " (\"{0}/\")".format("/".join(
            m3ufile.split('/')[0:-1]))
        self['ContentTitle'].setText(ctitle)
        self['F3'].hide()
        self.filelist = []
        self.group_set.clear()
        self.group_list = [(_('Show all channels'), 'all')]
        self.sp_option = "MP3"
        self.logo_base = ""
        extm3u = extinf = tvg_infos = self.logos = False
        title = path = logo = ""
        tvg_data = None
        do_append = True
        first_line = None
        proxy_num = None
        self.do_update = False
        try:
            with open(m3ufile, "rU") as inf:
                for line in inf:
                    line = line.strip()
                    if not extm3u and '#EXTM3U' in line:
                        first_line = line
                        extm3u = True
                        if '$' in line:
                            options = line.split(',')[1:]
                            for option in options:
                                mode, attr = option.split('=')
                                if mode == '$MODE':
                                    self.sp_option = attr
                                elif mode == '$LOGOBASE':
                                    self.logo_base = attr
                                elif mode == '$PROXY':
                                    proxy_num = attr
                    elif extm3u and not extinf and line.startswith('#EXTINF:'):
                        m = re.search('\s*[-]*\d+(,|\s)(.+)', line[8:])
                        if m:
                            extinf = True
                            ls = m.group(2).split(',')
                            title = ls[-1]
                            tvg = ls[0] if len(ls) > 1 else ''
                            if 'tvg-id=' in tvg:
                                tid = re.search('tvg-id="(.*?)"',
                                                tvg).group(1).replace(
                                                    '.ink',
                                                    '.nix').replace('_', '.')
                                cid = tvg_conv.get(tid)
                                if cid: tid = tid.replace(tid, cid)
                                epg_id = format(
                                    hash(tid.lower()) & sys.maxint, 'x')
                                self.enableThumbs = True
                                if 'tvg-logo=' in tvg:
                                    logo = self.logo_base + re.search(
                                        'tvg-logo="(.*?)"', tvg).group(1)
                                if 'group-title=' in tvg:
                                    grp = re.search('group-title="(.*?)"',
                                                    tvg).group(1)
                                    self.do_update = True
                                    tvg_data = (
                                        'TVG',
                                        'tvg-id',
                                    ) + (epg_id, grp, logo)
                                    k = grp.lower()
                                    if k != self.group_stype and 'all' != self.group_stype:
                                        do_append = False

                                    if not k in self.group_set:
                                        self.group_set.add(k)
                                        self.group_list.append(
                                            (_("Show \"{0}\" channels").format(
                                                grp.title()), k))
                                    tvg_infos = True
                                    m2 = re.search('\[COLOR\s(.*?)\]', title)
                                    tvg_data += (colors.get(
                                        m2.group(1), DEFAULT_COLOR),
                                                 ) if m2 else (DEFAULT_COLOR, )
                                    title = re.sub('(\[/*.*?\])', '', title)
                                else:  # Senderliste
                                    self.do_update = True
                                    tvg_data = ('TVG-LIST',
                                                'tvg-id') + (epg_id, logo)
                                    if logo: logo = self.logo_base + logo
                                    m2 = re.search('\[COLOR\s(.*?)\]', title)
                                    tvg_data += (colors.get(
                                        m2.group(1), DEFAULT_COLOR),
                                                 ) if m2 else (DEFAULT_COLOR, )
                                    title = re.sub('(\[/*.*?\])', '', title)
                                    k = title.lower()
                                    if k != self.group_stype and 'all' != self.group_stype:
                                        do_append = False
                            elif 'group-title=' in tvg:  # other tvg pl typ
                                k = re.search('group-title="(.*?)"',
                                              tvg).group(1).lower()
                                if k != self.group_stype and 'all' != self.group_stype:
                                    do_append = False

                                if not k in self.group_set:
                                    self.group_set.add(k)
                                    self.group_list.append(
                                        (_("Show \"{0}\" channels").format(
                                            k.title()), k))
                                tvg_infos = True
                                m2 = re.search('\[COLOR\s(.*?)\]', title)
                                tvg_data = (colors.get(
                                    m2.group(1), DEFAULT_COLOR), ) if m2 else (
                                        DEFAULT_COLOR, )
                                title = re.sub('(\[/*.*?\])', '', title)
                            else:
                                title = re.sub('(\[/*.*?\])', '', title)
                                tvg_data = (DEFAULT_COLOR, )
                        else:
                            title = path = logo = ""
                            extinf = False
                            tvg_data = None
                            do_append = True
                            continue

                    elif extm3u and extinf and line:
                        if not config_mp.mediaportal.hlsp_enable.value:
                            path = line.replace('|', '#')
                        else:
                            path = line
                        if path.endswith('#m3u#'):
                            path = path.replace('#m3u#', '')
                            options = first_line.strip()
                            return self.getPL(path, m3ufile, options)

                    if extinf and path:
                        if do_append:
                            if logo:
                                self.logos = True
                                if not logo[:4] in ('http', 'file'):
                                    logo_base = TVG_LOGO_BASE if not 'iptv.ink' in path else TVG_INK_LOGO_BASE
                                    logo = logo_base + logo
                            event = ""
                            if proxy_num:
                                ps = path.rsplit('|X-For', 1)
                                if proxy_num == '1':
                                    path = ps[0] + ' PROXY'
                                elif proxy_num == '2' and len(ps) > 1:
                                    path = ps[0] + ' PROXY'
                            self.filelist.append(
                                (title, path, logo, tvg_data, event))
                        title = path = logo = ""
                        extinf = False
                        tvg_data = None
                        do_append = True

        except IOError, e:
            printl(e, self, 'E')
示例#39
0
				request.add_header(header, val)
		if self.referer:
			request.add_header('Referer', self.referer)
		ck = self.joinCookies()
		if ck:
			request.add_header('Cookie', ck)
		try:
			response = self.opener.open(request)
			data = response.read()
			printl("Data length: %s" % len(data), self)
		except HTTPError, error:
			printl('HTTPError: %s' % error, self, "E")
		except URLError, error:
			printl('URLError: %s' % error, self, "E")
		except ValueError, error:
			printl('ValueError: %s' % error, self, "E")
		except:
			printl('Unknown error', self, "E")
		else:
			self.referer = url
			self.rcookie = response.info().getheaders('Set-Cookie')

		return data

class Flashx(UrllibHelper):

	def __init__(self):
		UrllibHelper.__init__(self)

	def __c(self, c):
		d = ""
示例#40
0
 def importFini(self, msg, session, err=False):
     self['F4'].setText(_('EPG import finished'))
     printl(str(msg), self)
     msg = msg.replace('[MP EPG] ', '')
     session.open(MessageBoxExt, str(msg), type=MessageBoxExt.TYPE_INFO)
	def dataErrorP(self, error):
		printl(error,self,'E')
		self.showCoverNone()
示例#42
0
 def gotPLError(self, result):
     printl(str(result), self, 'E')
     self.filelist.append((_("Can't download playlist"), "", "", None))
     self.ml.setList(map(self._defaultlistleft, self.filelist))
示例#43
0
	def dataError(self, error):
		from debuglog import printlog as printl
		printl(error,self,"E")
示例#44
0
	def errorload(self, error):
		printl('[streams]: ' + str(error),'','E')
		message = self.session.open(MessageBoxExt, _("Broken URL parsing, please report to the developers."), MessageBoxExt.TYPE_INFO, timeout=5)
示例#45
0
    def sendSegm(self, s_cache_item=None, r_range=None):
        self.playerReady = endlist = False
        while True:
            if s_cache_item != None:
                filepath = s_cache_item['filepath']
            else:
                filepath = ""

            if not self._request or (self._streamqueue.empty()
                                     and not s_cache_item):
                break
            if not filepath:
                filepath, endlist = self._streamqueue.get()
                if endlist:
                    self._request.setResponseCode(200)
                    self.set_seek_headers = True
            else:
                endlist = False
            try:
                size = os.path.getsize(filepath)
            except IOError, e:
                printl('File i/o error:' + str(e), self, 'E')
                raise Exception(e)
                break
            else:
                with open(filepath, 'rb') as f:
                    count = 0
                    if self.set_seek_headers:
                        if r_range == None:
                            bytes_range = self.bytes_sent
                        else:
                            bytes_range = r_range
                        self._request.setHeader(
                            'Content-Range', 'bytes=%d-%d/*' %
                            (bytes_range, bytes_range + size - 1))
                        if endlist:
                            self.content_size = str(bytes_range + size)
                        self._clean_seek_headers = True
                    elif self._clean_seek_headers:
                        self._request.removeHeader('Content-Range')
                        self._request.setResponseCode(200)
                        self._clean_seek_headers = False

                    self._request.write(f.read())
                    count = size

                if s_cache_item == None:
                    self._proxy_infos['seek_cache'].append({})
                    self._proxy_infos['seek_cache'][-1][
                        'bytes_range'] = self.bytes_sent
                    if len(self._proxy_infos['seek_cache']) > 2:
                        del self._proxy_infos['seek_cache'][0]
                    self.bytes_sent += size
                    self._proxy_infos['seek_cache'][-1]['bytes_size'] = size
                    self._proxy_infos['seek_cache'][-1]['filepath'] = filepath
                    self._on_about_to_finish()
                else:
                    s_cache_item = None

                if self._seeking and not endlist:
                    self.play()
    def loadM3UList(self, m3ufile):
        self._stopUpdateLoop()
        self.ltype = "sl_m3ulist"
        self.m3u_file = m3ufile
        self.m3u_title = m3ufile.split("/")[-1]
        ctitle = self.m3u_title + ' ("{0}/")'.format("/".join(m3ufile.split("/")[0:-1]))
        self["ContentTitle"].setText(ctitle)
        self["F3"].hide()
        self.filelist = []
        self.group_set.clear()
        self.group_list = [(_("Show all channels"), "all")]
        self.sp_option = "MP3"
        self.logo_base = ""
        extm3u = extinf = tvg_infos = self.logos = False
        title = path = logo = ""
        tvg_data = None
        do_append = True
        first_line = None
        proxy_num = None
        self.do_update = False
        try:
            with open(m3ufile, "rU") as inf:
                for line in inf:
                    line = line.strip()
                    if not extm3u and "#EXTM3U" in line:
                        first_line = line
                        extm3u = True
                        if "$" in line:
                            options = line.split(",")[1:]
                            for option in options:
                                mode, attr = option.split("=")
                                if mode == "$MODE":
                                    self.sp_option = attr
                                elif mode == "$LOGOBASE":
                                    self.logo_base = attr
                                elif mode == "$PROXY":
                                    proxy_num = attr
                    elif extm3u and not extinf and line.startswith("#EXTINF:"):
                        m = re.search("\s*[-]*\d+(,|\s)(.+)", line[8:])
                        if m:
                            extinf = True
                            ls = m.group(2).split(",")
                            title = ls[-1]
                            tvg = ls[0] if len(ls) > 1 else ""
                            if "tvg-id=" in tvg:
                                tid = (
                                    re.search('tvg-id="(.*?)"', tvg).group(1).replace(".ink", ".nix").replace("_", ".")
                                )
                                cid = tvg_conv.get(tid)
                                if cid:
                                    tid = tid.replace(tid, cid)
                                epg_id = format(hash(tid.lower()) & sys.maxint, "x")
                                self.enableThumbs = True
                                if "tvg-logo=" in tvg:
                                    logo = self.logo_base + re.search('tvg-logo="(.*?)"', tvg).group(1)
                                if "group-title=" in tvg:
                                    grp = re.search('group-title="(.*?)"', tvg).group(1)
                                    self.do_update = True
                                    tvg_data = ("TVG", "tvg-id") + (epg_id, grp, logo)
                                    k = grp.lower()
                                    if k != self.group_stype and "all" != self.group_stype:
                                        do_append = False

                                    if not k in self.group_set:
                                        self.group_set.add(k)
                                        self.group_list.append((_('Show "{0}" channels').format(grp.title()), k))
                                    tvg_infos = True
                                    m2 = re.search("\[COLOR\s(.*?)\]", title)
                                    tvg_data += (colors.get(m2.group(1), DEFAULT_COLOR),) if m2 else (DEFAULT_COLOR,)
                                    title = re.sub("(\[/*.*?\])", "", title)
                                else:  # Senderliste
                                    self.do_update = True
                                    tvg_data = ("TVG-LIST", "tvg-id") + (epg_id, logo)
                                    if logo:
                                        logo = self.logo_base + logo
                                    m2 = re.search("\[COLOR\s(.*?)\]", title)
                                    tvg_data += (colors.get(m2.group(1), DEFAULT_COLOR),) if m2 else (DEFAULT_COLOR,)
                                    title = re.sub("(\[/*.*?\])", "", title)
                                    k = title.lower()
                                    if k != self.group_stype and "all" != self.group_stype:
                                        do_append = False
                            elif "group-title=" in tvg:  # other tvg pl typ
                                k = re.search('group-title="(.*?)"', tvg).group(1).lower()
                                if k != self.group_stype and "all" != self.group_stype:
                                    do_append = False

                                if not k in self.group_set:
                                    self.group_set.add(k)
                                    self.group_list.append((_('Show "{0}" channels').format(k.title()), k))
                                tvg_infos = True
                                m2 = re.search("\[COLOR\s(.*?)\]", title)
                                tvg_data = (colors.get(m2.group(1), DEFAULT_COLOR),) if m2 else (DEFAULT_COLOR,)
                                title = re.sub("(\[/*.*?\])", "", title)
                            else:
                                title = re.sub("(\[/*.*?\])", "", title)
                                tvg_data = (DEFAULT_COLOR,)
                        else:
                            title = path = logo = ""
                            extinf = False
                            tvg_data = None
                            do_append = True
                            continue

                    elif extm3u and extinf and line:
                        if not config.mediaportal.use_hls_proxy.value:
                            path = line.replace("|", "#")
                        else:
                            path = line
                        if path.endswith("#m3u#"):
                            path = path.replace("#m3u#", "")
                            options = first_line.strip()
                            return self.getPL(path, m3ufile, options)

                    if extinf and path:
                        if do_append:
                            if logo:
                                self.logos = True
                                if not logo[:4] in ("http", "file"):
                                    logo_base = TVG_LOGO_BASE if not "iptv.ink" in path else TVG_INK_LOGO_BASE
                                    logo = logo_base + logo
                            event = ""
                            if proxy_num:
                                ps = path.rsplit("|X-For", 1)
                                if proxy_num == "1":
                                    path = ps[0] + " PROXY"
                                elif proxy_num == "2" and len(ps) > 1:
                                    path = ps[0] + " PROXY"
                            self.filelist.append((title, path, logo, tvg_data, event))
                        title = path = logo = ""
                        extinf = False
                        tvg_data = None
                        do_append = True

        except IOError, e:
            printl(e, self, "E")
示例#47
0
 def dataError(self, error):
     from debuglog import printlog as printl
     printl(error, self, "E")
	def mh_dataError(self, error):
		self.d_print("mh_dataError:")
		printl(error,self,"E")