예제 #1
0
	def _listen(self, task_id):
		# task_ids start at zero, so we gobble up ports starting at the base port and work up
		port_no = int(config.get("api_base_port")) + task_id
		
		# Log according to configured directory and port # we're operating on
		log_file = "%s/api%s.log" % (config.get("log_dir"), port_no)
		if config.test_mode and os.path.exists(log_file):
			os.remove(log_file)
		log.init(log_file, config.get("log_level"))
		log.debug("start", "Server booting, port %s." % port_no)
		db.open()
		
		# Fire ze missiles!
		app = tornado.web.Application(request_classes)
		http_server = tornado.httpserver.HTTPServer(app, xheaders = True)
		http_server.listen(port_no)
		for request in request_classes:
			log.debug("start", "   Handler: %s" % str(request))
		for sid in config.station_ids:
			cache.update_local_cache_for_sid(sid)
		log.info("start", "Server bootstrapped and ready to go.")
		self.ioloop = tornado.ioloop.IOLoop.instance()
		self.ioloop.start()
		http_server.stop()
		log.info("stop", "Server has been shutdown.")
		log.close()
예제 #2
0
def monitor():
    _common_init()

    pid = os.getpid()
    pid_file = open("%s/scanner.pid" % config.get_directory("pid_dir"), 'w')
    pid_file.write(str(pid))
    pid_file.close()

    mask = (pyinotify.IN_ATTRIB | pyinotify.IN_CREATE
            | pyinotify.IN_CLOSE_WRITE | pyinotify.IN_DELETE
            | pyinotify.IN_MOVED_TO | pyinotify.IN_MOVED_FROM
            | pyinotify.IN_MOVE_SELF | pyinotify.IN_EXCL_UNLINK)

    try:
        go = True
        while go:
            try:
                log.info("scan", "File monitor started.")
                wm = pyinotify.WatchManager()
                wm.add_watch(str(config.get("monitor_dir")), mask, rec=True)
                pyinotify.Notifier(wm, FileEventHandler()).loop()
                go = False
            except NewDirectoryException:
                log.debug("scan", "New directory added, restarting watch.")
            except DeletedDirectoryException:
                log.debug("scan", "Directory was deleted, restarting watch.")
            finally:
                try:
                    wm.close()
                except:
                    pass
    finally:
        log.info("scan", "File monitor shutdown.")
예제 #3
0
    def completed_info(self, log_path):
        log.info(
            "\n======================================================\nTest Completed\n==============================="
        )

        print 'test completed'
        print '--------------------------'

        test_details = dict(status=self.status, id=self.id, name=self.name)

        frame = inspect.stack()[1]
        module = inspect.getmodule(frame[0])
        destination_file = os.path.basename(
            os.path.splitext(module.__file__)[0])

        destination_file = "test_id_" + str(self.id) + "_" + str(
            self.msg) + "_" + destination_file

        log_copy_file = os.path.join(log_path, destination_file)

        print log_copy_file

        if not os.path.exists(log_path):
            os.makedirs(log_path)

        src = log.LOG_NAME

        copyfile(src, log_copy_file)

        with open(src, "w"):
            pass

        return test_details
예제 #4
0
def get_random_song_timed(sid, target_seconds = None, target_delta = None):
	"""
	Fetch a random song abiding by all election block, request block, and
	availability rules, but giving priority to the target song length
	provided.  Falls back to get_random_song on failure.
	"""
	if not target_seconds:
		return get_random_song(sid)
	if not target_delta:
		target_delta = config.get_station(sid, "song_lookup_length_delta")

	sql_query = ("FROM r4_song_sid "
					"JOIN r4_songs USING (song_id) "
					"JOIN r4_album_sid ON (r4_album_sid.album_id = r4_songs.album_id AND r4_album_sid.sid = r4_song_sid.sid) "
				"WHERE r4_song_sid.sid = %s "
					"AND song_exists = TRUE "
					"AND song_cool = FALSE "
					"AND song_elec_blocked = FALSE "
					"AND album_requests_pending IS NULL "
					"AND song_request_only = FALSE "
					"AND song_length >= %s AND song_length <= %s")
	lower_target_bound = target_seconds - (target_delta / 2)
	upper_target_bound = target_seconds + (target_delta / 2)
	num_available = db.c.fetch_var("SELECT COUNT(r4_song_sid.song_id) " + sql_query, (sid, lower_target_bound, upper_target_bound))
	log.info("song_select", "Song pool size (cooldown, blocks, requests, timed) [target %s delta %s]: %s" % (target_seconds, target_delta, num_available))
	if num_available == 0:
		log.warn("song_select", "No songs available with target_seconds %s and target_delta %s." % (target_seconds, target_delta))
		log.debug("song_select", "Song select query: SELECT COUNT(r4_song_sid.song_id) " + sql_query % (sid, lower_target_bound, upper_target_bound))
		return get_random_song(sid)
	else:
		offset = random.randint(1, num_available) - 1
		song_id = db.c.fetch_var("SELECT r4_song_sid.song_id " + sql_query + " LIMIT 1 OFFSET %s", (sid, lower_target_bound, upper_target_bound, offset))
		return Song.load_from_id(song_id, sid)
예제 #5
0
def monitor():
	_common_init()

	pid = os.getpid()
	pid_file = open("%s/scanner.pid" % config.get_directory("pid_dir"), 'w')
	pid_file.write(str(pid))
	pid_file.close()

	mask = (
		pyinotify.IN_CREATE |
		pyinotify.IN_CLOSE_WRITE |
		pyinotify.IN_DELETE |
		pyinotify.IN_MOVED_TO |
		pyinotify.IN_MOVED_FROM |
		pyinotify.IN_EXCL_UNLINK
	)

	try:
		go = True
		while go:
			try:
				log.info("scan", "File monitor started.")
				wm = pyinotify.WatchManager()
				wm.add_watch(str(config.get("monitor_dir")), mask, rec=True)
				pyinotify.Notifier(wm, FileEventHandler()).loop()
				go = False
			except NewDirectoryException:
				log.debug("scan", "New directory added, restarting watch.")
			finally:
				try:
					wm.close()
				except:
					pass
	finally:
		log.info("scan", "File monitor shutdown.")
예제 #6
0
파일: server.py 프로젝트: Reani/rainwave
    def _listen(self, task_id):
        # task_ids start at zero, so we gobble up ports starting at the base port and work up
        port_no = int(config.get("api_base_port")) + task_id

        # Log according to configured directory and port # we're operating on
        log_file = "%s/rw_api_%s.log" % (config.get("api_log_dir"), port_no)
        if config.test_mode and os.path.exists(log_file):
            os.remove(log_file)
        log.init(log_file, config.get("log_level"))
        log.debug("start", "Server booting, port %s." % port_no)
        db.open()
        cache.open()

        for sid in config.station_ids:
            cache.update_local_cache_for_sid(sid)

            # If we're not in developer, remove development-related URLs
        if not config.get("developer_mode"):
            i = 0
            while i < len(request_classes):
                if request_classes[i][0].find("/test/") != -1:
                    request_classes.pop(i)
                    i = i - 1
                i = i + 1

                # Make sure all other errors get handled in an API-friendly way
        request_classes.append((r".*", api.web.Error404Handler))

        # Initialize the help (rather than it scan all URL handlers every time someone hits it)
        api.help.sectionize_requests()

        # Initialize playlist variables
        playlist.prepare_cooldown_algorithm(sid)

        # Fire ze missiles!
        app = tornado.web.Application(
            request_classes,
            debug=(config.test_mode or config.get("developer_mode")),
            template_path=os.path.join(os.path.dirname(__file__), "../templates"),
            static_path=os.path.join(os.path.dirname(__file__), "../static"),
            autoescape=None,
        )
        http_server = tornado.httpserver.HTTPServer(app, xheaders=True)
        http_server.listen(port_no)

        if config.get("api_user") and config.get("api_group"):
            chuser.change_user(config.get("api_user"), config.get("api_group"))

        for request in request_classes:
            log.debug("start", "   Handler: %s" % str(request))
        log.info("start", "API server bootstrapped and ready to go.")
        self.ioloop = tornado.ioloop.IOLoop.instance()
        try:
            self.ioloop.start()
        finally:
            self.ioloop.stop()
            http_server.stop()
            db.close()
            log.info("stop", "Server has been shutdown.")
            log.close()
예제 #7
0
def descargarManga(codigoManga = None, parametros = ParamDescarga):
    log.debug(codigoManga)
    manga = config.mangas[codigoManga]
    lstExclusions = exclusionFiles(manga)   
    log.info(" exclusions.txt == %s" % lstExclusions) 
    MangaGet.lstCapitulos(manga, parametros)
    listCapitulos = []
    #TODO: Debo seguir trabajando en el tema de las exclusiones que no esta bien
    for capitulo in manga.capitulos:
        if not (capitulo.code in lstExclusions):
            listCapitulos.append(capitulo)
    fileTime =  time.strftime("%Y%m%d")       
    fileDownload = MangaFile.getMangaDownloadFolder(manga.uCode, "t%s_%s"%(fileTime, config.CONST_DOWNLOAD_FILE))  
    for capitulo in listCapitulos:        
        MangaFile.crearDirectorio(capitulo, manga)
        capitulo = MangaGet.lstImagenes(manga, capitulo)
        totalImgCarpeta = MangaFile.totalArchivosCarpeta(capitulo)
        if(capitulo.length > totalImgCarpeta):
            log.debug("Descargando Imágenes del capítulo :: %s" % capitulo.code)
            file_ = open(fileDownload, 'a')
            file_.write("====== Resumen C%s ====== \n"%(capitulo.code))
            file_.close()
            descargarImagenesCapitulo(manga, capitulo, fileDownload)
            totalImgCarpeta = MangaFile.totalArchivosCarpeta(capitulo)
            file_ = open(fileDownload, 'a')
            file_.write("C%s \t Total:%s \t Descargados:%s \n"%(capitulo.code, capitulo.length, totalImgCarpeta))
            file_.close()
        else:
            log.error("Todos los archivos del capitulo %s ya han sido descargados"%capitulo.title)  
        
    return manga
