简体   繁体   中英

Random match-up with Python and Google App Engine

I am building a website where two music videos are randomly chosen from a database and go head-to-head for voting. I need an algorithm that will continue picking unique match-ups for a user excluding match-ups they have had in the past, but with replacing videos for new match-ups. You can view a sample of the page here: http://10.showtownmvp.appspot.com/

I am running this on Google App Engine - Python, and have a voting table and videos table that stores the results. I would like to keep it as random as possible and avoid multiple queries, so if you have suggestions on how to model this in NDB or have a good algorithm, I would appreciate your help!

My solution to this problem was to query all videos from the datastore and randomly select one. I also ran a query for past votes / matchups for the user and converted this to a list so I could manipulate it without running several queries. Using the random video, I used a while loop to find a second video that was not in the previous matchup list. If no video was found, the program would remove the random choice from the video list, then select a new sample and run the search again. The code is below:

    class MainHandler(views.Template):              

        def post(self):
            # NOTE: we are posting genre and state.
            user = self.user_check()
            self.videos = models.videos.Videos.fetch_featured()

            try:
                random.sample(self.videos,2)

                if user:
                    self.user_votes = models.voting.Voting.query_by_user(user.key)

                    if self.user_votes != None:
                        self.user_votes = [[x.video_one,x.video_two] for x in self.user_votes]
                        page_vids = False
                        while page_vids == False and len(self.videos)>1:
                            rand_vid = random.choice(self.videos)
                            page_vids = self.find_match(rand_vid)
                            self.videos.remove(rand_vid)
                    else:
                        page_vids = random.sample(self.videos,2)

                else:
                    page_vids = random.sample(self.videos,2)

            except:
                page_vids = None


        def find_match(self, rand_vid):
            i =0

            while i < len(self.videos):
                if rand_vid.key != self.videos[i].key and ([rand_vid.key,self.videos[i].key] not in self.user_votes and [self.videos[i].key, rand_vid.key] not in self.user_votes):
                    return [rand_vid,self.videos[i]]
                i+=1
            return False




    class Videos(ndb.Model):
        acc_key = ndb.KeyProperty()
        musician_key = ndb.KeyProperty()
        musician_name = ndb.StringProperty()
        embed_link = ndb.StringProperty()
        genre_tag = ndb.StringProperty()
        video_title = ndb.StringProperty()
        featured = ndb.BooleanProperty(default = False)
        likes_count = ndb.IntegerProperty()
        video_added = ndb.DateTimeProperty(auto_now_add = True)

        @classmethod
        def query_by_account(cls, acc_key):
            return cls.query(cls.acc_key == acc_key).fetch()

        @classmethod
        def fetch_featured(cls):
            return cls.query(cls.featured == True).fetch(100)


class Voting(ndb.Model):
        voter_acc_key = ndb.KeyProperty()
        voter_type = ndb.StringProperty()
        video_one = ndb.KeyProperty()
        video_one_artist_key = ndb.KeyProperty()
        video_two = ndb.KeyProperty()
        video_two_artist_key = ndb.KeyProperty()
        voter_choice = ndb.KeyProperty()
        video_set_check = ndb.KeyProperty(repeated = True)
        voter_ip = ndb.StringProperty()
        vote_time = ndb.DateTimeProperty(auto_now_add = True)


        @classmethod
        def query_by_user(cls, acc_key):
            return cls.query(cls.voter_acc_key == acc_key).fetch(2000)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM