示例#1
0
def get_title(title):
    title_url = urllib2.unquote(title).replace(' ', '-').lower()
    title = urllib2.quote(title_url.replace('-', ' '))
    key = title_url
    movie = memcache.get(key)
    if movie is None:
        logging.info('DB QUERY')
        q = db.Query(Movie)
        q.filter('title =', key)
        result = q.fetch(limit=1)
        if len(result) > 0:
            logging.info('MOVIE FOUND IN DB')
            row = result[0]
            movie = row.data
            memcache.set(key, movie)
        if movie is None:
            logging.info('FETCHING MOVIE FROM API')
            movie = query.query_movie(title)
            movie = set_star_ratings(movie)
            movie = set_covers(movie)
            m_dict = json.loads(movie)
            if m_dict['total'] != 0:
                m = Movie(title=key, data=movie)
                m.put()
                memcache.set(key, movie)
            else:
                movie = '{}'
    return json.loads(movie)
示例#2
0
    def get(self, category):
        if category == 'year':
            return self.year

        elif category == 'hosts':
            if not self.hosts:
                c = 0
                count = Counter()
                for tweet in self.corpus:
                    text = tweet['text']
                    if 'host' in text:
                        c += 1
                        tags = nltk.pos_tag(nltk.word_tokenize(text))
                        for i in xrange(len(tags)-1):
                            first = tags[i][1] == "NNP" and tags[i][0] not in stopwords
                            second = tags[i+1][1] == "NNP" and tags[i+1][0] not in stopwords
                            if first and second:
                                count["%s %s" % (tags[i][0], tags[i+1][0])] += 1
                        if c > 5:
                            break
                most_common = count.most_common(2)
                self.hosts = [most_common[0][0]]+[most_common[1][0]]
            return self.hosts

        elif category == 'awards':
            return self.awards.keys()

        elif category == 'winner':
            winner_dict = {}
            # count = Counter()
            # win_pattern = re.compile('w[io]n', re.IGNORECASE)
            for award in self.awards:
                award_pattern = re.compile(self.regexes[award])
                award_type = self.award_type[award]
                if not self.awards[award]['winner']: # Fill the dict
                    name_query = []
                    if award_type:
                        name_query = query.query_name(self.corpus, award_pattern, g=2, n=1)
                    else:
                        name_query = query.query_movie(self.corpus, award_pattern, g=2, n=1)
                    if name_query:
                        name_tuple = name_query[0][0]
                        name_count = name_query[0][1]
                        print award, name_tuple, name_count
                        name_string = "%s %s" % name_tuple
                    winner_dict[award] = name_string
                    self.awards[award]['winner'] = name_string
                else:
                    winner_dict[award] = self.awards[award]['winner']
            return winner_dict

        elif category == 'nominees':
            nominees_dict = {}
            # count = Counter()
            # nomin_pattern = re.compile('(nomin|w[io]n)', re.IGNORECASE)
            for award in self.awards:              
                award_pattern = re.compile(self.regexes[award])
                award_type = self.award_type[award]
                if not self.awards[award]['nominees']: #Fill the dict
                    name_query = []
                    if award_type:
                        name_query = query.query_name(self.corpus, award_pattern, g=2, n=5)
                    else:
                        name_query = query.query_movie(self.corpus, award_pattern, g=2, n=5)
                    nomin_list = []
                    for name in name_query:
                        if name != []:
                            name_tuple = name[0]
                            name_count = name[1]
                            print award, name_tuple, name_count
                            name_string = "%s %s" % name_tuple
                            nomin_list.append(name_string)
                    nominees_dict[award] = nomin_list
                    self.awards[award]['nominees'] = nomin_list
                else:
                    nominees_dict[award] = self.awards[award]['nominees']
            return nominees_dict

        elif category == "presenters":
            presenters_dict = {}
            for award in self.awards:
                award_pattern = re.compile(self.regexes[award])
                # award_type = self.award_type[award] #No need to check award type - presenters only people
                if not self.awards[award]['presenters']: #fill the dict
                    name_query = query.query_name(self.corpus, award_pattern, g=2, n=7)
                    presenter_list = []
                    for name in name_query[5:]:
                        if name != []:
                            name_tuple = name[0]
                            name_count = name[1]
                            print award, name_tuple, name_count
                            name_string = "%s %s" % name_tuple
                            presenter_list.append(name_string)
                            # print name_string, award
                    presenters_dict[award] = presenter_list
                    self.awards[award]['presenters'] = presenter_list
                else:
                    presenters_dict[award] = self.awards[award]['presenters']
            return presenters_dict

        else:
            raise KeyError("GET called with improper argument")