Пример #1
0
    def convert_subtitles(self, subtitles, output):
        subtitle_data = subtitles
        subtitle_data = subtitle_data.replace("\n","").replace("\r","")
        subtitle_data = BeautifulStoneSoup(subtitle_data, convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
        subtitle_array = []
        srt_output = ''

        print "HULU: --> Converting subtitles to SRT"
        heading = 'Subtitles'
        message = 'Converting subtitles'
        duration = 4000
        #xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' % ( heading, message, duration) )
        lines = subtitle_data.findAll('sync') #split the file into lines
        for line in lines:
            if(line['encrypted'] == 'true'):
                sub = self.decrypt_subs(line.string)
                sub = self.clean_subs(sub)
                sub = unicode(BeautifulStoneSoup(sub,convertEntities=BeautifulStoneSoup.HTML_ENTITIES).contents[0]).encode( "utf-8" )

            else:
                sub = unicode(BeautifulStoneSoup(sub,convertEntities=BeautifulStoneSoup.HTML_ENTITIES).contents[0]).encode( "utf-8" )

            begin_time = int(line['start'])
            seconds = int(math.floor(begin_time/1000))
            milliseconds = int(begin_time - (seconds * 1000))
            timestamp = self.convert_time(seconds)
            timestamp = "%s,%03d" % (timestamp, milliseconds)

            index = len(subtitle_array)-1
            if(index > -1 and subtitle_array[index]['end'] == None):
                millsplit = subtitle_array[index]['start'].split(',')
                itime = millsplit[0].split(':')
                start_seconds = (int(itime[0])*60*60)+(int(itime[1])*60)+int(itime[2])
                end_seconds = start_seconds + 4
                if end_seconds < seconds:
                    endmilliseconds = int(millsplit[1])
                    endtimestamp = self.convert_time(end_seconds)
                    endtimestamp = "%s,%03d" % (endtimestamp, endmilliseconds)
                    subtitle_array[index]['end'] = endtimestamp
                else:
                    subtitle_array[index]['end'] = timestamp

            if sub != '&#160; ':
                sub = sub.replace('&#160;', ' ')
                temp_dict = {'start':timestamp, 'end':None, 'text':sub}
                subtitle_array.append(temp_dict)

        for i, subtitle in enumerate(subtitle_array):
            line = str(i+1)+"\n"+str(subtitle['start'])+" --> "+str(subtitle['end'])+"\n"+str(subtitle['text'])+"\n\n"
            srt_output += line

        file = open(os.path.join(os.getcwd().replace(';', ''),'resources','cache',output+'.srt'), 'w')
        file.write(srt_output)
        file.close()
        print "HULU: --> Successfully converted subtitles to SRT"
        heading = 'Subtitles'
        message = 'Conversion Complete'
        duration = 4000
        #xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' % ( heading, message, duration) )
        return True
Пример #2
0
 def checkCaptions(self, pid):
     url = 'http://www.hulu.com/captions?content_id='+pid
     html = ba.FetchUrl(url)
     capSoup = BeautifulStoneSoup(html)
     hasSubs = capSoup.find('en')
     heading = 'Subtitles'
     if(hasSubs):
         print "HULU --> Grabbing subtitles..."
         message = 'Grabbing subtitles...'
         duration = 4000
         #xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' % ( heading, message, duration) )
         html=ba.FetchUrl(hasSubs.string)
         ok = self.convert_subtitles(html,pid)
         if ok:
             print "HULU --> Subtitles enabled."
         else:
             print "HULU --> There was an error grabbing the subtitles."
             message = 'Error grabbing subtitles.'
             duration = 4000
             #xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' % ( heading, message, duration) )
     else:
         print "HULU --> No subtitles available."
         message = 'No subtitles available.'
         duration = 4000
Пример #3
0
    def Play(self, stream_name, stream_id, subtitle):
        #path = self.tinyurl(stream_id)
        #play = ba.CreatePlay()
        #play.SetPath(quote_plus(path))
        #play.SetDomain('bartsidee.nl')
        #play.SetJSactions(quote_plus('http://bartsidee.nl/boxee/apps/js/hulu.js'))

        #grab eid from failsafe url
        html = ba.FetchUrl(stream_id)
        p=re.compile('content_id", (.+?)\);')
        ecid=p.findall(html)[0]
        pid = ecid

        if subtitle: self.checkCaptions(pid)

        #getSMIL
        try:
            #smilURL = "http://s.hulu.com/select.ashx?pid=" + pid + "&auth=" + self.pid_auth(pid) + "&v=713434170&np=1&pp=hulu&dp_id=hulu&cb=499"
            smilURL = 'http://s.hulu.com/select?video_id=' + pid + '&v=850037518&ts=1294866343&np=1&vp=1&pp=hulu&dp_id=hulu&bcs=' + self.content_sig(pid)
            print 'HULU --> SMILURL: ' + smilURL
            smilXML = ba.FetchUrl(smilURL)
            tmp = self.decrypt_SMIL(smilXML)
            smilSoup = BeautifulStoneSoup(tmp, convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
            print smilSoup.prettify()
        except:
            print "error retreiving smil"
            return

        #getRTMP
        video=smilSoup.findAll('video')
        streams=[]
        selectedStream = None
        cdn = None
        qtypes=['ask', 'p011', 'p010', 'p008', 'H264 Medium', 'H264 650K', 'H264 400K', 'High', 'Medium','Low']
        #label streams
        qt = 4
        if qt < 0 or qt > 9: qt = 0
        while qt < 9:
            qtext = qtypes[qt]
            for vid in video:
                #if qt == 0:
                streams.append([vid['profile'],vid['cdn'],vid['server'],vid['stream'],vid['token']])
                if qt > 6 and 'H264' in vid['profile']: continue
                if qtext in vid['profile']:
                    if vid['cdn'] == 'akamai':
                        selectedStream = [vid['server'],vid['stream'],vid['token']]
                        print selectedStream
                        cdn = vid['cdn']
                        break

            if qt == 0 or selectedStream != None: break
            qt += 1

        if selectedStream != None:
            #form proper streaming url
            server = selectedStream[0]
            stream = selectedStream[1]
            token = selectedStream[2]

            protocolSplit = server.split("://")
            pathSplit = protocolSplit[1].split("/")
            hostname = pathSplit[0]
            appName = protocolSplit[1].split(hostname + "/")[1]

            if "level3" in cdn:
                appName += "?" + token
                stream = stream[0:len(stream)-4]
                newUrl = server + " app=" + appName

            elif "limelight" in cdn:
                appName += '?' + token
                stream = stream[0:len(stream)-4]
                newUrl = server + '?' + token + " app=" + appName

            elif "akamai" in cdn:
                appName += '?' + token
                newUrl = server + '?' + token
                #newUrl = server + "?_fcs_vhost=" + hostname + "&" + token

            else:
                #xbmcgui.Dialog().ok('Unsupported Content Delivery Network',cdn+' is unsupported at this time')
                return

            print "item url -- > " + newUrl
            print "app name -- > " + appName
            print "playPath -- > " + stream

            #define item
            SWFPlayer = 'http://www.hulu.com/site-player/86070/player.swf'
            #newUrl += " playpath=" + stream + " swfurl=" + SWFPlayer

            #newUrl += " swfvfy=true"
            playPath = stream
            rtmpURL = newUrl
            authPath = ''

            play = ba.CreatePlay()
            play.SetContent_type('video/x-flv')
            play.SetRTMPPath(playPath)
            play.SetRTMPDomain(rtmpURL)
            play.SetRTMPAuth(authPath)
            play.SetRTMPSwf(SWFPlayer)
            print "finish"
            return play
Пример #4
0
class JsonService( object ):
	
	# earthengine_...
	
	def earthengine_getyears( self, opt ):
		EarthEngine( current_handler )
		
		poly = ee.Geometry.Rectangle(-69.9365234375,-11.6928965625,-69.8161621094,-11.4097965708)
		collection = ee.ImageCollection(opt['sat'][1]).filterBounds(poly)
		newCollection = collection.map(lambda img: ee.Feature(poly).set({'date':img.get('system:time_start')}))
		response = newCollection.getInfo()

		if 'error' in response:
			return response
		
		years = {}
		ymd = {}
		for image in response['features']:
			seconds_since_epoch = image['properties']['date']
			date = datetime.datetime.fromtimestamp(int(seconds_since_epoch / 1000)).strftime('%Y-%m-%d') 
			logging.info(date)
			year = int( date.split('-')[0] )
			years[year] = True
			ymd[date] = True
		
		ymd = list(ymd)
		ymd.sort()
		
		years = list(years)
		years.sort()
		return {
			'years': years,
			'ymd': ymd
		}
	
	def make_mosaic( self, opt):
		EarthEngine( current_handler )
		collection = ee.ImageCollection(opt['sat'][1])
		vcf = ee.Image('MOD44B_C4_TREE_2000')
		elevation = ee.Image('srtm90_v4')
		sensor = opt['sat'][0]
		starttime = opt['times'][0]['starttime']
		endtime = opt['times'][0]['endtime']
		#polygon = [ polyBbox( opt['bbox'] ) ]
		polygon = ee.Geometry.Rectangle(-69.9365234375,-11.6928965625,-69.8161621094,-11.4097965708)
		call = CLASLITE + 'MosaicScene'
		mosaic = ee.call(call,
			collection, 
			vcf, 
			elevation,
			sensor,
			starttime,
			endtime, 
			polygon)
		return mosaic

	#def make_mosaic(self, opt):
	#	EarthEngine(current_handler)
	#	image = ee.Image('srtm90_v4')
	#	return image

	def earthengine_map( self, action, opt):
		mosaic = self.make_mosaic(opt)
		result = _get_raw_mapid(mosaic.getMapId({'bands':'vis-red,vis-green,vis-blue'}))
		logging.info('mosaic')
		logging.info(result)
		return {'tiles':result}

		#polygon = [ polyBbox( opt['bbox'] ) ]
		#palette = opt.get('palette')
		#mode = opt['mode']
		#mosaic = self.make_mosaic(opt)
		#return _get_raw_mapid(mosaic.getMapId({
		#	'bands': 'vis-red,vis-green,vis-blue'
		#}))
			
		
	def old_earthengine_map( self, action, opt ):
		'''	Create an Earth Engine map as tiles or as a download image,
			or requests statistics.
			action = 'click', 'download', 'stats', or 'tiles'
			opt = {
				'sat': [ satname, sensor ],
				'mode': 'fractcover' or 'forestcover' or 'forestchange',
				'times': [
					{ 'starttime': n, 'endtime': n },
					/* ... */
				],
				# for tiles or download:
				'bbox': [ west, south, east, north ],
				'crs': 'EPSG:nnnn',
				# for click:
				'points': [ lng, lat ],
				# for fractcover:
				'bias': n,
				'gain': n,
				'gamma': n,
				# for forestcover:
				# (nothing extra)
				# for forestchange:
				'type': 'deforestation' or 'disturbance',
				# for forestcover or forestchange:
				'palette': [ rrggbb, ..., rrggbb ],
			}
		'''
		polygon = [ polyBbox( opt['bbox'] ) ]
		palette = opt.get('palette')
		mode = opt['mode']
		final = None
		bandsDown = None
		if mode == 'fractcover':
			bands = 'sub,pv,npv'
			bandsDown = 'sub,pv,npv,sdev_sub,sdev_pv,sdev_npv,rms'
			visual = 'bias=%f&gain=%f&gamma=%f' %(
				float(opt['bias']), float(opt['gain']), float(opt['gamma'])
			)
		elif mode == 'forestcover':
			final = 'ForestCoverMap'
			bands = 'Forest_NonForest'
			visual = 'min=0&max=2&palette=%s' %(
				str( ','.join(palette) )
			)
			statFields = 'count'
		elif mode == 'forestchange':
			final = 'ForestCoverChange'
			bands = ( 'disturb', 'deforest' )[ opt['type'] == 'deforestation' ]
			visual = 'min=1&max=%d&palette=%s' %(
				len(palette), str( ','.join(palette) )
			)
			statFields = 'DEFORESTATION_PIX_CNT,DISTURBANCE_PIX_CNT,TOTAL_PIX_CNT'
		if action == 'download':
			( w, s, e, n ) = opt['bbox']
			coords = [
				[ w, s ],
				[ w, n ],
				[ e, n ],
				[ e, s ],
			]
			region = json_encode({
				"type": "LinearRing",
				"coordinates": coords,
			})
			bands = map(
				lambda band: { 'id': band, 'scale': 30 },
				( bandsDown or bands ).split(',')
			)
			bands = '%s&crs=%s&region=%s' %(
				json_encode(bands),
				opt['crs'],
				region
			)
		
		ee = EarthEngine( current_handler )
		ei =  EarthImage()
		modImage = ei.obj( 'Image', 'MOD44B_C4_TREE_2000' )
		tempImage = ei.obj( 'Image', 'LANDSAT/L7_L1T/LE72290682008245EDC00' )
		collection = ei.obj( 'ImageCollection', opt['sat'][1] )
		sensor = opt['sat'][0]
		
		image = []
		for time in opt['times']:
			image.append( ei.step(
				CLASLITE+'MosaicScene',
				collection, modImage, sensor,
				time['starttime'], time['endtime'],
				[ polygon ]
			) )
		if len(image) == 1:
			image = image[0]
		
		if final:
			image = ei.step( CLASLITE+final, image )
		#image = ei.clip( image )
		
		if action == 'stats':
			image = ei.step( CLASLITE+final+'Stat', image, {
				"features": [
					{
						"type": "Feature",
						"geometry": {
							"type": "Polygon",
							"coordinates": polygon
						}
					}
				]
			})
		
		params = 'image=%s' % json_encode(image)
		
		if action == 'click':
			params += '&points=[[%s,%s]]' %( opt['lng'], opt['lat'] )
		elif action == 'stats':
			params += '&fields=%s' % statFields
		else:
			params += '&bands=%s' %( bands )
		
		def vp( p ): return visual + '&' + p
		
		if action == 'click':
			value = ee.get( 'value', params )
			return value
		elif action == 'download':
			download = ee.post( 'download', vp(params) )
			return download
		elif action == 'tiles':
			tiles = ee.post( 'mapid', vp(params) )
			if 'error' in tiles:
				return tiles
			else:
				return { 'tiles': tiles['data'] }
		elif action == 'stats':
			stats = ee.post( 'value', params )
			if 'error' in stats:
				return stats
			else:
				return { 'stats': stats['data']['properties'] }
	
	# project_...
	
	def project_delete( self, keytexts ):
		for keytext in keytexts:
			key = db.Key( keytext )
			project = db.get( key )
			if project.owner != current_handler.auth.user.auth_id:
				return {
					'error': 'Wrong owner'
				}
			db.delete( project )
		return {
			'deleted': keytexts
		}
	
	def project_get( self, keytext ):
		key = db.Key( keytext )
		project = db.get( key )
		if project.owner != current_handler.auth.user.auth_id:
			return {
				'error': 'Wrong owner'
			}
		return {
			'key': keytext,
			'name': project.name,
			'settings': project.settings
		}
	
	def project_list( self ):
		owner = current_handler.auth.user.auth_id
		projects = Project.gql(
			'WHERE owner = :1 ORDER BY name',
			owner
		)
		result = []
		for project in projects:
			result.append({
				'key': str( project.key() ),
				'name': project.name
			})
		return {
			'projects': result,
		}
	
	def project_new( self, name, settings ):
		owner = current_handler.auth.user.auth_id
		project = Project( name=name, owner=owner, settings=settings )
		project.put()
		return {
			'key': str( project.key() )
		}
	
	# shape_...
	
	def shape_list_tables( self ):
		client = fusion.Client( current_handler )
		tables = filter(
			lambda row: row['name'].endswith(('.kml','.shp')),
			client.query( 'SHOW TABLES' )
		)
		return {
			'tables': tables
		}
	
	def shape_describe( self, idTable ):
		client = fusion.Client( current_handler )
		cols = client.query( 'DESCRIBE ' + fusion.fixId(idTable) )
		return {
			'cols': cols
		}
	
	def shape_list_rows( self, idTable ):
		client = fusion.Client( current_handler )
		cols = client.query( 'DESCRIBE ' + fusion.fixId(idTable) )
		if not fusion.hasCol( cols, 'geometry' ):
			return { 'error': 'No geometry column' }
		rows = client.query( 'SELECT rowid FROM ' + fusion.fixId(idTable) )
		return {
			'rows': rows
		}
	
	# other
	
	def fetch_content( self, path ):
		try:
			response = urlfetch.fetch( 'http://claslite.ciw.edu/%s/.atom' % path )
		except urlfetch.DownloadError, e:
			#logging.exception( e )
			response = { 'status_code': 500 }
		if response.status_code != 200:
			return { 'error': response.status_code }
		strainer = SoupStrainer( 'content' )
		soup = BeautifulStoneSoup( response.content, parseOnlyThese=strainer )
		content = soup.content
		if content is None:
			return { 'error': 404 }
		return { 'content': '<div>%s</div>' % content.text }