예제 #8
0
def monitor():
    _common_init()

    pid = os.getpid()
    pid_file = open("%s/scanner.pid" % config.get_directory("pid_dir"), 'w')
    pid_file.write(str(pid))
    pid_file.close()

    observers = []
    for directory, sids in config.get("song_dirs").iteritems():
        observer = RWObserver()
        observer.schedule(FileEventHandler(directory, sids),
                          directory,
                          recursive=True)
        observer.start()
        log.info("scan", "Observing %s with sids %s" % (directory, repr(sids)))
        observers.append(observer)

    try:
        while True:
            time.sleep(1)
    finally:
        for observer in observers:
            observer.stop()
        for observer in observers:
            observer.join()
예제 #9
0
파일: before.py 프로젝트: HunterTony/Portal
    def before_request():
        ### Request Logging ###
        details = {
            "parameters":      dict(flask.request.args),
            "data":            dict(flask.request.form),
            "remote_addr":     flask.request.remote_addr,
            "x_forwarded_for": flask.request.headers.get("X-Forwarded-For"),
            "username":        flask.session["username"] if "username" in flask.session else None,
        }

        if("username" in details["data"]):
            details["data"]["username"] = "******"
        if("password" in details["data"]):
            details["data"]["password"] = "******"

        log.info("{0} {1}>".format(str(flask.request).rstrip(">"), details))

        ### Check CSRF Token ###
        if(flask.request.method == "POST" or flask.request.form):
            if("_CSRF_TOKEN_" not in flask.session):
                flask.abort(400)

            if(flask.request.form["_CSRF_TOKEN_"] != flask.session["_CSRF_TOKEN_"]):
                flask.abort(400)

        ### Check Auth State ###
        if(flask.request.endpoint in auth_excluded_endpoints):
            return None

        if(not auth.check.client_authed()):
            flask.session["auth_return_url"] = flask.request.path
            return flask.redirect(flask.url_for("auth_redirect"), 302)
예제 #10
0
def get_random_song(sid):
	"""
	Fetch a random song, abiding by all election block, request block, and
	availability rules.  Falls back to get_random_ignore_requests on failure.
	"""

	sql_query = ("FROM r4_song_sid "
					"JOIN r4_songs USING (song_id) "
					"JOIN r4_album_sid ON (r4_album_sid.album_id = r4_songs.album_id AND r4_album_sid.sid = r4_song_sid.sid) "
				"WHERE r4_song_sid.sid = %s "
					"AND song_exists = TRUE "
					"AND song_cool = FALSE "
					"AND song_request_only = FALSE "
					"AND song_elec_blocked = FALSE "
					"AND album_requests_pending IS NULL")
	num_available = db.c.fetch_var("SELECT COUNT(song_id) " + sql_query, (sid,))
	log.info("song_select", "Song pool size (cooldown, blocks, requests): %s" % num_available)
	offset = 0
	if num_available == 0:
		log.warn("song_select", "No songs available despite no timing rules.")
		log.debug("song_select", "Song select query: SELECT COUNT(song_id) " + (sql_query %  (sid,)))
		return get_random_song_ignore_requests(sid)
	else:
		offset = random.randint(1, num_available) - 1
		song_id = db.c.fetch_var("SELECT song_id " + sql_query + " LIMIT 1 OFFSET %s", (sid, offset))
		return Song.load_from_id(song_id, sid)
예제 #11
0
def monitor():
	_common_init()

	pid = os.getpid()
	pid_file = open("%s/scanner.pid" % config.get_directory("pid_dir"), 'w')
	pid_file.write(str(pid))
	pid_file.close()

	observers = []
	for directory, sids in config.get("song_dirs").iteritems():
		observer = watchdog.observers.Observer()
		observer.schedule(FileEventHandler(directory, sids), directory, recursive=True)
		observer.start()
		log.info("scan", "Observing %s with sids %s" % (directory, repr(sids)))
		observers.append(observer)

	try:
		while True:
			time.sleep(60)
			_process_album_art_queue()
	except Exception as e:
		log.exception("scan", "Exception leaked to top monitoring function.", e)
		for observer in observers:
			observer.stop()
	for observer in observers:
		observer.join()
예제 #12
0
    def _listen(self, task_id):
        # task_ids start at zero, so we gobble up ports starting at the base port and work up
        port_no = int(config.get("api_base_port")) + task_id

        # Log according to configured directory and port # we're operating on
        log_file = "%s/rw_api_%s.log" % (config.get("api_log_dir"), port_no)
        if config.test_mode and os.path.exists(log_file):
            os.remove(log_file)
        log.init(log_file, config.get("log_level"))
        log.debug("start", "Server booting, port %s." % port_no)
        db.open()
        cache.open()

        for sid in config.station_ids:
            cache.update_local_cache_for_sid(sid)

        # If we're not in developer, remove development-related URLs
        if not config.get("developer_mode"):
            i = 0
            while (i < len(request_classes)):
                if request_classes[i][0].find("/test/") != -1:
                    request_classes.pop(i)
                    i = i - 1
                i = i + 1

        # Make sure all other errors get handled in an API-friendly way
        request_classes.append((r".*", api.web.Error404Handler))

        # Initialize the help (rather than it scan all URL handlers every time someone hits it)
        api.help.sectionize_requests()

        # Initialize playlist variables
        playlist.prepare_cooldown_algorithm(sid)

        # Fire ze missiles!
        app = tornado.web.Application(
            request_classes,
            debug=(config.test_mode or config.get("developer_mode")),
            template_path=os.path.join(os.path.dirname(__file__),
                                       "../templates"),
            static_path=os.path.join(os.path.dirname(__file__), "../static"),
            autoescape=None)
        http_server = tornado.httpserver.HTTPServer(app, xheaders=True)
        http_server.listen(port_no)

        if config.get("api_user") and config.get("api_group"):
            chuser.change_user(config.get("api_user"), config.get("api_group"))

        for request in request_classes:
            log.debug("start", "   Handler: %s" % str(request))
        log.info("start", "API server bootstrapped and ready to go.")
        self.ioloop = tornado.ioloop.IOLoop.instance()
        try:
            self.ioloop.start()
        finally:
            self.ioloop.stop()
            http_server.stop()
            db.close()
            log.info("stop", "Server has been shutdown.")
            log.close()
예제 #13
0
def monitor():
    _common_init()

    pid = os.getpid()
    pid_file = open("%s/scanner.pid" % config.get_directory("pid_dir"), 'w')
    pid_file.write(str(pid))
    pid_file.close()

    observers = []
    for directory, sids in config.get("song_dirs").iteritems():
        observer = watchdog.observers.Observer()
        observer.schedule(FileEventHandler(directory, sids),
                          directory,
                          recursive=True)
        observer.start()
        log.info("scan", "Observing %s with sids %s" % (directory, repr(sids)))
        observers.append(observer)

    try:
        while True:
            time.sleep(60)
            _process_album_art_queue()
    except Exception as e:
        log.exception("scan", "Exception leaked to top monitoring function.",
                      e)
        for observer in observers:
            observer.stop()
    for observer in observers:
        observer.join()
예제 #14
0
def advance_station(sid):
	db.c.start_transaction()
	try:
		log.debug("advance", "Advancing station %s." % sid)
		start_time = time.time()
		# If we need some emergency elections here
		if len(upnext[sid]) == 0:
			manage_next(sid)

		while upnext[sid][0].used or len(upnext[sid][0].songs) == 0:
			log.warn("advance", "Event ID %s was already used or has zero songs.  Removing." % upnext[sid][0].id)
			upnext[sid].pop(0)
			if len(upnext[sid]) == 0:
				manage_next(sid)

		start_time = time.time()
		upnext[sid][0].prepare_event()
		db.c.commit()

		log.debug("advance", "upnext[0] preparation time: %.6f" % (time.time() - start_time,))
		log.info("advance", "Next song: %s" % get_advancing_file(sid))

		tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(milliseconds=150), lambda: post_process(sid))
	except:
		db.c.rollback()
		raise
예제 #15
0
def search(opts):
    query= {}
    if ':' in opts:
        opts = str.split(opts, ':')
        query = {
            'track': opts[0],
            'artist': opts[1],
            'limit': 1
        }
        log.success('searching track: "%s" of Artist: "%s"' % (opts[0], opts[1]))
    else:
        query = {
            'track': opts,
            'limit': 1
        }
        log.success('searching track: "%s"' % (opts))

    params = setParams(query)
    request = URL_BASE + urllib.urlencode(params)
    log.info('url to search:', request)

    try:
        response = urllib2.urlopen(request)
        status = response.getcode()
        if status == 200:
            log.info('response status:', status)
            handleRequest(response)
        elif status == 404:
            log.warn('response status:', status)
        else:
            log.err('response status:', status)

    except urllib2.HTTPError, e:
        log.err('HTTPError:', e.code, 'the url:', e.url, e.reason)
예제 #16
0
def get_random_song_timed(sid, target_seconds = None, target_delta = None):
	"""
	Fetch a random song abiding by all election block, request block, and
	availability rules, but giving priority to the target song length
	provided.  Falls back to get_random_song on failure.
	"""
	if not target_seconds:
		return get_random_song(sid)
	if not target_delta:
		target_delta = config.get_station(sid, "song_lookup_length_delta")

	sql_query = ("FROM r4_song_sid "
					"JOIN r4_songs USING (song_id) "
					"JOIN r4_album_sid ON (r4_album_sid.album_id = r4_songs.album_id AND r4_album_sid.sid = r4_song_sid.sid) "
				"WHERE r4_song_sid.sid = %s "
					"AND song_exists = TRUE "
					"AND song_cool = FALSE "
					"AND song_elec_blocked = FALSE "
					"AND album_requests_pending IS NULL "
					"AND song_request_only = FALSE "
					"AND song_length >= %s AND song_length <= %s")
	lower_target_bound = target_seconds - (target_delta / 2)
	upper_target_bound = target_seconds + (target_delta / 2)
	num_available = db.c.fetch_var("SELECT COUNT(r4_song_sid.song_id) " + sql_query, (sid, lower_target_bound, upper_target_bound))
	log.info("song_select", "Song pool size (cooldown, blocks, requests, timed) [target %s delta %s]: %s" % (target_seconds, target_delta, num_available))
	if num_available == 0:
		log.warn("song_select", "No songs available with target_seconds %s and target_delta %s." % (target_seconds, target_delta))
		log.debug("song_select", "Song select query: SELECT COUNT(r4_song_sid.song_id) " + sql_query % (sid, lower_target_bound, upper_target_bound))
		return get_random_song(sid)
	else:
		offset = random.randint(1, num_available) - 1
		song_id = db.c.fetch_var("SELECT r4_song_sid.song_id " + sql_query + " LIMIT 1 OFFSET %s", (sid, lower_target_bound, upper_target_bound, offset))
		return Song.load_from_id(song_id, sid)
