def post(self): args = self.post_parser.parse_args() try: pajbot.web.utils.pleblist_login(args["password"], app.bot_config) except pajbot.exc.InvalidLogin as e: return {"error": str(e)}, 401 with DBManager.create_session_scope() as session: youtube_id = args["youtube_id"] current_stream = session.query(Stream).filter_by( ended=False).order_by(Stream.stream_start).first() if current_stream is None: return {"error": "Stream offline"}, 400 skip_after = args["skip_after"] log.info(f"Request song youtube ID: {youtube_id}") song_requested = PleblistSong(current_stream.id, youtube_id, skip_after=skip_after) session.add(song_requested) song_info = session.query(PleblistSongInfo).filter_by( pleblist_song_youtube_id=youtube_id).first() if song_info is None and song_requested.song_info is None: PleblistManager.init( app.bot_config["youtube"]["developer_key"]) song_info = PleblistManager.create_pleblist_song_info( song_requested.youtube_id) if song_info is not False: session.add(song_info) session.commit() return {"success": "got em!"}, 200
def post(self): args = self.post_parser.parse_args() with DBManager.create_session_scope() as session: youtube_id = args["youtube_id"] log.info("Validating youtube ID {}".format(youtube_id)) song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first() if song_info is not None: return {"message": "success", "song_info": song_info.jsonify()} PleblistManager.init(app.bot_config["youtube"]["developer_key"]) song_info = PleblistManager.create_pleblist_song_info(youtube_id) if not song_info and len(youtube_id) > 11: youtube_id = youtube_id[:11] song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first() if song_info is not None: return {"message": "success", "new_youtube_id": youtube_id, "song_info": song_info.jsonify()} else: song_info = PleblistManager.create_pleblist_song_info(youtube_id) if song_info: log.debug(song_info) session.add(song_info) session.commit() return {"message": "success", "new_youtube_id": youtube_id, "song_info": song_info.jsonify()} return {"message": "invalid youtube id", "song_info": None}
def pleblist_validate(): if not request.method == "POST": return make_response(jsonify({"error": "Invalid request method"}), 405) if "youtube_id" not in request.form: return make_response(jsonify({"error": "Missing data youtube_id"}), 400) with DBManager.create_session_scope() as session: youtube_id = request.form["youtube_id"] print(youtube_id) song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first() if song_info is not None: return jsonify({"message": "success", "song_info": song_info.jsonify()}) PleblistManager.init(config["youtube"]["developer_key"]) song_info = PleblistManager.create_pleblist_song_info(youtube_id) if not song_info and len(youtube_id) > 11: youtube_id = youtube_id[:11] song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first() if song_info is not None: return jsonify({"message": "success", "new_youtube_id": youtube_id, "song_info": song_info.jsonify()}) else: song_info = PleblistManager.create_pleblist_song_info(youtube_id) if song_info: print(song_info) session.add(song_info) session.commit() return jsonify({"message": "success", "new_youtube_id": youtube_id, "song_info": song_info.jsonify()}) return jsonify({"message": "invalid youtube id", "song_info": None})
def post(self): args = self.post_parser.parse_args() try: pajbot.web.utils.pleblist_login(args['password'], app.bot_config) except pajbot.exc.InvalidLogin as e: return {'error': str(e)}, 401 with DBManager.create_session_scope() as session: youtube_id = args['youtube_id'] current_stream = session.query(Stream).filter_by( ended=False).order_by(Stream.stream_start).first() if current_stream is None: return {'error': 'Stream offline'}, 400 skip_after = args['skip_after'] log.info('Request song youtube ID: {}'.format(youtube_id)) song_requested = PleblistSong(current_stream.id, youtube_id, skip_after=skip_after) session.add(song_requested) song_info = session.query(PleblistSongInfo).filter_by( pleblist_song_youtube_id=youtube_id).first() if song_info is None and song_requested.song_info is None: PleblistManager.init( app.bot_config['youtube']['developer_key']) song_info = PleblistManager.create_pleblist_song_info( song_requested.youtube_id) if song_info is not False: session.add(song_info) session.commit() return {'success': 'got em!'}, 200
def post(self): args = self.post_parser.parse_args() try: pajbot.web.utils.pleblist_login(args['password'], app.bot_config) except pajbot.exc.InvalidLogin as e: return {'error': str(e)}, 401 with DBManager.create_session_scope() as session: youtube_id = args['youtube_id'] current_stream = session.query(Stream).filter_by(ended=False).order_by(Stream.stream_start).first() if current_stream is None: return { 'error': 'Stream offline' }, 400 skip_after = args['skip_after'] log.info('Request song youtube ID: {}'.format(youtube_id)) song_requested = PleblistSong(current_stream.id, youtube_id, skip_after=skip_after) session.add(song_requested) song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first() if song_info is None and song_requested.song_info is None: PleblistManager.init(app.bot_config['youtube']['developer_key']) song_info = PleblistManager.create_pleblist_song_info(song_requested.youtube_id) if song_info is not False: session.add(song_info) session.commit() return { 'success': 'got em!' }, 200
def bg_pleblist_add_song(self, stream_id, youtube_id, force, bot, source): with DBManager.create_session_scope() as db_session: song_info = PleblistManager.get_song_info(youtube_id, db_session) if song_info is None or force: try: # XXX: Should this be a setting in the module? idk PleblistManager.init( bot.config["youtube"]["developer_key"]) except: log.error("No youtube key set up.") bot.whisper(source, "No youtube key set up") return False song_info = PleblistManager.create_pleblist_song_info( youtube_id) if song_info is False: bot.whisper( source, "Invalid song given (or the YouTube API is down)") return False db_session.merge(song_info) db_session.commit() # See if the user has already submitted X songs num_unplayed_songs_requested = int( db_session.query(func.count( PleblistSong.id)).filter_by(stream_id=stream_id, user_id=source.id, date_played=None).one()[0]) if num_unplayed_songs_requested >= self.settings[ "max_songs_per_user"] and not force: bot.whisper( source, f"You can only request {num_unplayed_songs_requested} songs at the same time!" ) return False # Add the song request song_request = PleblistSong(bot.stream_manager.current_stream.id, youtube_id, user_id=source.id) # See if the song is too long # If it is, make it autoskip after that time if song_info.duration > self.settings["max_song_length"]: song_request.skip_after = self.settings["max_song_length"] db_session.add(song_request) bot.say( f'{source} just requested the song "{song_info.title}" to be played KKona' )
def pleblist_add(): if not request.method == 'POST': return make_response(jsonify({'error': 'Invalid request method'}), 405) if 'youtube_id' not in request.form: return make_response(jsonify({'error': 'Missing data youtube_id'}), 400) if 'password' not in request.form: return make_response(jsonify({'error': 'Missing data password'}), 400) salted_password = generate_password_hash( config['web']['pleblist_password'], config['web']['pleblist_password_salt']) try: user_password = base64.b64decode(request.form['password']) except binascii.Error: return make_response(jsonify({'error': 'Invalid data password'}), 400) if not user_password == salted_password: return make_response(jsonify({'error': 'Invalid password'}), 401) with DBManager.create_session_scope() as session: youtube_id = request.form['youtube_id'] current_stream = session.query(Stream).filter_by(ended=False).order_by( Stream.stream_start).first() if current_stream is None: return make_response(jsonify({'error': 'Stream offline'}), 400) skip_after = request.form.get('skip_after', None) if skip_after is not None: try: skip_after = int(skip_after) except ValueError: skip_after = None song_requested = PleblistSong(current_stream.id, youtube_id, skip_after=skip_after) session.add(song_requested) song_info = session.query(PleblistSongInfo).filter_by( pleblist_song_youtube_id=youtube_id).first() if song_info is None and song_requested.song_info is None: PleblistManager.init(config['youtube']['developer_key']) song_info = PleblistManager.create_pleblist_song_info( song_requested.youtube_id) if song_info is not False: session.add(song_info) session.commit() return jsonify({'success': True})
def pleblist_validate(): if not request.method == 'POST': return make_response(jsonify({'error': 'Invalid request method'}), 405) if 'youtube_id' not in request.form: return make_response(jsonify({'error': 'Missing data youtube_id'}), 400) with DBManager.create_session_scope() as session: youtube_id = request.form['youtube_id'] print(youtube_id) song_info = session.query(PleblistSongInfo).filter_by( pleblist_song_youtube_id=youtube_id).first() if song_info is not None: return jsonify({ 'message': 'success', 'song_info': song_info.jsonify() }) PleblistManager.init(config['youtube']['developer_key']) song_info = PleblistManager.create_pleblist_song_info(youtube_id) if not song_info and len(youtube_id) > 11: youtube_id = youtube_id[:11] song_info = session.query(PleblistSongInfo).filter_by( pleblist_song_youtube_id=youtube_id).first() if song_info is not None: return jsonify({ 'message': 'success', 'new_youtube_id': youtube_id, 'song_info': song_info.jsonify() }) else: song_info = PleblistManager.create_pleblist_song_info( youtube_id) if song_info: print(song_info) session.add(song_info) session.commit() return jsonify({ 'message': 'success', 'new_youtube_id': youtube_id, 'song_info': song_info.jsonify() }) return jsonify({'message': 'invalid youtube id', 'song_info': None})
def pleblist_validate(): if not request.method == 'POST': return make_response(jsonify({'error': 'Invalid request method'}), 405) if 'youtube_id' not in request.form: return make_response(jsonify({'error': 'Missing data youtube_id'}), 400) with DBManager.create_session_scope() as session: youtube_id = request.form['youtube_id'] print(youtube_id) song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first() if song_info is not None: return jsonify({ 'message': 'success', 'song_info': song_info.jsonify() }) PleblistManager.init(config['youtube']['developer_key']) song_info = PleblistManager.create_pleblist_song_info(youtube_id) if not song_info and len(youtube_id) > 11: youtube_id = youtube_id[:11] song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first() if song_info is not None: return jsonify({ 'message': 'success', 'new_youtube_id': youtube_id, 'song_info': song_info.jsonify() }) else: song_info = PleblistManager.create_pleblist_song_info(youtube_id) if song_info: print(song_info) session.add(song_info) session.commit() return jsonify({ 'message': 'success', 'new_youtube_id': youtube_id, 'song_info': song_info.jsonify() }) return jsonify({ 'message': 'invalid youtube id', 'song_info': None })
def pleblist_add(): if not request.method == "POST": return make_response(jsonify({"error": "Invalid request method"}), 405) if "youtube_id" not in request.form: return make_response(jsonify({"error": "Missing data youtube_id"}), 400) if "password" not in request.form: return make_response(jsonify({"error": "Missing data password"}), 400) salted_password = generate_password_hash( config["web"]["pleblist_password"], config["web"]["pleblist_password_salt"] ) try: user_password = base64.b64decode(request.form["password"]) except binascii.Error: return make_response(jsonify({"error": "Invalid data password"}), 400) if not user_password == salted_password: return make_response(jsonify({"error": "Invalid password"}), 401) with DBManager.create_session_scope() as session: youtube_id = request.form["youtube_id"] current_stream = session.query(Stream).filter_by(ended=False).order_by(Stream.stream_start).first() if current_stream is None: return make_response(jsonify({"error": "Stream offline"}), 400) skip_after = request.form.get("skip_after", None) if skip_after is not None: try: skip_after = int(skip_after) except ValueError: skip_after = None song_requested = PleblistSong(current_stream.id, youtube_id, skip_after=skip_after) session.add(song_requested) song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first() if song_info is None and song_requested.song_info is None: PleblistManager.init(config["youtube"]["developer_key"]) song_info = PleblistManager.create_pleblist_song_info(song_requested.youtube_id) if song_info is not False: session.add(song_info) session.commit() return jsonify({"success": True})
def bg_pleblist_add_song(self, stream_id, youtube_id, force, **options): bot = options['bot'] source = options['source'] with DBManager.create_session_scope() as db_session: song_info = PleblistManager.get_song_info(youtube_id, db_session) if song_info is None or force: try: # XXX: Should this be a setting in the module? idk PleblistManager.init(bot.config['youtube']['developer_key']) except: log.error('No youtube key set up.') bot.whisper(source.username, 'No youtube key set up') return False song_info = PleblistManager.create_pleblist_song_info(youtube_id) if song_info is False: bot.whisper(source.username, 'Invalid song given (or the YouTube API is down)') return False db_session.merge(song_info) db_session.commit() # See if the user has already submitted X songs num_unplayed_songs_requested = int(db_session.query(func.count(PleblistSong.id)).filter_by(stream_id=stream_id, user_id=source.id, date_played=None).one()[0]) if num_unplayed_songs_requested >= self.settings['max_songs_per_user']: bot.whisper(source.username, 'You can only request {} songs at the same time!'.format(num_unplayed_songs_requested)) return False # Add the song request song_request = PleblistSong(bot.stream_manager.current_stream.id, youtube_id, user_id=source.id) # See if the song is too long # If it is, make it autoskip after that time if song_info.duration > self.settings['max_song_length']: song_request.skip_after = self.settings['max_song_length'] db_session.add(song_request) bot.say('{} just requested the song "{}" to be played KKona'.format(source.username_raw, song_info.title))
def bg_pleblist_add_song(self, stream_id, youtube_id, **options): bot = options['bot'] source = options['source'] with DBManager.create_session_scope() as db_session: song_info = db_session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first() if song_info is None: try: # XXX: Should this be a setting in the module? idk PleblistManager.init(bot.config['youtube']['developer_key']) except: log.error('No youtube key set up.') bot.whisper(source.username, 'No youtube key set up') return False song_info = PleblistManager.create_pleblist_song_info(youtube_id) if song_info is False: bot.whisper(source.username, 'Invalid song given (or the YouTube API is down)') return False db_session.add(song_info) db_session.commit() # See if the user has already submitted X songs num_unplayed_songs_requested = int(db_session.query(func.count(PleblistSong.id)).filter_by(stream_id=stream_id, user_id=source.id, date_played=None).one()[0]) if num_unplayed_songs_requested >= self.settings['max_songs_per_user']: bot.whisper(source.username, 'You can only request {} songs at the same time!'.format(num_unplayed_songs_requested)) return False # Add the song request song_request = PleblistSong(bot.stream_manager.current_stream.id, youtube_id, user_id=source.id) # See if the song is too long # If it is, make it autoskip after that time if song_info.duration > self.settings['max_song_length']: song_request.skip_after = self.settings['max_song_length'] db_session.add(song_request) bot.say('{} just requested the song "{}" to be played KKona'.format(source.username_raw, song_info.title))
def post(self): args = self.post_parser.parse_args() with DBManager.create_session_scope() as session: youtube_id = args['youtube_id'] log.info('Validating youtube ID {}'.format(youtube_id)) song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first() if song_info is not None: return { 'message': 'success', 'song_info': song_info.jsonify() } PleblistManager.init(app.bot_config['youtube']['developer_key']) song_info = PleblistManager.create_pleblist_song_info(youtube_id) if not song_info and len(youtube_id) > 11: youtube_id = youtube_id[:11] song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first() if song_info is not None: return { 'message': 'success', 'new_youtube_id': youtube_id, 'song_info': song_info.jsonify() } else: song_info = PleblistManager.create_pleblist_song_info(youtube_id) if song_info: log.debug(song_info) session.add(song_info) session.commit() return { 'message': 'success', 'new_youtube_id': youtube_id, 'song_info': song_info.jsonify() } return { 'message': 'invalid youtube id', 'song_info': None }
def get_current_song_value(self, key, extra={}): if self.stream_manager.online: current_song = PleblistManager.get_current_song(self.stream_manager.current_stream.id) inner_keys = key.split(".") val = current_song for inner_key in inner_keys: val = getattr(val, inner_key, None) if val is None: return None if val is not None: return val return None
def get_current_song_value(self, key, extra={}): if self.stream_manager.online: current_song = PleblistManager.get_current_song(self.stream_manager.current_stream.id) inner_keys = key.split('.') val = current_song for inner_key in inner_keys: val = getattr(val, inner_key, None) if val is None: return None if val is not None: return val return None
def pleblist_add(): if not request.method == 'POST': return make_response(jsonify({'error': 'Invalid request method'}), 405) if 'youtube_id' not in request.form: return make_response(jsonify({'error': 'Missing data youtube_id'}), 400) if 'password' not in request.form: return make_response(jsonify({'error': 'Missing data password'}), 400) salted_password = generate_password_hash(config['web']['pleblist_password'], config['web']['pleblist_password_salt']) try: user_password = base64.b64decode(request.form['password']) except binascii.Error: return make_response(jsonify({'error': 'Invalid data password'}), 400) if not user_password == salted_password: return make_response(jsonify({'error': 'Invalid password'}), 401) with DBManager.create_session_scope() as session: youtube_id = request.form['youtube_id'] current_stream = session.query(Stream).filter_by(ended=False).order_by(Stream.stream_start).first() if current_stream is None: return make_response(jsonify({'error': 'Stream offline'}), 400) skip_after = request.form.get('skip_after', None) if skip_after is not None: try: skip_after = int(skip_after) except ValueError: skip_after = None song_requested = PleblistSong(current_stream.id, youtube_id, skip_after=skip_after) session.add(song_requested) song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first() if song_info is None and song_requested.song_info is None: PleblistManager.init(config['youtube']['developer_key']) song_info = PleblistManager.create_pleblist_song_info(song_requested.youtube_id) if song_info is not False: session.add(song_info) session.commit() return jsonify({'success': True})
def post(self): args = self.post_parser.parse_args() with DBManager.create_session_scope() as session: youtube_id = args['youtube_id'] log.info('Validating youtube ID {}'.format(youtube_id)) song_info = session.query(PleblistSongInfo).filter_by( pleblist_song_youtube_id=youtube_id).first() if song_info is not None: return {'message': 'success', 'song_info': song_info.jsonify()} PleblistManager.init(app.bot_config['youtube']['developer_key']) song_info = PleblistManager.create_pleblist_song_info(youtube_id) if not song_info and len(youtube_id) > 11: youtube_id = youtube_id[:11] song_info = session.query(PleblistSongInfo).filter_by( pleblist_song_youtube_id=youtube_id).first() if song_info is not None: return { 'message': 'success', 'new_youtube_id': youtube_id, 'song_info': song_info.jsonify() } else: song_info = PleblistManager.create_pleblist_song_info( youtube_id) if song_info: log.debug(song_info) session.add(song_info) session.commit() return { 'message': 'success', 'new_youtube_id': youtube_id, 'song_info': song_info.jsonify() } return {'message': 'invalid youtube id', 'song_info': None}