示例#1
0
def _get_socket(sid):
	client = None
	if hasattr(socket,"AF_UNIX") and config.has_station(sid, "liquidsoap_socket_path") and os.path.exists(config.get_station(sid, "liquidsoap_socket_path")):
		client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
		client.connect(config.get_station(sid, "liquidsoap_socket_path"))
	elif config.has_station(sid, "liquidsoap_telnet_host"):
		client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    	client.settimeout(2)
    	client.connect((config.get_station(sid, "liquidsoap_telnet_host"), config.get_station(sid, "liquidsoap_telnet_port")))
	return client
示例#2
0
def _get_socket(sid):
    client = None
    if hasattr(socket, "AF_UNIX") and config.has_station(
            sid, "liquidsoap_socket_path") and os.path.exists(
                config.get_station(sid, "liquidsoap_socket_path")):
        client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        client.connect(config.get_station(sid, "liquidsoap_socket_path"))
    elif config.has_station(sid, "liquidsoap_telnet_host"):
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.settimeout(2)
    client.connect((config.get_station(sid, "liquidsoap_telnet_host"),
                    config.get_station(sid, "liquidsoap_telnet_port")))
    return client
示例#3
0
def _send_command(sid, cmd):
	if not config.has_station(sid, "liquidsoap_socket_path") and not config.has_station(sid, "liquidsoap_telnet_host"):
		return ""

	client = _get_socket(sid)
	if not client:
		return ""

	cmd += "\n"
	client.send(cmd)
	to_ret = client.recv(1024)
	client.send("exit")
	client.close()
	to_ret = to_ret.strip().strip("END").strip()
	return to_ret
示例#4
0
	def _start_cooldown_db(self, sid, cool_time):
		if config.has_station(sid, "cooldown_enable_for_categories") and not config.get_station(sid, "cooldown_enable_for_categories"):
			return

		cool_end = int(cool_time + timestamp())
		log.debug("cooldown", "Group ID %s Station ID %s cool_time period: %s" % (self.id, sid, cool_time))
		# Make sure to update both the if and else SQL statements if doing any updates
		if db.c.allows_join_on_update:
			db.c.update("UPDATE r4_song_sid SET song_cool = TRUE, song_cool_end = %s "
						"FROM r4_song_group "
						"WHERE r4_song_sid.song_id = r4_song_group.song_id AND r4_song_group.group_id = %s "
							"AND r4_song_sid.sid = %s AND r4_song_sid.song_exists = TRUE AND r4_song_sid.song_cool_end <= %s ",
						(cool_end, self.id, sid, cool_end))
			request_only_end = cool_end + 300
			db.c.update("UPDATE r4_song_sid SET song_request_only = TRUE, song_request_only_end = %s "
						"FROM r4_song_group "
						"WHERE r4_song_sid.song_id = r4_song_group.song_id AND r4_song_group.group_id = %s "
							"AND r4_song_sid.sid = %s AND r4_song_sid.song_exists = TRUE AND r4_song_sid.song_cool_end <= %s "
							"AND song_request_only_end IS NOT NULL",
						(request_only_end, self.id, sid, cool_end))
		else:
			song_ids = db.c.fetch_list(
				"SELECT song_id "
				"FROM r4_song_group JOIN r4_song_sid USING (song_id) "
				"WHERE r4_song_group.group_id = %s AND r4_song_sid.sid = %s AND r4_song_sid.song_exists = TRUE AND r4_song_sid.song_cool_end < %s",
				(self.id, sid, timestamp() - cool_time))
			for song_id in song_ids:
				db.c.update("UPDATE r4_song_sid SET song_cool = TRUE, song_cool_end = %s WHERE song_id = %s AND sid = %s", (cool_end, song_id, sid))
