def get_init_data(stream_id, user=None): from requests import stream_events_handler song = Song.objects().order_by("-last_played")[0] poll = Poll.get_current_poll(stream_id) init_data = InitData() if (user): init_data.user = user poll_item = user.get_poll_item(poll) if (poll_item): init_data.user_poll_item_id = str(poll_item.id) #string init_data.poll = poll init_data.n_user = len(stream_events_handler.event_listeners[stream_id]) init_data.current_song = song return init_data
def get_init_data(stream_id, user=None): from requests import stream_events_handler song = Song.objects().order_by("-last_played")[0] poll = Poll.get_current_poll(stream_id) init_data = InitData() if(user): init_data.user = user poll_item = user.get_poll_item(poll) if(poll_item): init_data.user_poll_item_id = str(poll_item.id)#string init_data.poll = poll init_data.n_user = len( stream_events_handler.event_listeners[stream_id]) init_data.current_song = song return init_data
def _run(self): print "loading audio stream :: ", self.stream_id while(True): song_url_path = None current_poll = Poll.get_current_poll(self.stream_id) if(current_poll): if(not IS_TEST_BUILD): song = current_poll.get_highest_poll_song(self.stream_id) else: song = Song.objects(track_n=158).get() if(song): song_url_path = song.path retry_poll_creation = 3 while(retry_poll_creation>0): poll = Poll.create_next_poll(self.stream_id , not IS_TEST_BUILD) if(poll!=None): break retry_poll_creation-=1 if(poll==None): continue if(not song_url_path): song_url_path = "http://storage.googleapis.com/telugubeats_files/music/Telugu/devisri%20prasad/arya/amalapuram.mp3" if(song_url_path.startswith("/Users/abhinav/Desktop/Telugu//")): song_url_path="http://storage.googleapis.com/telugubeats_files/music/Telugu/"+urllib.quote(song_url_path[31:]) print "playing::", song_url_path self.fd = RemoteUrlFileObject(song_url_path) #spawn greenlet, keep reading into buffer #block calls to seek and read if buffer is not sufficient enough reset_data = InitData() reset_data.poll = poll reset_data.n_user = 1000+len( stream_events_handler.event_listeners[self.stream_id]) reset_data.current_song = song song.last_played = datetime.utcnow() if(not IS_TEST_BUILD): song.save() event_data = json_util.dumps(reset_data.to_son()) stream_events_handler.publish_event(self.stream_id, Event.RESET_POLLS_AND_SONG, event_data, from_user = None) # if there is valid ID3 data, read it out of the file first, # so we can skip sending it to the client try: self.id3 = id3reader.Reader(self.fd) if isinstance(self.id3.header.size, int): # read out the id3 data self.fd.seek(self.id3.header.size) while(True): try: cur_time = time.time() if(cur_time- self.last_time_stamp > self.sleep_time): self.last_time_stamp = cur_time self.buffer.queue_chunk(self.fd.read(self.buffer.chunk_byte_size)) gevent.sleep(self.sleep_time- time.time()+self.last_time_stamp) except EOFError: self.fd.close() break except Exception as e: print e