def test4(create_poll=False):
    ''' poll create and get one'''
#    initDb()
    if(create_poll):
        poll = Poll.create_next_poll("telugu")
        print poll.to_json()
    print Poll.get_current_poll("telugu").to_json()
def test_7():
    #random , test id , mongo field
    initDb()
    poll = Poll.get_current_poll("telugu")
    print str(poll.poll_items[0].id)
    print json_util.dumps(poll.poll_items[0].id)
    print json_util.dumps(poll.poll_items[0].poll.id)
def randomize_poll_winner():
    poll_id = int(input("Enter poll you'd like to pick a winner for: "))

    options = Poll.get(poll_id).options
    _print_poll_options(options)

    option_id = int(input("Enter which is the winning option, we'll pick a random winner from voters: "))
    votes = Option.get(option_id).votes    
    winner = random.choice(votes)
    print(f"The randomly selected winner is {winner[0]}.")
def prompt_vote_poll():
    poll_id = int(input("Enter poll would you like to vote on: "))
    poll = Poll.get(poll_id)
    options = poll.options
    _print_poll_options(options)

    option_id = int(input("Enter option you'd like to vote for: "))
    username = input("Enter the username you'd like to vote as: ")
    
    Option.get(option_id).vote(username)
def insert_init_data():
    
    Album.drop_collection()
    Song.drop_collection()
    
    
    data = json.loads(open("mapped_songs.json","r").read())
    count = 0
    for movie_name in data:
        movie = data[movie_name]
        a = Album()
        a.name = movie_name
        a.directors = movie.get("directors",None)
        a.music_directors = movie.get("music_directors",None)
        a.image_url = movie.get("img",None)
        a.actors = movie.get("actors",None)
        a.save()
        
        
        print "saved :: ", a.name
        for song_title_path  in  movie.get("song_entries",[]):
            if(not song_title_path): continue
            song_title , path  = song_title_path
            s = Song()
            s.title = song_title
#             s.lyricists = song.get("lyricists",None)
#             s.singers = song.get("singers",None)
            s.path = path
            s.album = a
            s.track_n = count
            count +=1
            s.save()
            print "    ", "saved song : " , s.title , s.album 
            
    
    poll = Poll.create_next_poll("telugu")
    print poll.to_json()
    print Poll.get_current_poll("telugu").to_json()        
    
    
    set_max_track()
def create_plt_fig():
    # print all polls and IDs as guide
    polls = Poll.all()
    print("\n\t\t------Available Polls------\n\n")
    for poll in polls:
        print(f"\t\t{poll.id}: {poll.title} (created by {poll.owner})")

    # select poll of interest
    poll_id = int(input("Enter poll you'd like to see a figure for: "))
    

    # save poll name of selected poll to variable (to pass to create_pie_chart() )
    poll_name = poll_name_by_id(polls, poll_id)

    # get vote info for selected poll
    options, votes = Poll.get_info_for_plt(poll_id) 

    # create figure
    fig = charts.create_pie_chart(options, votes, poll_name)
    plt.show()

    save_plot(options, votes, poll_name)
def prompt_create_poll():
    poll_title = input("Enter poll title: ")
    poll_owner = input("Enter poll owner: ")
    poll = Poll(poll_title, poll_owner)
    poll.save()

    while (new_option := input(NEW_OPTION_PROMPT)):
        poll.add_option(new_option)
def insert_init_data():

    Album.drop_collection()
    Song.drop_collection()

    data = json.loads(open("mapped_songs.json", "r").read())
    count = 0
    for movie_name in data:
        movie = data[movie_name]
        a = Album()
        a.name = movie_name
        a.directors = movie.get("directors", None)
        a.music_directors = movie.get("music_directors", None)
        a.image_url = movie.get("img", None)
        a.actors = movie.get("actors", None)
        a.save()

        print "saved :: ", a.name
        for song_title_path in movie.get("song_entries", []):
            if (not song_title_path): continue
            song_title, path = song_title_path
            s = Song()
            s.title = song_title
            #             s.lyricists = song.get("lyricists",None)
            #             s.singers = song.get("singers",None)
            s.path = path
            s.album = a
            s.track_n = count
            count += 1
            s.save()
            print "    ", "saved song : ", s.title, s.album

    poll = Poll.create_next_poll("telugu")
    print poll.to_json()
    print Poll.get_current_poll("telugu").to_json()

    set_max_track()
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
Exemple #10
0
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 show_poll_votes():
    # get input on which poll's info to see
    poll_id = int(input("Enter poll you would like to see votes for: "))

    # get poll of interest
    poll = Poll.get(poll_id)
    # get options for that poll
    options = poll.options
    # get number of votes per option
    votes_per_option = [len(option.votes) for option in options]
    # calculate the total number of votes from values in list
    total_votes = sum(votes_per_option)

    # iterate over options and according votes and print out info
    try:
        for option, votes in zip(options, votes_per_option):
            percentage = votes/total_votes * 100
            print(f"{option.text}: got {votes} votes ({percentage}% of total votes)")
    except ZeroDivisionError:
        print("No votes were casted for this poll yet.")
def list_open_polls():
    polls = Poll.all()
    print("\n\t\t------Open Polls------\n\n")
    for poll in polls:
        print(f"\t\t{poll.id}: {poll.title} (created by {poll.owner})")
def test_6():
    #save in db offlise for create_next_poll
    initDb()
    poll = Poll.create_next_poll("telugu", False)
    print poll
    print json_util.dumps(poll.to_son())
    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