Ejemplo n.º 1
0
def _add_scan_error(filename, xception, full_exc=None):
    scan_errors = []
    try:
        scan_errors = cache.get("backend_scan_errors")
    except:
        pass
    if not scan_errors:
        scan_errors = []

    eo = {
        "time": int(timestamp()),
        "file": filename,
        "type": xception.__class__.__name__,
        "error": str(xception),
        "traceback": "",
    }
    if (not isinstance(xception, PassableScanError)
            and not isinstance(xception, IOError)
            and not isinstance(xception, OSError)):
        if full_exc:
            eo["traceback"] = traceback.format_exception(*full_exc)
            log.exception("scan", "Error scanning %s" % filename, full_exc)
        else:
            eo["traceback"] = traceback.format_exception(*sys.exc_info())
            log.exception("scan", "Error scanning %s" % filename,
                          sys.exc_info())
    else:
        log.warn("scan", "Warning scanning %s: %s" % (filename, xception))
    scan_errors.insert(0, eo)
    if len(scan_errors) > 100:
        scan_errors = scan_errors[0:100]
    cache.set_global("backend_scan_errors", scan_errors)
Ejemplo n.º 2
0
def update_expire_times():
    expiry_times = {}
    for row in db.c.fetch_all("SELECT * FROM r4_request_line"):
        expiry_times[row["user_id"]] = None
        if not row["line_expiry_tune_in"] and not row["line_expiry_election"]:
            pass
        elif row["line_expiry_tune_in"] and not row["line_expiry_election"]:
            expiry_times[row["user_id"]] = row["line_expiry_tune_in"]
        elif row["line_expiry_election"] and not row["line_expiry_tune_in"]:
            expiry_times[row["user_id"]] = row["line_expiry_election"]
        elif row["line_expiry_election"] <= row["line_expiry_tune_in"]:
            expiry_times[row["user_id"]] = row["line_expiry_election"]
        else:
            expiry_times[row["user_id"]] = row["line_expiry_tune_in"]
    cache.set_global("request_expire_times", expiry_times, True)
Ejemplo n.º 3
0
def _cache_relay_status():
    global in_process

    relays = {}
    for relay, _relay_info in config.get("relays").items():
        relays[relay] = 0

    for handler, data in in_process.items():
        if isinstance(data, list):
            relays[handler.relay_name] += len(data)

    for relay, count in relays.items():
        log.debug("icecast_sync", "%s total listeners: %s" % (relay, count))

    cache.set_global("relay_status", relays)
Ejemplo n.º 4
0
    def post(self):
        # limit size of submission
        for k, v in self.cleaned_args.items():
            if isinstance(object, str):
                self.cleaned_args[k] = v[:2048]
        self.cleaned_args["user_id"] = self.user.id
        self.cleaned_args["username"] = self.user.data["name"]
        self.cleaned_args["time"] = time.time()

        reports = cache.get("error_reports")
        if not isinstance(reports, list):
            reports = []

        while len(reports) > 30:
            reports.pop()

        reports.insert(0, self.cleaned_args)
        cache.set_global("error_reports", reports)

        self.append_standard("report_submitted", "Error report submitted.")
Ejemplo n.º 5
0
    def ensure_api_key(self):
        if self.id == 1:
            if self.data.get("api_key") and self.data["listen_key"]:
                return self.data["api_key"]
            api_key = self.generate_api_key(
                int(timestamp()) + 172800, self.data.get("api_key"))
            cache_key = unicodedata.normalize(
                "NFKD",
                u"api_key_listen_key_%s" % api_key).encode("ascii", "ignore")
            cache.set_global(cache_key, self.data["listen_key"])
        elif self.id > 1:
            if "api_key" in self.data and self.data["api_key"]:
                return self.data["api_key"]

            api_key = db.c.fetch_var(
                "SELECT api_key FROM r4_api_keys WHERE user_id = %s",
                (self.id, ))
            if not api_key:
                api_key = self.generate_api_key()
        self.data["api_key"] = api_key

        return api_key
Ejemplo n.º 6
0
async def _start():
    loop = asyncio.get_running_loop()

    stream_names = {}
    for sid in config.station_ids:
        stream_names[sid] = config.get_station(sid, "stream_filename")

    calls = []
    requests = []
    clients = []
    for relay, relay_info in config.get("relays").items():
        client = aiohttp.ClientSession(
            loop=loop,
            timeout=aiohttp.ClientTimeout(total=5),
            auth=aiohttp.BasicAuth(
                login=relay_info["admin_username"],
                password=relay_info["admin_password"],
            ))
        clients.append(client)
        relay_base_url = "%s%s:%s/admin/listclients?mount=/" % (
            relay_info["protocol"],
            relay_info["ip_address"],
            relay_info["port"],
        )
        for sid in relay_info["sids"]:
            for ftype in (".mp3", ".ogg"):
                call = IcecastSyncCall(relay, relay_info, ftype, sid)
                calls.append(call)
                requests.append(
                    call.request(
                        client=client,
                        url=relay_base_url + stream_names[sid] + ftype,
                    ))

    try:
        await asyncio.gather(*requests)
    finally:
        for client in clients:
            await client.close()

    log.debug("icecast_sync", "All responses came back for counting.")

    try:
        stations = {}
        for sid in config.station_ids:
            stations[sid] = 0

        relays = {}
        for relay, _relay_info in config.get("relays").items():
            relays[relay] = 0

        for call in calls:
            listeners = call.get_listeners()
            stations[call.sid] += len(listeners)
            relays[call.relay_name] += len(listeners)

        for sid, listener_count in stations.items():
            log.debug(
                "icecast_sync",
                "%s has %s listeners." %
                (config.station_id_friendly[sid], listener_count),
            )
            db.c.update(
                "INSERT INTO r4_listener_counts (sid, lc_guests) VALUES (%s, %s)",
                (sid, listener_count),
            )

        for relay, count in relays.items():
            log.debug("icecast_sync",
                      "%s total listeners: %s" % (relay, count))

        cache.set_global("relay_status", relays)

        # db.c.update("DELETE FROM r4_listener_counts WHERE lc_time <= %s", (current_time - config.get("trim_history_length"),))
    except Exception as e:
        log.exception("icecast_sync", "Could not finish counting listeners.",
                      e)