예제 #17
0
def get_random_song(sid):
	"""
	Fetch a random song, abiding by all election block, request block, and
	availability rules.  Falls back to get_random_ignore_requests on failure.
	"""

	sql_query = ("FROM r4_song_sid "
					"JOIN r4_songs USING (song_id) "
					"JOIN r4_album_sid ON (r4_album_sid.album_id = r4_songs.album_id AND r4_album_sid.sid = r4_song_sid.sid) "
				"WHERE r4_song_sid.sid = %s "
					"AND song_exists = TRUE "
					"AND song_cool = FALSE "
					"AND song_request_only = FALSE "
					"AND song_elec_blocked = FALSE "
					"AND album_requests_pending IS NULL")
	num_available = db.c.fetch_var("SELECT COUNT(song_id) " + sql_query, (sid,))
	log.info("song_select", "Song pool size (cooldown, blocks, requests): %s" % num_available)
	offset = 0
	if num_available == 0:
		log.warn("song_select", "No songs available despite no timing rules.")
		log.debug("song_select", "Song select query: SELECT COUNT(song_id) " + (sql_query %  (sid,)))
		return get_random_song_ignore_requests(sid)
	else:
		offset = random.randint(1, num_available) - 1
		song_id = db.c.fetch_var("SELECT song_id " + sql_query + " LIMIT 1 OFFSET %s", (sid, offset))
		return Song.load_from_id(song_id, sid)
예제 #18
0
파일: web.py 프로젝트: Abchrisabc/rainwave
	def prepare(self):
		if self.local_only and not self.request.remote_ip in config.get("api_trusted_ip_addresses"):
			log.info("api", "Rejected %s request from %s, untrusted address." % (self.url, self.request.remote_ip))
			raise APIException("rejected", text="You are not coming from a trusted address.")

		if self.allow_cors:
			self.set_header("Access-Control-Allow-Origin", "*")
			self.set_header("Access-Control-Max-Age", "600")
			self.set_header("Access-Control-Allow-Credentials", "false")

		if not isinstance(self.locale, locale.RainwaveLocale):
			self.locale = self.get_browser_locale()

		self.setup_output()

		if 'in_order' in self.request.arguments:
			self._output = []
			self._output_array = True
		else:
			self._output = {}

		if not self.sid:
			self.sid = fieldtypes.integer(self.get_cookie("r4_sid", None))
			hostname = self.request.headers.get('Host', None)
			if hostname:
				hostname = unicode(hostname).split(":")[0]
				if hostname in config.station_hostnames:
					self.sid = config.station_hostnames[hostname]
			sid_arg = fieldtypes.integer(self.get_argument("sid", None))
			if sid_arg is not None:
				self.sid = sid_arg
			if self.sid is None and self.sid_required:
				raise APIException("missing_station_id", http_code=400)

		self.arg_parse()

		self.sid_check()

		if self.sid:
			self.set_cookie("r4_sid", str(self.sid), expires_days=365)

		if self.phpbb_auth:
			self.do_phpbb_auth()
		else:
			self.rainwave_auth()

		if not self.user and self.auth_required:
			raise APIException("auth_required", http_code=403)
		elif not self.user and not self.auth_required:
			self.user = User(1)
			self.user.ip_address = self.request.remote_ip

		self.user.refresh(self.sid)

		if self.user and config.get("store_prefs"):
			self.user.save_preferences(self.request.remote_ip, self.get_cookie("r4_prefs", None))

		self.permission_checks()
예제 #19
0
	def prepare(self):
		if self.local_only and not self.request.remote_ip in config.get("api_trusted_ip_addresses"):
			log.info("api", "Rejected %s request from %s, untrusted address." % (self.url, self.request.remote_ip))
			raise APIException("rejected", text="You are not coming from a trusted address.")

		if self.allow_cors:
			self.set_header("Access-Control-Allow-Origin", "*")
			self.set_header("Access-Control-Max-Age", "600")
			self.set_header("Access-Control-Allow-Credentials", "false")

		if not isinstance(self.locale, locale.RainwaveLocale):
			self.locale = self.get_browser_locale()

		self.setup_output()

		if 'in_order' in self.request.arguments:
			self._output = []
			self._output_array = True
		else:
			self._output = {}

		self.sid = fieldtypes.integer(self.get_cookie("r4_sid", None))
		hostname = self.request.headers.get('Host', None)
		if hostname:
			hostname = unicode(hostname).split(":")[0]
			if hostname in config.station_hostnames:
				self.sid = config.station_hostnames[hostname]
		sid_arg = fieldtypes.integer(self.get_argument("sid", None))
		if sid_arg is not None:
			self.sid = sid_arg
		if self.sid is None and self.sid_required:
			raise APIException("missing_station_id", http_code=400)

		self.arg_parse()

		self.sid_check()

		if self.sid:
			self.set_cookie("r4_sid", str(self.sid), expires_days=365, domain=config.get("cookie_domain"))

		if self.phpbb_auth:
			self.do_phpbb_auth()
		else:
			self.rainwave_auth()

		if not self.user and self.auth_required:
			raise APIException("auth_required", http_code=403)
		elif not self.user and not self.auth_required:
			self.user = User(1)
			self.user.ip_address = self.request.remote_ip

		self.user.refresh(self.sid)

		if self.user and config.get("store_prefs"):
			self.user.save_preferences(self.request.remote_ip, self.get_cookie("r4_prefs", None))

		self.permission_checks()
예제 #20
0
def getMangaInfo(manga = Manga):
    parser = InfoHTMLParser()
    http = httplib2.Http(".cache", disable_ssl_certificate_validation=True)
    log.info("http.request[infoManga] ==> %s"%manga.infoUrl)
    headers, body = http.request(manga.infoUrl)
    #body = str(body).decode('utf-8')
    parser.feed("%s"%body)
    parser.close()  
    return parser.STATUS, parser.VOL_INFO  
예제 #21
0
    def _listen(self, sid):
        pid = os.getpid()
        pid_file = open(
            "%s/backend_%s.pid" % (
                config.get_directory("pid_dir"),
                config.station_id_friendly[sid].lower(),
            ),
            "w",
        )
        pid_file.write(str(pid))
        pid_file.close()

        db.connect()
        cache.connect()
        zeromq.init_pub()
        log.init(
            "%s/rw_%s.log" % (
                config.get_directory("log_dir"),
                config.station_id_friendly[sid].lower(),
            ),
            config.get("log_level"),
        )
        memory_trace.setup(config.station_id_friendly[sid].lower())

        if config.test_mode:
            playlist.remove_all_locks(sid)

        # (r"/refresh/([0-9]+)", RefreshScheduleRequest)
        app = tornado.web.Application(
            [
                (r"/advance/([0-9]+)", AdvanceScheduleRequest),
            ],
            debug=(config.test_mode or config.get("developer_mode")),
        )

        port = int(config.get("backend_port")) + sid
        server = tornado.httpserver.HTTPServer(app)
        server.listen(port, address="127.0.0.1")

        for station_id in config.station_ids:
            playlist.prepare_cooldown_algorithm(station_id)
        schedule.load()
        log.debug(
            "start",
            "Backend server started, station %s port %s, ready to go." %
            (config.station_id_friendly[sid], port),
        )

        ioloop = tornado.ioloop.IOLoop.instance()
        try:
            ioloop.start()
        finally:
            ioloop.stop()
            server.stop()
            db.close()
            log.info("stop", "Backend has been shutdown.")
            log.close()
예제 #22
0
def manage_next(sid):
    # pylint: disable=W0612
    max_sched_id, max_elec_id, num_elections, max_future_time = _get_schedule_stats(sid)
    # pylint: enable=W0612
    now_producer = get_producer_at_time(sid, timestamp())
    next_producer = get_producer_at_time(sid, max_future_time)
    nextnext_producer_start = db.c.fetch_var(
        "SELECT sched_start FROM r4_schedule WHERE sid = %s AND sched_used = FALSE AND sched_start > %s AND sched_timed = TRUE",
        (sid, max_future_time),
    )
    time_to_future_producer = None
    if nextnext_producer_start:
        time_to_future_producer = nextnext_producer_start - max_future_time
    else:
        time_to_future_producer = 86400
    while len(upnext[sid]) < min(now_producer.plan_ahead_limit, next_producer.plan_ahead_limit):
        target_length = None
        if time < 20:
            log.debug("timing", "SID %s <20 seconds to upnext event, not using timing." % sid)
        if time_to_future_producer < 40:
            target_length = time_to_future_producer
            next_producer = rainwave.events.shortest_election.ShortestElectionProducer(sid)
            log.debug("timing", "SID %s <40 seconds to upnext event, using shortest elections." % sid)
        elif time_to_future_producer < (playlist.get_average_song_length(sid) * 1.3):
            target_length = time_to_future_producer
            log.debug("timing", "SID %s close to event, timing to %s seconds long." % (sid, target_length))
        elif time_to_future_producer < (playlist.get_average_song_length(sid) * 2.2):
            target_length = playlist.get_average_song_length(sid)
            log.debug("timing", "SID %s has an upcoming event, timing to %s seconds long." % (sid, target_length))
        next_event = next_producer.load_next_event(target_length, max_elec_id)
        if not next_event:
            log.info(
                "manage_next",
                "Producer ID %s type %s did not produce an event." % (next_producer.id, next_producer.type),
            )
            next_producer = election.ElectionProducer(sid)
            next_event = next_producer.load_next_event(target_length, max_elec_id)
        upnext[sid].append(next_event)
        if next_event.is_election:
            num_elections += 1
        if next_event.is_election and next_event.id > max_elec_id:
            max_elec_id = next_event.id
        max_future_time += upnext[sid][-1].length()
        time_to_future_producer -= upnext[sid][-1].length()
        next_producer = get_producer_at_time(sid, max_future_time)

    future_time = None
    if current[sid].start:
        future_time = current[sid].start + current[sid].length()
    else:
        future_time = int(timestamp() + current[sid].length())
    for evt in upnext[sid]:
        evt.start = future_time
        future_time += evt.length()
        if evt.is_election:
            evt.update_vote_counts()
예제 #23
0
def listaVolumenes(manga = Manga):
    parser = VolumenHTMLParser()
    http = httplib2.Http()
    urlVol = getURLScann(manga)
    log.info("http.request[listaVolumenes] ==> %s"%urlVol)  
    headers, body = http.request(urlVol)
    body = str(body).decode('utf-8')
    parser.feed("%s"%body)
    parser.close()
    return parser.VOLUMEN, parser.STATUS