示例#5
0
    def _start_cooldown_db(self, sid, cool_time):
        if config.has_station(
                sid,
                "cooldown_enable_for_categories") and not config.get_station(
                    sid, "cooldown_enable_for_categories"):
            return

        cool_end = int(cool_time + timestamp())
        log.debug(
            "cooldown",
            "Group ID %s Station ID %s cool_time period: %s" %
            (self.id, sid, cool_time),
        )
        # Make sure to update both the if and else SQL statements if doing any updates
        db.c.update(
            "UPDATE r4_song_sid SET song_cool = TRUE, song_cool_end = %s "
            "FROM r4_song_group "
            "WHERE r4_song_sid.song_id = r4_song_group.song_id AND r4_song_group.group_id = %s "
            "AND r4_song_sid.sid = %s AND r4_song_sid.song_exists = TRUE AND r4_song_sid.song_cool_end <= %s ",
            (cool_end, self.id, sid, cool_end),
        )
        request_only_end = cool_end + 300
        db.c.update(
            "UPDATE r4_song_sid SET song_request_only = TRUE, song_request_only_end = %s "
            "FROM r4_song_group "
            "WHERE r4_song_sid.song_id = r4_song_group.song_id AND r4_song_group.group_id = %s "
            "AND r4_song_sid.sid = %s AND r4_song_sid.song_exists = TRUE AND r4_song_sid.song_cool_end <= %s "
            "AND song_request_only_end IS NOT NULL",
            (request_only_end, self.id, sid, cool_end),
        )
示例#6
0
	def _start_cooldown_db(self, sid, cool_time):
		if config.has_station(sid, "cooldown_enable_for_categories") and not config.get_station(sid, "cooldown_enable_for_categories"):
			return

		cool_end = int(cool_time + timestamp())
		log.debug("cooldown", "Group ID %s Station ID %s cool_time period: %s" % (self.id, sid, cool_time))
		# Make sure to update both the if and else SQL statements if doing any updates
		if db.c.allows_join_on_update:
			db.c.update("UPDATE r4_song_sid SET song_cool = TRUE, song_cool_end = %s "
						"FROM r4_song_group "
						"WHERE r4_song_sid.song_id = r4_song_group.song_id AND r4_song_group.group_id = %s "
							"AND r4_song_sid.sid = %s AND r4_song_sid.song_exists = TRUE AND r4_song_sid.song_cool_end <= %s ",
						(cool_end, self.id, sid, cool_end))
			request_only_end = cool_end + 300
			db.c.update("UPDATE r4_song_sid SET song_request_only = TRUE, song_request_only_end = %s "
						"FROM r4_song_group "
						"WHERE r4_song_sid.song_id = r4_song_group.song_id AND r4_song_group.group_id = %s "
							"AND r4_song_sid.sid = %s AND r4_song_sid.song_exists = TRUE AND r4_song_sid.song_cool_end <= %s "
							"AND song_request_only_end IS NOT NULL",
						(request_only_end, self.id, sid, cool_end))
		else:
			song_ids = db.c.fetch_list(
				"SELECT song_id "
				"FROM r4_song_group JOIN r4_song_sid USING (song_id) "
				"WHERE r4_song_group.group_id = %s AND r4_song_sid.sid = %s AND r4_song_sid.song_exists = TRUE AND r4_song_sid.song_cool_end < %s",
				(self.id, sid, timestamp() - cool_time))
			for song_id in song_ids:
				db.c.update("UPDATE r4_song_sid SET song_cool = TRUE, song_cool_end = %s WHERE song_id = %s AND sid = %s", (cool_end, song_id, sid))
示例#7
0
def _send_command(sid, cmd):
    if not config.has_station(
            sid, "liquidsoap_socket_path") and not config.has_station(
                sid, "liquidsoap_telnet_host"):
        return ""

    client = _get_socket(sid)
    if not client:
        return ""

    cmd += "\n"
    client.send(cmd)
    to_ret = client.recv(1024)
    client.send("exit")
    client.close()
    to_ret = to_ret.strip().strip("END").strip()
    return to_ret
示例#8
0
def _send_command(sid, cmd):
    if not config.has_station(sid, "liquidsoap_socket_path"):
        return ""
    cmd += "\n"
    socket_path = config.get_station(sid, "liquidsoap_socket_path")
    if os.path.exists(socket_path):
        client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        client.connect(socket_path)
        client.send(cmd)
        to_ret = client.recv(1024)
        client.send("exit")
        client.close()
        to_ret = to_ret.strip().strip("END").strip()
        return to_ret
    else:
        raise APIException("internal_error", "Could not connect to Liquidsoap. (station %s)" % sid)
示例#9
0
def _send_command(sid, cmd):
    if not config.has_station(sid, "liquidsoap_socket_path"):
        return ""
    cmd += "\n"
    socket_path = config.get_station(sid, "liquidsoap_socket_path")
    if os.path.exists(socket_path):
        client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        client.connect(socket_path)
        client.send(cmd)
        to_ret = client.recv(1024)
        client.send("exit")
        client.close()
        to_ret = to_ret.strip().strip("END").strip()
        return to_ret
    else:
        raise APIException(
            "internal_error",
            "Could not connect to Liquidsoap. (station %s)" % sid)
