def get(self): db = Database(connect=1) message = self.request.get("message") session_id = self.request.get("id") if session_id and db.sessionExists(session_id): if not message and session_id: start_poll = time() # get timestamp of poll started new_messages = [] while 1: try: db = Database(connect=1) # reconnecting every time is necessary chat = db.deserialize(db.getData("chat", session_id)) for message in chat: if message: message_timestamp = float(message.split(":")[0]) if message_timestamp > start_poll: new_messages.append(message) if new_messages: self.response.out.write(new_messages) break else: sleep(2) except DeadlineExceededError: break elif message and db.isIDValid(session_id): current_chat = db.deserialize(db.getData("chat", session_id)) current_chat.append(str(time()) + ":" + quote_plus(message)) if len(current_chat >= 10): current_chat.pop() db.setData("chat", db.serialize(current_chat), session_id)
def get(self): def getVideoDuration(video_id): if video_id.startswith("yt;"): video_id = video_id.replace("yt;", "", 1) API_URL = "http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc&prettyprint=true" % video_id video_json = urllib.urlopen(API_URL) json_data = json.load(video_json) return int(json_data["data"]["duration"]) session_id = self.request.get('id') video_id = self.request.get('video') db = Database(connect=1) if db.sessionExists(session_id) and db.isStringSafe(video_id): old_queue = db.deserialize(db.getData("queue", session_id)) duration = getVideoDuration(video_id) timestamp = int(time()) play_time = timestamp + db.PLAY_OFFSET if old_queue: last_item = old_queue[-1].split(":") last_song_duration = last_item[2] last_song_playtime = last_item[3] play_time = int(last_song_playtime) + int(last_song_duration) if play_time < timestamp: play_time = timestamp + db.PLAY_OFFSET old_queue.append("%s:yt;%s:%s:%s" % (timestamp, video_id, duration, play_time)) db.setData("queue", db.serialize(old_queue), session_id)
def get(self): #check if session id is valid first db = Database(connect=1) session_id = self.request.get("id") if db.sessionExists(session_id): queue = db.deserialize(db.getData("queue", session_id)) if len(queue) < 10: upload_url = blobstore.create_upload_url('/blob/upload') self.response.out.write(upload_url) else: self.response.out.write("queue limit reached") logging.debug(queue) logging.debug(len(queue)) else: self.response.out.write("invalid session id")
def get(self): session_id = self.request.get("id") last_poll = self.request.get("last_poll") db = Database(connect=1) if db.sessionExists(session_id) and last_poll: new_songs = False try: while not new_songs: queue = db.deserialize(db.getData("queue", session_id)) if len(queue): queue = [item.split(":") for item in queue] for item in queue: if int(item[0]) > int(last_poll): self.response.out.write(":".join(item)) new_songs = True if not new_songs: db.connect() sleep(2) except DeadlineExceededError: self.response.out.write('queue check timed out')
def post(self): upload_files = self.get_uploads('file') # 'file' is file upload field in the form session_id = self.request.get('id') song_length = self.request.get('length') blob_info = upload_files[0] db = Database(connect=1) new_queue = db.deserialize(db.getData("queue", session_id)) timestamp = int(time()) play_time = timestamp + db.PLAY_OFFSET if new_queue: last_song_data = new_queue[-1].split(":") last_song_len = last_song_data[2] last_song_playtime = last_song_data[3] play_time = int(last_song_playtime) + int(last_song_len) if play_time < timestamp: # song should have started playing in the past play_time = timestamp + db.PLAY_OFFSET new_queue.append("{0}:{1}:{2}:{3}".format(timestamp, str(blob_info.key()), song_length, play_time)) db.setData("queue", db.serialize(new_queue), session_id) self.response.out.write(blob_info.key())