예제 #24
0
    def started_info(self):
        log.info(
            '\n==========================================================================================='
            '============== \ntest details'
            '\n---------\ntest_id:%s\ntest_name:%s\n============================='
            % (self.id, self.name))

        print '---------------------------'
        print 'test_started:%s' % self.name
        print 'test_id:%s' % self.id
예제 #25
0
    def started_info(self):
        log.info(
            "\n==========================================================================================="
            "============== \ntest details"
            "\n---------\ntest_id:%s\ntest_name:%s\n============================="
            % (self.id, self.name))

        print "---------------------------"
        print "test_started:%s" % self.name
        print "test_id:%s" % self.id
예제 #26
0
	def disable(self):
		if not self.id:
			log.critical("song_disable", "Tried to disable a song without a song ID.")
			return
		log.info("song_disable", "Disabling ID %s / file %s" % (self.id, self.filename))
		db.c.update("UPDATE r4_songs SET song_verified = FALSE WHERE song_id = %s", (self.id,))
		db.c.update("UPDATE r4_song_sid SET song_exists = FALSE WHERE song_id = %s", (self.id,))
		if self.albums:
			for metadata in self.albums:
				metadata.reconcile_sids()
예제 #27
0
def obtenerURLCaps(manga = Manga):
    pat = re.compile(CONST_EXP_LST_CAPITULOS)    
    manga.url = 'http://%s/%s/'%(manga.site, manga.code)
    log.info("http.request[lstCapitulos] ==> %s"%manga.url)  
    http = httplib2.Http()
    headers, body = http.request(manga.url)
    log.file(body)
    caps = pat.findall("%s"%body)
    log.file(caps)
    urlCapitulo = caps[0][0]
    return urlCapitulo
예제 #28
0
def excludeFilesTest():
    manga = mangas['liar_game']
    parametros = ParamDescarga(None, None)
    lstExclusions = Main.exclusionFiles(manga)   
    log.info(" exclusions.txt == %s" % lstExclusions) 
    MangaGet.lstCapitulos(manga, parametros)
    listCapitulos = []
    #TODO: Debo seguir trabajando en el tema de las exclusiones que no esta bien
    for capitulo in manga.capitulos:        
        if not (capitulo.code in lstExclusions):
            print capitulo.code
            listCapitulos.append(capitulo)
예제 #29
0
def main():
    # sound test
    log.info('test beep')
    brick.sound.beep()

    # color sensor test
    sensor_color = ColorSensor(ROBOT['sensor_color_port'])
    log.debug('color sensor: color=%s' % sensor_color.color())

    # gyro sensor test
    sensor_gyro = GyroSensor(ROBOT['sensor_gyro_port'])
    log.debug('gyro sensor: speed=%d, angle=%d' %
              (sensor_gyro.speed(), sensor_gyro.angle()))
예제 #30
0
def renombrarArchivos(mypath='.', prefijo=None, sufijo = '', overflow=7):
    log.debug("Renombrar Archivos")
    listFiles = [ f for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath,f)) ]
    largo = len(str(len(listFiles))) + overflow
    log.debug("Total de archivos = %s"%len(listFiles))
    for fileName in listFiles :
        newName = fileName.replace(prefijo, '')
        newName = newName.replace(sufijo, '')
        newName = "p_%s"%funciones.prefijo(newName, largo)
        pathFile = "%s%s"%(mypath, fileName)
        pathNewFile = "%s%s"%(mypath, newName)
        os.rename(pathFile, pathNewFile)
        log.info("Ren %s -> %s"%(fileName, newName))
예제 #31
0
def clean_response(response):

    log.info(response.content)

    response.raise_for_status()

    pretty_response = json.dumps(response.json(), indent=2)

    cleaned_response = json.loads(pretty_response)

    log.info("\n%s" % pretty_response)

    return cleaned_response
예제 #32
0
파일: song.py 프로젝트: Abchrisabc/rainwave
	def disable(self):
		if not self.id:
			log.critical("song_disable", "Tried to disable a song without a song ID.")
			return
		log.info("song_disable", "Disabling ID %s / file %s" % (self.id, self.filename))
		db.c.update("UPDATE r4_songs SET song_verified = FALSE WHERE song_id = %s", (self.id,))
		db.c.update("UPDATE r4_song_sid SET song_exists = FALSE WHERE song_id = %s", (self.id,))
		db.c.update("DELETE FROM r4_request_store WHERE song_id = %s", (self.id,))
		if self.albums:
			for metadata in self.albums:
				metadata.reconcile_sids()
		if self.groups:
			for metadata in self.groups:
				metadata.reconcile_sids()
예제 #33
0
def obtenerCapitulos(manga = Manga, urlCapitulos = '', parametros = ParamDescarga):
    codCapituloIni = parametros.codCapitulo
    pat = re.compile('<td class="s">(.+?)</td>')
    http = httplib2.Http()
    lstCaps = []
    listCapsRet=[]
    lst=[]
    log.info("http.request[lstCapitulos] ==> %s"%urlCapitulos) 
    headers, body = http.request(urlCapitulos)
    log.file(body)
    li = pat.findall("%s"%body)
    log.file(li)
    for elem in li:    
        strOption = str(elem)
        strOption = strOption.replace('<strong>', '')
        strOption = strOption.replace('</strong>', '')
        pat2 = re.compile('<a href="(.+?)">(.+?)</a>')
        cap = pat2.findall("%s"%strOption)
        lstCaps.append(cap)    
    total = len(lstCaps) 
    manga.length = total
    totPre = len(str(total))
#    print lstCaps
#    exit(0)
    if(total > 1):          
        lstCaps = sorted(lstCaps, key=splittedname)
    if(codCapituloIni is not None):
        for cap in lstCaps:
            codCap = cap[0][0].split("/")[-2]
            codCap = funciones.prefijo(codCap, totPre)
            capIni = funciones.prefijo(str(codCapituloIni), totPre)
            #codCap = funciones.decode(codCap)
            if(parametros.tipo == TYPE.UNIQUE and codCap == capIni):
                listCapsRet.append(cap)
                break
            if (codCap >= capIni and parametros.tipo != TYPE.UNIQUE):
                listCapsRet.append(cap)
    else:
        listCapsRet = lstCaps

    
    for cap in listCapsRet:
        capitulo = Capitulo(cap[0][0], cap[0][1])
        #capitulo.url = "%s%s/"%(manga.url, capitulo.code)
        capitulo.code = capitulo.url.split("/")[-2]
        auxUri = capitulo.url.split("/")[-3]
        capitulo.url = capitulo.url.replace("%s/%s"%(auxUri, capitulo.code), "c")
        lst.append(capitulo)    
    return lst, manga
예제 #34
0
def obtenerCapitulos(manga = Manga, urlCapitulos = '', parametros = ParamDescarga):
    lst=[]
    listCapsRet = []    
    codCapituloIni = parametros.codCapitulo
    parser = esMangaHTMLParser(manga)    
    http = httplib2.Http()
    log.info("http.request[lstCapitulos] ==> %s"%urlCapitulos) 
    headers, body = http.request(manga.url)
    parser.feed("%s"%body)
    for capitulo in parser.CAPITULOS:
        strUrl = capitulo.url
        res = strUrl.split("/")[-1]
        res = res.replace("c", "", 1)        
        capitulo.code = res
        capitulo.title = "%s %s"%(capitulo.title, capitulo.code)
        lst.append(capitulo)
        
    total = len(lst)
    totPre = len(str(total))
    lstAux = []
    index = 0
    for cap in lst:
        encontrado = False
        codCapOrig = funciones.prefijo(str(cap.code), totPre)
        for capAux in lstAux:
            codCapAux = funciones.prefijo(str(capAux.code), totPre)
            if codCapOrig < codCapAux:
                index = lstAux.index(capAux)
                encontrado = True
                break
        if not encontrado:
            lstAux.append(cap)
        else:
            lstAux.insert(index, cap)
    lst = lstAux    
    if(codCapituloIni is not None):
        capIni = funciones.prefijo(str(codCapituloIni), totPre)
        for cap in lst:
            codCap = funciones.prefijo(cap.code, totPre)
            if(parametros.tipo == TYPE.UNIQUE and codCap == capIni):
                listCapsRet.append(cap)
                break
            if (codCap >= capIni and parametros.tipo != TYPE.UNIQUE):
                listCapsRet.append(cap)            
    else:
        listCapsRet = lst
    manga.length = len(lst)        
    return listCapsRet, manga
예제 #35
0
파일: schedule.py 프로젝트: Dinir/rainwave
def manage_next(sid):
	max_sched_id, max_elec_id, num_elections, max_future_time = _get_schedule_stats(sid)
	now_producer = get_producer_at_time(sid, time.time())
	next_producer = get_producer_at_time(sid, max_future_time)
	nextnext_producer_start = db.c.fetch_var("SELECT sched_start FROM r4_schedule WHERE sid = %s AND sched_used = FALSE AND sched_start > %s AND sched_timed = TRUE", (sid, max_future_time))
	time_to_future_producer = None
	if nextnext_producer_start:
		time_to_future_producer = nextnext_producer_start - max_future_time
	else:
		time_to_future_producer = 86400
	while len(upnext[sid]) < min(now_producer.plan_ahead_limit, next_producer.plan_ahead_limit):
		target_length = None
		if time < 20:
			log.debug("timing", "SID %s <20 seconds to upnext event, not using timing." % sid)
		if time_to_future_producer < 40:
			target_length = time_to_future_producer
			next_producer = rainwave.events.shortest_election.ShortestElectionProducer(sid)
			log.debug("timing", "SID %s <40 seconds to upnext event, using shortest elections." % sid)
		elif time_to_future_producer < (playlist.get_average_song_length(sid) * 1.3):
			target_length = time_to_future_producer
			log.debug("timing", "SID %s close to event, timing to %s seconds long." % (sid, target_length))
		elif time_to_future_producer < (playlist.get_average_song_length(sid) * 2.2):
			target_length = playlist.get_average_song_length(sid)
			log.debug("timing", "SID %s has an upcoming event, timing to %s seconds long." % (sid, target_length))
		next_event = next_producer.load_next_event(target_length, max_elec_id)
		if not next_event:
			log.info("manage_next", "Producer ID %s type %s did not produce an event." % (next_producer.id, next_producer.type))
			next_producer = election.ElectionProducer(sid)
			next_event = next_producer.load_next_event(target_length, max_elec_id)
		upnext[sid].append(next_event)
		if next_event.is_election:
			num_elections += 1
		if next_event.is_election and next_event.id > max_elec_id:
			max_elec_id = next_event.id
		max_future_time += upnext[sid][-1].length()
		time_to_future_producer -= upnext[sid][-1].length()
		next_producer = get_producer_at_time(sid, max_future_time)

	future_time = None
	if current[sid].start:
		future_time = current[sid].start + current[sid].length()
	else:
		future_time = int(time.time() + current[sid].length())
	for evt in upnext[sid]:
		evt.start = future_time
		future_time += evt.length()