示例#10
0
def post_process(sid):
    try:
        db.c.start_transaction()
        start_time = timestamp()
        playlist.prepare_cooldown_algorithm(sid)
        rainwave.playlist_objects.album.clear_updated_albums(sid)
        log.debug("post", "Playlist prepare time: %.6f" % (timestamp() - start_time,))

        start_time = timestamp()
        current[sid].finish()
        for sched_id in db.c.fetch_list(
            "SELECT sched_id FROM r4_schedule WHERE sched_end < %s AND sched_used = FALSE",
            (timestamp(),),
        ):
            t_evt = events.event.BaseProducer.load_producer_by_id(sched_id)
            t_evt.finish()
        log.debug("post", "Current finish time: %.6f" % (timestamp() - start_time,))

        start_time = timestamp()
        last_song = current[sid].get_song()
        if last_song:
            db.c.update(
                "INSERT INTO r4_song_history (sid, song_id) VALUES (%s, %s)",
                (sid, last_song.id),
            )
        log.debug("post", "Last song insertion time: %s" % (timestamp() - start_time,))

        start_time = timestamp()
        history[sid].insert(0, current[sid])
        while len(history[sid]) > 5:
            history[sid].pop()
        log.debug("post", "History management: %.6f" % (timestamp() - start_time,))

        start_time = timestamp()
        current[sid] = upnext[sid].pop(0)
        current[sid].start_event()
        log.debug("advance", "Current management: %.6f" % (timestamp() - start_time,))

        start_time = timestamp()
        playlist.warm_cooled_songs(sid)
        playlist.warm_cooled_albums(sid)
        log.debug("advance", "Cooldown warming: %.6f" % (timestamp() - start_time,))

        start_time = timestamp()
        _trim(sid)
        user.trim_listeners(sid)
        cache.update_user_rating_acl(sid, history[sid][0].get_song().id)
        user.unlock_listeners(sid)
        db.c.update(
            "UPDATE r4_listeners SET listener_voted_entry = NULL WHERE sid = %s", (sid,)
        )
        log.debug(
            "advance",
            "User management and trimming: %.6f" % (timestamp() - start_time,),
        )

        start_time = timestamp()
        # reduce song blocks has to come first, otherwise it wll reduce blocks generated by _create_elections
        playlist.reduce_song_blocks(sid)
        # update_cache updates both the line and expiry times
        # this is expensive and must be done before and after every request is filled
        # DO THIS AFTER EVERYTHING ELSE, RIGHT BEFORE NEXT MANAGEMENT, OR PEOPLE'S REQUESTS SLIP THROUGH THE CRACKS
        request.update_line(sid)
        # add to the event list / update start times for events
        manage_next(sid)
        # update expire times AFTER manage_next, so people who aren't in line anymore don't see expiry times
        request.update_expire_times()
        log.debug(
            "advance",
            "Request and upnext management: %.6f" % (timestamp() - start_time,),
        )

        update_memcache(sid)

        sync_to_front.sync_frontend_all(sid)
        db.c.commit()
    except:
        db.c.rollback()
        raise

    if (
        current[sid]
        and config.has_station(sid, "tunein_partner_key")
        and config.get_station(sid, "tunein_partner_key")
    ):
        ti_song = current[sid].get_song()
        if ti_song:
            ti_title = ti_song.data["title"]
            ti_album = ti_song.albums[0].data["name"]
            ti_artist = ", ".join([a.data["name"] for a in ti_song.artists])

            params = {
                "id": config.get_station(sid, "tunein_id"),
                "title": ti_title,
                "artist": ti_artist,
                "album": ti_album,
            }

            try:
                req = requests.Request(
                    "GET", "http://air.radiotime.com/Playing.ashx", params=params
                )
                p = req.prepare()
                # Must be done here rather than in params because of odd strings TuneIn creates
                p.url += "&partnerId=%s" % config.get_station(sid, "tunein_partner_id")
                p.url += "&partnerKey=%s" % config.get_station(
                    sid, "tunein_partner_key"
                )
                s = requests.Session()
                resp = s.send(p, timeout=3)
                log.debug(
                    "advance", "TuneIn updated (%s): %s" % (resp.status_code, resp.text)
                )
            except Exception as e:
                log.exception("advance", "Could not update TuneIn.", e)