예제 #36
0
    def patch(self, url, data, request_api=True):

        try:

            log.info('data to patch\n %s' % data)

            response = self.http_request.patch(url, data)

            cleaned_response = clean_response(response)

            if request_api:
                check_request_id(self.api_request, cleaned_response['request_id'])

            return cleaned_response

        except Exception:
            log.error('\n%s' % traceback.format_exc())
            raise AssertionError
예제 #37
0
    def post(self, url, data, request_api=True):

        try:

            log.info("data to post:\n%s" % data)

            response = self.http_request.post(url, data)

            cleaned_response = clean_response(response)

            if request_api:

                check_request_id(self.api_request,
                                 cleaned_response["request_id"])

            return cleaned_response

        except Exception:
            log.error("\n%s" % traceback.format_exc())
            raise AssertionError
예제 #38
0
	def _listen(self, sid):
		pid = os.getpid()
		pid_file = open("%s/backend_%s.pid" % (config.get_directory("pid_dir"), config.station_id_friendly[sid].lower()), 'w')
		pid_file.write(str(pid))
		pid_file.close()

		db.connect()
		cache.connect()
		zeromq.init_pub()
		log.init("%s/rw_%s.log" % (config.get_directory("log_dir"), config.station_id_friendly[sid].lower()), config.get("log_level"))
		memory_trace.setup(config.station_id_friendly[sid].lower())

		if config.test_mode:
			playlist.remove_all_locks(sid)

		# (r"/refresh/([0-9]+)", RefreshScheduleRequest)
		app = tornado.web.Application([
			(r"/advance/([0-9]+)", AdvanceScheduleRequest),
			], debug=(config.test_mode or config.get("developer_mode")))

		port = int(config.get("backend_port")) + sid
		server = tornado.httpserver.HTTPServer(app)
		server.listen(port, address='127.0.0.1')

		for station_id in config.station_ids:
			playlist.prepare_cooldown_algorithm(station_id)
		schedule.load()
		log.debug("start", "Backend server started, station %s port %s, ready to go." % (config.station_id_friendly[sid], port))

		ioloop = tornado.ioloop.IOLoop.instance()
		try:
			ioloop.start()
		finally:
			ioloop.stop()
			server.stop()
			db.close()
			log.info("stop", "Backend has been shutdown.")
			log.close()
예제 #39
0
def descargarArchivo(imagen = Imagen, capitulo = Capitulo, manga = Manga, fileDownload = None):
    estado = False
    try:
        filename = imagen.urlReal.split("/")[-1]
        filename = funciones.agregaCeros(filename, config.CONST_CANTIDAD_CERO_IMG)
        filePath = '%s/%s' %(capitulo.folder, filename)
        if(not os.path.isfile(filePath)):
            log.info('curl %s -o %s/%s'%( imagen.urlReal, capitulo.folder, filename))
            os.system('curl %s -o %s/%s' % (imagen.urlReal, capitulo.folder, filename))        
        else:
            log.error('El archivo [%s] ya existe'% filename)
        imagen.path = filePath
        estado = True
    except Exception:
        log.error("No se pudo descargar la img %s"%imagen.code)
        estado = False
    finally:
            
        if not estado:
            file_ = open(fileDownload, 'a')
            file_.write("%s \t C%s I%s \t %s \n"%(estado, capitulo.code, imagen.code, imagen.urlReal))
            file_.close()
    return imagen
예제 #40
0
def infoManga(manga = Manga):
    log.info("[Info Manga] %s"%manga.code)
    fileInfo = "%s%s/%s"%(config.CONST_PATH, manga.uCode, config.CONST_INFO_FILE)

    #open the file template
    filein = open( '%s/tpl/info.tpl'%config.CONST_PATH_SRC )
    info = VolumenScan.getURLScann(manga)
    info = str(info).decode('utf-8')
    lstVol, status = VolumenScan.listaVolumenes(manga)
    listVol = []
    countVol = int(0)
    countCap = int(0)
    for vol in lstVol:                
        for cap in vol.capitulos:
            capStr = str("  > %s"%cap.name).decode('utf-8')
            listVol.append(capStr)
            countCap = countCap + 1
        listVol.append("------------------------------")
        listVol.append(vol.name)
        listVol.append("------------------------------")
        countVol = countVol + 1
    listVol = listVol[::-1]
    status, volinfo = VolumenScan.getMangaInfo(manga)
    #read it
    src = Template( str(filein.read()).decode('utf-8') )
    #document data
    title = str(manga.code).decode('utf-8')
    cover = str(manga.cover).decode('utf-8')
    d={ 'title':title, 'list':'\n'.join(listVol) , 'cover' : cover, 'info' : info, 'countCap' : countCap, 'countVol' : countVol, 'status' : status, 'volinfo': volinfo}
    #do the substitution
    result = src.substitute(d)
    result = result.encode('utf-8')
    log.debug(result)
    file_ = open(fileInfo, 'w')
    file_.write(result)
    file_.close()
예제 #41
0
def monitor():
	_common_init()

	pid = os.getpid()
	pid_file = open("%s/scanner.pid" % config.get_directory("pid_dir"), 'w')
	pid_file.write(str(pid))
	pid_file.close()

	observers = []
	for directory, sids in config.get("song_dirs").iteritems():
		observer = RWObserver()
		observer.schedule(FileEventHandler(directory, sids), directory, recursive=True)
		observer.start()
		log.info("scan", "Observing %s with sids %s" % (directory, repr(sids)))
		observers.append(observer)

	try:
		while True:
			time.sleep(1)
	finally:
		for observer in observers:
			observer.stop()
		for observer in observers:
			observer.join()
예제 #42
0
    def _listen(self, task_id):
        import api_requests.sync
        api_requests.sync.init()

        # task_ids start at zero, so we gobble up ports starting at the base port and work up
        port_no = int(config.get("api_base_port")) + task_id

        pid = os.getpid()
        pid_file = open(
            "%s/api_%s.pid" % (config.get_directory("pid_dir"), port_no), 'w')
        pid_file.write(str(pid))
        pid_file.close()

        # Log according to configured directory and port # we're operating on
        log_file = "%s/rw_api_%s.log" % (config.get_directory("log_dir"),
                                         port_no)
        if config.test_mode and os.path.exists(log_file):
            os.remove(log_file)
        log.init(log_file, config.get("log_level"))
        log.debug("start", "Server booting, port %s." % port_no)
        db.connect()
        cache.connect()
        memory_trace.setup(port_no)

        if config.get("web_developer_mode"):
            for station_id in config.station_ids:
                playlist.prepare_cooldown_algorithm(station_id)
            # automatically loads every station ID and fills things in if there's no data
            schedule.load()
            for station_id in config.station_ids:
                schedule.update_memcache(station_id)
                rainwave.request.update_line(station_id)
                rainwave.request.update_expire_times()
                cache.set_station(station_id, "backend_ok", True)
                cache.set_station(station_id, "backend_message", "OK")
                cache.set_station(station_id, "get_next_socket_timeout", False)

        for sid in config.station_ids:
            cache.update_local_cache_for_sid(sid)
            playlist.prepare_cooldown_algorithm(sid)
            playlist.update_num_songs()

        # If we're not in developer, remove development-related URLs
        if not config.get("developer_mode"):
            i = 0
            while (i < len(request_classes)):
                if request_classes[i][0].find("/test/") != -1:
                    request_classes.pop(i)
                    i = i - 1
                i = i + 1

        # Make sure all other errors get handled in an API-friendly way
        request_classes.append((r"/api/.*", api.web.Error404Handler))
        request_classes.append((r"/api4/.*", api.web.Error404Handler))
        request_classes.append((r".*", api.web.HTMLError404Handler))

        # Initialize the help (rather than it scan all URL handlers every time someone hits it)
        api.help.sectionize_requests()

        # Fire ze missiles!
        app = tornado.web.Application(
            request_classes,
            debug=(config.test_mode or config.get("developer_mode")),
            template_path=os.path.join(os.path.dirname(__file__),
                                       "../templates"),
            static_path=os.path.join(os.path.dirname(__file__), "../static"),
            autoescape=None)
        http_server = tornado.httpserver.HTTPServer(app, xheaders=True)
        http_server.listen(port_no)

        if config.get("api_user") and config.get("api_group"):
            chuser.change_user(config.get("api_user"), config.get("api_group"))

        if task_id == 0:
            buildtools.bake_css()
            buildtools.bake_js()
            buildtools.bake_beta_js()

        for request in request_classes:
            log.debug("start", "   Handler: %s" % str(request))
        log.info("start", "API server on port %s ready to go." % port_no)
        self.ioloop = tornado.ioloop.IOLoop.instance()

        try:
            self.ioloop.start()
        finally:
            self.ioloop.stop()
            http_server.stop()
            db.close()
            log.info("stop", "Server has been shutdown.")
            log.close()
예제 #43
0
	def prepare(self):
		self._startclock = time.time()
		self.user = None

		if self.local_only and not self.request.remote_ip in config.get("api_trusted_ip_addresses"):
			log.info("api", "Rejected %s request from %s" % (self.url, self.request.remote_ip))
			self.set_status(403)
			self.finish()

		if not isinstance(self.locale, locale.RainwaveLocale):
			self.locale = self.get_browser_locale()

		if not self.return_name:
			self.return_name = self.url[self.url.rfind("/")+1:] + "_result"
		else:
			self.return_name = self.return_name

		if self.admin_required or self.dj_required:
			self.login_required = True

		if 'in_order' in self.request.arguments:
			self._output = []
			self._output_array = True
		else:
			self._output = {}
			self._output_array = False

		self.sid = fieldtypes.integer(self.get_cookie("r4_sid", "1"))
		if "sid" in self.request.arguments:
			self.sid = int(self.get_argument("sid"))
		elif not self.sid:
			for possible_sid in config.station_ids:
				if self.request.host == config.get_station(possible_sid, "host"):
					self.sid = possible_sid
					break
		if not self.sid and self.sid_required:
			raise APIException("missing_station_id", http_code=400)
		if self.sid and not self.sid in config.station_ids:
			raise APIException("invalid_station_id", http_code=400)
		self.set_cookie("r4_sid", str(self.sid), expires_days=365, domain=config.get("cookie_domain"))

		for field, field_attribs in self.__class__.fields.iteritems():
			type_cast, required = field_attribs
			if required and field not in self.request.arguments:
				raise APIException("missing_argument", argument=field, http_code=400)
			elif not required and field not in self.request.arguments:
				self.cleaned_args[field] = None
			else:
				parsed = type_cast(self.get_argument(field), self)
				if parsed == None and required != None:
					raise APIException("invalid_argument", argument=field, reason=getattr(fieldtypes, "%s_error" % type_cast.__name__), http_code=400)
				else:
					self.cleaned_args[field] = parsed

		if self.phpbb_auth:
			self.do_phpbb_auth()
		else:
			self.rainwave_auth()

		if self.auth_required and not self.user:
			raise APIException("auth_required", http_code=403)

		if self.login_required and (not self.user or self.user.is_anonymous()):
			raise APIException("login_required", http_code=403)
		if self.tunein_required and (not self.user or not self.user.is_tunedin()):
			raise APIException("tunein_required", http_code=403)
		if self.admin_required and (not self.user or not self.user.is_admin()):
			raise APIException("admin_required", http_code=403)
		if self.dj_required and (not self.user or not self.user.is_dj()):
			raise APIException("dj_required", http_code=403)
		if self.perks_required and (not self.user or not self.user.has_perks()):
			raise APIException("perks_required", http_code=403)

		if self.unlocked_listener_only and not self.user:
			raise APIException("auth_required", http_code=403)
		elif self.unlocked_listener_only and self.user.data['listener_lock'] and self.user.data['listener_lock_sid'] != self.sid:
			raise APIException("unlocked_only", station=config.station_id_friendly[self.user.data['listener_lock_sid']], lock_counter=self.user.data['listener_lock_counter'], http_code=403)
예제 #44
0
import os
import time
import flask
import datetime

import config
from root import route
from root import csrf
from root import error
from root import before
from libs import log

log.info("Initialising server")

if (config.root.debug is not True):
    os.environ["TZ"] = "UTC"
    time.tzset()

### Init Flask ###
app = flask.Flask(__name__)

app.static_folder = "../static/app/"
app.template_folder = "templates/"

app.secret_key = config.flask.session_key
if (app.secret_key == "debug" and config.root.debug is not True):
    raise RuntimeError("Secret key not set in a production environment")

app.permanent_session_lifetime = datetime.timedelta(hours=4)

app.jinja_env.trim_blocks = True
예제 #45
0
파일: web.py 프로젝트: Siqo53/rainwave
	def prepare(self):
		if self.local_only and not self.request.remote_ip in config.get("api_trusted_ip_addresses"):
			log.info("api", "Rejected %s request from %s, untrusted address." % (self.url, self.request.remote_ip))
			raise APIException("rejected", text="You are not coming from a trusted address.")

		if not isinstance(self.locale, locale.RainwaveLocale):
			self.locale = self.get_browser_locale()

		if not self.return_name:
			self.return_name = self.url[self.url.rfind("/")+1:] + "_result"
		else:
			self.return_name = self.return_name

		if self.admin_required:
			self.login_required = True

		if 'in_order' in self.request.arguments:
			self._output = []
			self._output_array = True
		else:
			self._output = {}

		self.sid = fieldtypes.integer(self.get_cookie("r4_sid", None))
		hostname = self.request.headers.get('Host', None)
		if hostname:
			hostname = unicode(hostname).split(":")[0]
			if hostname in config.station_hostnames:
				self.sid = config.station_hostnames[hostname]
		sid_arg = fieldtypes.integer(self.get_argument("sid", None))
		if sid_arg is not None:
			self.sid = sid_arg
		if self.sid is None and self.sid_required:
			raise APIException("missing_station_id", http_code=400)

		for field, field_attribs in self.__class__.fields.iteritems():
			type_cast, required = field_attribs
			if required and field not in self.request.arguments:
				raise APIException("missing_argument", argument=field, http_code=400)
			elif not required and field not in self.request.arguments:
				self.cleaned_args[field] = None
			else:
				parsed = type_cast(self.get_argument(field), self)
				if parsed == None and required != None:
					raise APIException("invalid_argument", argument=field, reason="%s %s" % (field, getattr(fieldtypes, "%s_error" % type_cast.__name__)), http_code=400)
				else:
					self.cleaned_args[field] = parsed

		if self.sid is None and not self.sid_required:
			self.sid = config.get("default_station")
		if self.sid == 0 and self.allow_sid_zero:
			pass
		elif not self.sid in config.station_ids:
			raise APIException("invalid_station_id", http_code=400)
		if self.sid:
			self.set_cookie("r4_sid", str(self.sid), expires_days=365, domain=config.get("cookie_domain"))

		if self.phpbb_auth:
			self.do_phpbb_auth()
		else:
			self.rainwave_auth()

		if not self.user and self.auth_required:
			raise APIException("auth_required", http_code=403)
		elif not self.user and not self.auth_required:
			self.user = User(1)
			self.user.ip_address = self.request.remote_ip

		self.user.refresh(self.sid)

		if self.user and config.get("store_prefs"):
			self.user.save_preferences(self.request.remote_ip, self.get_cookie("r4_prefs", None))

		if self.login_required and (not self.user or self.user.is_anonymous()):
			raise APIException("login_required", http_code=403)
		if self.tunein_required and (not self.user or not self.user.is_tunedin()):
			raise APIException("tunein_required", http_code=403)
		if self.admin_required and (not self.user or not self.user.is_admin()):
			raise APIException("admin_required", http_code=403)
		if self.perks_required and (not self.user or not self.user.has_perks()):
			raise APIException("perks_required", http_code=403)

		if self.unlocked_listener_only and not self.user:
			raise APIException("auth_required", http_code=403)
		elif self.unlocked_listener_only and self.user.data['lock'] and self.user.data['lock_sid'] != self.sid:
			raise APIException("unlocked_only", station=config.station_id_friendly[self.user.data['lock_sid']], lock_counter=self.user.data['lock_counter'], http_code=403)

		is_dj = False
		if self.dj_required and not self.user:
			raise APIException("dj_required", http_code=403)
		if self.dj_required and not self.user.is_admin():
			#pylint: disable=E1103
			potential_dj_ids = []
			if cache.get_station(self.sid, "sched_current") and cache.get_station(self.sid, "sched_current").dj_user_id:
				potential_dj_ids.append(cache.get_station(self.sid, "sched_current").dj_user_id)
			if cache.get_station(self.sid, "sched_next"):
				for evt in cache.get_station(self.sid, "sched_next"):
					potential_dj_ids.append(evt.dj_user_id)
			if cache.get_station(self.sid, "sched_history") and cache.get_station(self.sid, "sched_history")[-1].dj_user_id:
				potential_dj_ids.append(cache.get_station(self.sid, "sched_history")[-1].dj_user_id)
			if not self.user.id in potential_dj_ids:
				raise APIException("dj_required", http_code=403)
			is_dj = True
			#pylint: enable=E1103

		if self.dj_preparation and not is_dj and not self.user.is_admin():
			if not db.c.fetch_var("SELECT COUNT(*) FROM r4_schedule WHERE sched_used = 0 AND sched_dj_user_id = %s", (self.user.id,)):
				raise APIException("dj_required", http_code=403)
예제 #46
0
	def _listen(self, task_id):
		zeromq.init_pub()
		zeromq.init_sub()

		import api_requests.sync
		api_requests.sync.init()

		# task_ids start at zero, so we gobble up ports starting at the base port and work up
		port_no = int(config.get("api_base_port")) + task_id

		pid = os.getpid()
		pid_file = open("%s/api_%s.pid" % (config.get_directory("pid_dir"), port_no), 'w')
		pid_file.write(str(pid))
		pid_file.close()

		# Log according to configured directory and port # we're operating on
		log_file = "%s/rw_api_%s.log" % (config.get_directory("log_dir"), port_no)
		if config.test_mode and os.path.exists(log_file):
			os.remove(log_file)
		log.init(log_file, config.get("log_level"))
		log.debug("start", "Server booting, port %s." % port_no)
		db.connect()
		cache.connect()
		memory_trace.setup(port_no)

		api.locale.load_translations()
		api.locale.compile_static_language_files()

		if config.get("web_developer_mode"):
			for station_id in config.station_ids:
				playlist.prepare_cooldown_algorithm(station_id)
			# automatically loads every station ID and fills things in if there's no data
			schedule.load()
			for station_id in config.station_ids:
				schedule.update_memcache(station_id)
				rainwave.request.update_line(station_id)
				rainwave.request.update_expire_times()
				cache.set_station(station_id, "backend_ok", True)
				cache.set_station(station_id, "backend_message", "OK")
				cache.set_station(station_id, "get_next_socket_timeout", False)

		for sid in config.station_ids:
			cache.update_local_cache_for_sid(sid)
			playlist.prepare_cooldown_algorithm(sid)
			playlist.update_num_songs()

		# If we're not in developer, remove development-related URLs
		if not config.get("developer_mode"):
			i = 0
			while (i < len(request_classes)):
				if request_classes[i][0].find("/test/") != -1:
					request_classes.pop(i)
					i = i - 1
				i = i + 1

		# Make sure all other errors get handled in an API-friendly way
		request_classes.append((r"/api/.*", api.web.Error404Handler))
		request_classes.append((r"/api4/.*", api.web.Error404Handler))
		request_classes.append((r".*", api.web.HTMLError404Handler))

		# Initialize the help (rather than it scan all URL handlers every time someone hits it)
		api.help.sectionize_requests()

		# Fire ze missiles!
		global app
		app = tornado.web.Application(request_classes,
			debug=(config.test_mode or config.get("developer_mode")),
			template_path=os.path.join(os.path.dirname(__file__), "../templates"),
			static_path=os.path.join(os.path.dirname(__file__), "../static"),
			autoescape=None)
		http_server = tornado.httpserver.HTTPServer(app, xheaders = True)
		http_server.listen(port_no)

		if config.get("api_user") and config.get("api_group"):
			chuser.change_user(config.get("api_user"), config.get("api_group"))

		for request in request_classes:
			log.debug("start", "   Handler: %s" % str(request))
		log.info("start", "API server on port %s ready to go." % port_no)
		self.ioloop = tornado.ioloop.IOLoop.instance()

		try:
			self.ioloop.start()
		finally:
			self.ioloop.stop()
			http_server.stop()
			db.close()
			log.info("stop", "Server has been shutdown.")
			log.close()