示例#11
0
def post_process(sid):
    try:
        db.c.start_transaction()
        start_time = timestamp()
        playlist.prepare_cooldown_algorithm(sid)
        rainwave.playlist_objects.album.clear_updated_albums(sid)
        log.debug("post", "Playlist prepare time: %.6f" % (timestamp() - start_time,))

        start_time = timestamp()
        current[sid].finish()
        for sched_id in db.c.fetch_list(
            "SELECT sched_id FROM r4_schedule WHERE sched_end < %s AND sched_used = FALSE", (timestamp(),)
        ):
            t_evt = events.event.BaseProducer.load_producer_by_id(sched_id)
            t_evt.finish()
        log.debug("post", "Current finish time: %.6f" % (timestamp() - start_time,))

        start_time = timestamp()
        last_song = current[sid].get_song()
        if last_song:
            db.c.update("INSERT INTO r4_song_history (sid, song_id) VALUES (%s, %s)", (sid, last_song.id))
        log.debug("post", "Last song insertion time: %s" % (timestamp() - start_time,))

        start_time = timestamp()
        history[sid].insert(0, current[sid])
        while len(history[sid]) > 5:
            history[sid].pop()
        log.debug("post", "History management: %.6f" % (timestamp() - start_time,))

        start_time = timestamp()
        current[sid] = upnext[sid].pop(0)
        current[sid].start_event()
        log.debug("advance", "Current management: %.6f" % (timestamp() - start_time,))

        start_time = timestamp()
        playlist.warm_cooled_songs(sid)
        playlist.warm_cooled_albums(sid)
        log.debug("advance", "Cooldown warming: %.6f" % (timestamp() - start_time,))

        start_time = timestamp()
        _add_listener_count_record(sid)
        _trim(sid)
        user.trim_listeners(sid)
        cache.update_user_rating_acl(sid, history[sid][0].get_song().id)
        user.unlock_listeners(sid)
        db.c.update("UPDATE r4_listeners SET listener_voted_entry = NULL WHERE sid = %s", (sid,))
        log.debug("advance", "User management and trimming: %.6f" % (timestamp() - start_time,))

        start_time = timestamp()
        # reduce song blocks has to come first, otherwise it wll reduce blocks generated by _create_elections
        playlist.reduce_song_blocks(sid)
        # update_cache updates both the line and expiry times
        # this is expensive and must be done before and after every request is filled
        # DO THIS AFTER EVERYTHING ELSE, RIGHT BEFORE NEXT MANAGEMENT, OR PEOPLE'S REQUESTS SLIP THROUGH THE CRACKS
        request.update_line(sid)
        # add to the event list / update start times for events
        manage_next(sid)
        # update expire times AFTER manage_next, so people who aren't in line anymore don't see expiry times
        request.update_expire_times()
        log.debug("advance", "Request and upnext management: %.6f" % (timestamp() - start_time,))

        update_memcache(sid)

        sync_to_front.sync_frontend_all(sid)
        db.c.commit()
    except:
        db.c.rollback()
        raise

    if current[sid] and config.has_station(sid, "tunein_partner_key") and config.get_station(sid, "tunein_partner_key"):
        ti_song = current[sid].get_song()
        if ti_song:
            ti_title = ti_song.data["title"]
            ti_album = ti_song.albums[0].data["name"]
            ti_artist = ", ".join([a.data["name"] for a in ti_song.artists])

            params = {
                "id": config.get_station(sid, "tunein_id"),
                "title": ti_title,
                "artist": ti_artist,
                "album": ti_album,
            }

            try:
                req = requests.Request("GET", "http://air.radiotime.com/Playing.ashx", params=params)
                p = req.prepare()
                # Must be done here rather than in params because of odd strings TuneIn creates
                p.url += "&partnerId=%s" % config.get_station(sid, "tunein_partner_id")
                p.url += "&partnerKey=%s" % config.get_station(sid, "tunein_partner_key")
                s = requests.Session()
                resp = s.send(p, timeout=3)
                log.debug("advance", "TuneIn updated (%s): %s" % (resp.status_code, resp.text))
            except Exception as e:
                log.exception("advance", "Could not update TuneIn.", e)