예제 #47
0
def exec_test(config_data):

    add_test_info = AddTestInfo(17, '\napi/v2/user\n' 'api/v2/user/<pk>')
    add_test_info.started_info()

    try:
        test = Test(**config_data)

        # --------------- get users --------------

        response = test.get(test.user_url)

        clean_response(response)

        # --------------- create new users --------------

        new_user = UserCreationDefination()

        new_user.username = names.get_first_name().lower()
        new_user.email = '*****@*****.**'
        new_user.password = '******'

        log.info('new username: %s' % new_user.username)

        response = test.post(test.user_url, new_user.__dict__)

        new_user_created = clean_response(response)

        new_uid = new_user_created['id']

        logged_out = test.logout()

        assert logged_out, "logout failed"

        # ------------- edit the user details by logging back as the new user ---------------

        new_config_data = config_data.copy()

        new_config_data['username'] = new_user.username
        new_config_data['password'] = new_user.password

        test2 = Test(**new_config_data)

        edit = UserCreationDefination()

        edit.email = '*****@*****.**'

        response = test2.patch(test.user_url + "/" + str(new_uid),
                               edit.__dict__)

        clean_response(response)

        test2.logout()

        # --------------- delete the created user ---------------

        test3 = Test(**config_data)

        response = test3.delete(test3.user_url + "/" + str(new_uid))

        clean_response(response)

        response = test3.get(test2.user_url)

        clean_response(response)

        add_test_info.success('test ok')

    except Exception:
        log.error('\n%s' % traceback.format_exc())
        add_test_info.failed('test error')

    return add_test_info.completed_info(config_data['log_copy_location'])
예제 #48
0
    except Exception: pass

    try:
        univer = info['university_name']
        print(tab + pct.green.bold('University: ') + univer)

    except Exception: pass

    try:
        fac = info['faculty_name']
        print(tab + pct.green.bold('Faculty: ') + fac)

    except Exception: pass

    print()
    print(tab + pct.white.bold('You can see more public information at ' + link))

    print()
    print(frame)


if __name__ == '__main__':
    try:
        main()

    except KeyboardInterrupt:
        print()
        log.info('Interrupted by user')
        exit(0)
예제 #49
0
파일: web.py 프로젝트: Dinir/rainwave
    def prepare(self):
        if self.local_only and not self.request.remote_ip in config.get(
                "api_trusted_ip_addresses"):
            log.info(
                "api", "Rejected %s request from %s, untrusted address." %
                (self.url, self.request.remote_ip))
            raise APIException(
                "rejected", text="You are not coming from a trusted address.")

        if not isinstance(self.locale, locale.RainwaveLocale):
            self.locale = self.get_browser_locale()

        if not self.return_name:
            self.return_name = self.url[self.url.rfind("/") + 1:] + "_result"
        else:
            self.return_name = self.return_name

        if self.admin_required or self.dj_required:
            self.login_required = True

        if 'in_order' in self.request.arguments:
            self._output = []
            self._output_array = True
        else:
            self._output = {}

        self.sid = fieldtypes.integer(self.get_cookie("r4_sid", None))
        hostname = self.request.headers.get('Host', None)
        if hostname:
            hostname = unicode(hostname).split(":")[0]
            if hostname in config.station_hostnames:
                self.sid = config.station_hostnames[hostname]
        self.sid = fieldtypes.integer(self.get_argument("sid",
                                                        None)) or self.sid
        if self.sid and not self.sid in config.station_ids:
            self.sid = None
        if not self.sid and self.sid_required:
            raise APIException("missing_station_id", http_code=400)

        for field, field_attribs in self.__class__.fields.iteritems():
            type_cast, required = field_attribs
            if required and field not in self.request.arguments:
                raise APIException("missing_argument",
                                   argument=field,
                                   http_code=400)
            elif not required and field not in self.request.arguments:
                self.cleaned_args[field] = None
            else:
                parsed = type_cast(self.get_argument(field), self)
                if parsed == None and required != None:
                    raise APIException(
                        "invalid_argument",
                        argument=field,
                        reason="%s %s" %
                        (field,
                         getattr(fieldtypes, "%s_error" % type_cast.__name__)),
                        http_code=400)
                else:
                    self.cleaned_args[field] = parsed

        if not self.sid and not self.sid_required:
            self.sid = 5
        if not self.sid in config.station_ids:
            raise APIException("invalid_station_id", http_code=400)
        self.set_cookie("r4_sid",
                        str(self.sid),
                        expires_days=365,
                        domain=config.get("cookie_domain"))

        if self.phpbb_auth:
            self.do_phpbb_auth()
        else:
            self.rainwave_auth()

        if not self.user and self.auth_required:
            raise APIException("auth_required", http_code=403)
        elif not self.user and not self.auth_required:
            self.user = User(1)
            self.user.ip_address = self.request.remote_ip

        self.user.refresh(self.sid)

        if self.login_required and (not self.user or self.user.is_anonymous()):
            raise APIException("login_required", http_code=403)
        if self.tunein_required and (not self.user
                                     or not self.user.is_tunedin()):
            raise APIException("tunein_required", http_code=403)
        if self.admin_required and (not self.user or not self.user.is_admin()):
            raise APIException("admin_required", http_code=403)
        if self.dj_required and (not self.user or not self.user.is_dj()):
            raise APIException("dj_required", http_code=403)
        if self.perks_required and (not self.user
                                    or not self.user.has_perks()):
            raise APIException("perks_required", http_code=403)

        if self.unlocked_listener_only and not self.user:
            raise APIException("auth_required", http_code=403)
        elif self.unlocked_listener_only and self.user.data[
                'lock'] and self.user.data['lock_sid'] != self.sid:
            raise APIException(
                "unlocked_only",
                station=config.station_id_friendly[self.user.data['lock_sid']],
                lock_counter=self.user.data['lock_counter'],
                http_code=403)
예제 #50
0
 def success(self, status):
     self.msg = "success"
     log.info('**********  %s  *********' % status)
     print '**********  %s  *********' % status
     self.status = True
예제 #51
0
 def failed(self, status):
     self.msg = "failed"
     log.info('!!!!!!!!!!! %s !!!!!!!!!!!!' % status)
     print '!!!!!!!!!!! %s !!!!!!!!!!!!' % status
     self.status = False
예제 #52
0
    async def register_and_login(self, token: str, destination: str):
        discord_user = await self.oauth2_request(
            "https://discord.com/api/users/@me", access_token=token)

        radio_username = discord_user['username']
        discord_user_id = discord_user['id']
        user_avatar = f"https://cdn.discordapp.com/avatars/{discord_user_id}/{discord_user['avatar']}.png?size=320"
        user_avatar_type = "avatar.driver.remote"
        user_id = 1
        username = str(uuid.uuid4())
        if self.user.id > 1:
            user_id = self.user.id
            radio_username = self.user.data['name']
            username = self.user.data['name']
            log.debug(
                "discord",
                f"Connected legacy phpBB {user_id} to Discord {discord_user_id}"
            )
        else:
            user_id = self.get_user_id_by_discord_user_id(discord_user_id)
            if user_id > 1:
                log.debug(
                    "discord",
                    f"Connected linked phpBB {user_id} to Discord {discord_user_id}"
                )
            else:
                log.debug(
                    "discord",
                    f"Could not find existing user for Discord {discord_user_id}"
                )

        if user_id > 1:
            log.info(
                "discord",
                f"Updating exising user {user_id} from Discord {discord_user_id}"
            )
            db.c.update(
                ("UPDATE phpbb_users SET "
                 "  discord_user_id = %s, "
                 "  radio_username = %s, "
                 "  user_avatar_type = %s, "
                 "  user_avatar = %s, "
                 "  user_password = '', "
                 "  user_email = '', "
                 "  user_email_hash = 0 "
                 "WHERE user_id = %s"),
                (
                    discord_user_id,
                    radio_username,
                    user_avatar_type,
                    user_avatar,
                    user_id,
                ),
            )
        else:
            log.debug("discord",
                      f"Creating new user from Discord {discord_user_id}")
            db.c.update(
                ("INSERT INTO phpbb_users "
                 "  (username, username_clean, discord_user_id, radio_username, user_avatar_type, user_avatar) "
                 "  VALUES "
                 "  (%s      , %s,             %s             , %s            , %s              , %s)"
                 ),
                (
                    username,
                    username,
                    discord_user_id,
                    radio_username,
                    user_avatar_type,
                    user_avatar,
                ),
            )
            user_id = self.get_user_id_by_discord_user_id(discord_user_id)
            log.info(
                "discord",
                f"Created new user {user_id} from Discord {discord_user_id}")

        self.setup_rainwave_session_and_redirect(user_id, destination)
예제 #53
0
def exec_test(config_data):
    add_test_info = AddTestInfo(
        13, '\napi/v2/cluster/<fsid>/pool \n'
        'api/v2/cluster/<fsid>/pool/<pool_id>')
    add_test_info.started_info()

    try:

        pool_name = 'pool_' + "api_testing" + str(random.randint(1, 1000))

        pool_ops = Test(**config_data)

        pool_ops.get(pool_ops.pool_url)

        # ------------ creating pool --------------

        pool_definition = PoolDefination()

        pool_definition.name = pool_name
        pool_definition.size = 3
        pool_definition.pg_num = 64
        pool_definition.crush_ruleset = 0
        pool_definition.min_size = 2
        pool_definition.crash_replay_interval = 0
        pool_definition.pg_num = 64
        pool_definition.hashpspool = True
        pool_definition.quota_max_objects = 0
        pool_definition.quota_max_bytes = 0

        log.debug('pool definition complete')

        log.info('json data \n%s:' % pool_definition.__dict__)

        pool_ops.post(pool_ops.pool_url, pool_definition.__dict__)

        # ------------- editing pool ------------

        pools = pool_ops.get(pool_ops.pool_url)

        my_pool = None

        for pool in pools:
            if pool_definition.name == pool['name']:
                log.debug('got matching pool')
                my_pool = pool
                log.debug(my_pool)
                break

        # asserts if my_pool is none,
        assert my_pool is not None, ("did not find any pool with the name %s" %
                                     pool_definition.name)

        pool_editing = PoolDefination()

        pool_editing.name = pool_name + "_renamed"

        pool_ops.patch(pool_ops.pool_url + "/" + str(my_pool['id']),
                       pool_editing.__dict__)

        # ---------------- deleting pool ---------------

        pool_ops.delete(pool_ops.pool_url + "/" + str(my_pool['id']))

        add_test_info.success('test ok')

    except AssertionError, e:
        log.error(e)
        add_test_info.failed('test error')
예제 #54
0
    try:
        json.loads(req)
        is_valid=True
    except ValueError, e:
        log.err("response is not a valid json")

    if is_valid is True:
        # the magic!
        obj = json.loads(req, object_hook=lambda d: namedtuple('musikker', d.keys())(*d.values()))
        if hasattr(obj, 'tracks'):
            if hasattr(obj.tracks, 'items'):
                total_items = len(obj.tracks.items)
                if total_items > 0:
                    print
                    log.warn("items found:", total_items)
                    log.info('Artist:', obj.tracks.items[0].artists[0].name)
                    log.info('Album:', obj.tracks.items[0].album.name)
                    log.info('Track:', obj.tracks.items[0].name)
                    log.warn('Spotify uri:', obj.tracks.items[0].uri)
                    print
                else:
                    log.warn("response does not have items")
            else:
                log.err("response has no attribute 'items'")
        else:
            log.err("response has no attribute 'tracks'")


def setParams(query):
    q=""
    if 'artist' in query:
예제 #55
0
pr.import_band(dict_list)

#pr.set_gcp_geolocation()
#Geocode TP files & set class property (sza) ... :
#
#pr.set_geometry('TP')
#Geocode Full Image (FI) files :
#pr.set_geometry('FI')

#Define geometric reference (where to find geometry information)
reference = dict_list[0]['Radiance file']
#Set geometry to VZA, VAA, OZA, OAA and Solar Flux file
pr.set_geometry(dict_list[0]['Radiance file'])
#Test : Read the dict list  for the fist record

log.info(' Convert to TOA Reflectance')
for rec in pr.band_dict:
    log.info(' Input files : ')
    log.info('     Band Number               : ' + str(rec['Band']))
    log.info('     Radiance Scaling factor  : ' + str(rec['Scaling factor']))
    log.info('     Radiance file             : ' + rec['Radiance file'])
    log.info('     Solar flux file           : ' + rec['Solar flux file'])
    log.info('     Sun Zenith Angle file     : ' + pr.sza)

    scaling_factor = rec['Scaling factor']
    radiance_image = rec['Radiance file']
    solar_flux_image = rec['Solar flux file']
    sun_zenith_image = pr.sza
    new_name = os.path.join(WD_RES, rec['Radiance file'].replace('geo', 'toa'))
    log.info('     Output TOA File     : ' + new_name)
예제 #56
0
	def prepare(self):
		if self.local_only and not self.request.remote_ip in config.get("api_trusted_ip_addresses"):
			log.info("api", "Rejected %s request from %s, untrusted address." % (self.url, self.request.remote_ip))
			raise APIException("rejected", text="You are not coming from a trusted address.")

		if not isinstance(self.locale, locale.RainwaveLocale):
			self.locale = self.get_browser_locale()

		if not self.return_name:
			self.return_name = self.url[self.url.rfind("/")+1:] + "_result"
		else:
			self.return_name = self.return_name

		if self.admin_required or self.dj_required:
			self.login_required = True

		if 'in_order' in self.request.arguments:
			self._output = []
			self._output_array = True
		else:
			self._output = {}

		self.sid = fieldtypes.integer(self.get_cookie("r4_sid", None))
		hostname = self.request.headers.get('Host', None)
		if hostname:
			hostname = unicode(hostname).split(":")[0]
			if hostname in config.station_hostnames:
				self.sid = config.station_hostnames[hostname]
		self.sid = fieldtypes.integer(self.get_argument("sid", None)) or self.sid
		if self.sid and not self.sid in config.station_ids:
			self.sid = None
		if not self.sid and self.sid_required:
			raise APIException("missing_station_id", http_code=400)

		for field, field_attribs in self.__class__.fields.iteritems():
			type_cast, required = field_attribs
			if required and field not in self.request.arguments:
				raise APIException("missing_argument", argument=field, http_code=400)
			elif not required and field not in self.request.arguments:
				self.cleaned_args[field] = None
			else:
				parsed = type_cast(self.get_argument(field), self)
				if parsed == None and required != None:
					raise APIException("invalid_argument", argument=field, reason="%s %s" % (field, getattr(fieldtypes, "%s_error" % type_cast.__name__)), http_code=400)
				else:
					self.cleaned_args[field] = parsed

		if not self.sid and not self.sid_required:
			self.sid = 5
		if not self.sid in config.station_ids:
			raise APIException("invalid_station_id", http_code=400)
		self.set_cookie("r4_sid", str(self.sid), expires_days=365, domain=config.get("cookie_domain"))

		if self.phpbb_auth:
			self.do_phpbb_auth()
		else:
			self.rainwave_auth()

		if not self.user and self.auth_required:
			raise APIException("auth_required", http_code=403)
		elif not self.user and not self.auth_required:
			self.user = User(1)
			self.user.ip_address = self.request.remote_ip
		
		self.user.refresh(self.sid)

		if self.login_required and (not self.user or self.user.is_anonymous()):
			raise APIException("login_required", http_code=403)
		if self.tunein_required and (not self.user or not self.user.is_tunedin()):
			raise APIException("tunein_required", http_code=403)
		if self.admin_required and (not self.user or not self.user.is_admin()):
			raise APIException("admin_required", http_code=403)
		if self.dj_required and (not self.user or not self.user.is_dj()):
			raise APIException("dj_required", http_code=403)
		if self.perks_required and (not self.user or not self.user.has_perks()):
			raise APIException("perks_required", http_code=403)

		if self.unlocked_listener_only and not self.user:
			raise APIException("auth_required", http_code=403)
		elif self.unlocked_listener_only and self.user.data['lock'] and self.user.data['lock_sid'] != self.sid:
			raise APIException("unlocked_only", station=config.station_id_friendly[self.user.data['lock_sid']], lock_counter=self.user.data['lock_counter'], http_code=403)
예제 #57
0
def makeDir(dirName = None):
    if not os.path.exists(dirName):
        os.makedirs(dirName)
        log.info("mkdir %s"%dirName)
    else:
        log.error("La carpeta [%s] ya exíste"%dirName)
예제 #58
0
파일: web.py 프로젝트: blorp77/MetaFolder
	def prepare(self):
		if self.local_only and not self.request.remote_ip in config.get("api_trusted_ip_addresses"):
			log.info("api", "Rejected %s request from %s" % (self.url, self.request.remote_ip))
			self.set_status(403)
			self.finish()

		# Tornado doesn't want to read the JSON cookies at all; have to look into why and work around it.
		#if self.get_cookie("r3prefs"):
		#	try:
		#		self.cookie_prefs = tornado.escape.json_decode(self.get_cookie("r3prefs"))
		#	except Exception as e:
		#		print self.cookies
		#		print self.get_cookie("r3prefs")
		#		print e

		if not isinstance(self.locale, locale.RainwaveLocale):
			self.locale = self.get_browser_locale()

		if not self.return_name:
			self.return_name = self.url[self.url.rfind("/")+1:] + "_result"
		else:
			self.return_name = self.return_name

		if self.admin_required or self.dj_required:
			self.login_required = True

		if 'in_order' in self.request.arguments:
			self._output = []
			self._output_array = True
		else:
			self._output = {}

		if not self.sid:
			self.sid = fieldtypes.integer(self.get_cookie("r4_sid", "5"))
		if "sid" in self.request.arguments:
			possible_sid = fieldtypes.integer(self.get_argument("sid"))
			if possible_sid in config.station_ids:
				self.sid = possible_sid
		if not self.sid in config.station_ids:
			self.sid = None
		if not self.sid:
			host = self.request.headers.get('Host', 'game.rainwave.cc')
			for possible_sid in config.station_ids:
				if host == config.get_station(possible_sid, "host"):
					self.sid = possible_sid
					break
		if not self.sid and self.sid_required:
			raise APIException("missing_station_id", http_code=400)
		elif not self.sid:
			self.sid = 5
		if self.sid and not self.sid in config.station_ids:
			raise APIException("invalid_station_id", http_code=400)
		self.set_cookie("r4_sid", str(self.sid), expires_days=365, domain=config.get("cookie_domain"))

		for field, field_attribs in self.__class__.fields.iteritems():
			type_cast, required = field_attribs
			if required and field not in self.request.arguments:
				raise APIException("missing_argument", argument=field, http_code=400)
			elif not required and field not in self.request.arguments:
				self.cleaned_args[field] = None
			else:
				parsed = type_cast(self.get_argument(field), self)
				if parsed == None and required != None:
					raise APIException("invalid_argument", argument=field, reason="%s %s" % (field, getattr(fieldtypes, "%s_error" % type_cast.__name__)), http_code=400)
				else:
					self.cleaned_args[field] = parsed

		if self.phpbb_auth:
			self.do_phpbb_auth()
		else:
			self.rainwave_auth()

		if self.auth_required and not self.user:
			raise APIException("auth_required", http_code=403)

		if self.login_required and (not self.user or self.user.is_anonymous()):
			raise APIException("login_required", http_code=403)
		if self.tunein_required and (not self.user or not self.user.is_tunedin()):
			raise APIException("tunein_required", http_code=403)
		if self.admin_required and (not self.user or not self.user.is_admin()):
			raise APIException("admin_required", http_code=403)
		if self.dj_required and (not self.user or not self.user.is_dj()):
			raise APIException("dj_required", http_code=403)
		if self.perks_required and (not self.user or not self.user.has_perks()):
			raise APIException("perks_required", http_code=403)

		if self.unlocked_listener_only and not self.user:
			raise APIException("auth_required", http_code=403)
		elif self.unlocked_listener_only and self.user.data['lock'] and self.user.data['lock_sid'] != self.sid:
			raise APIException("unlocked_only", station=config.station_id_friendly[self.user.data['lock_sid']], lock_counter=self.user.data['lock_counter'], http_code=403)