def get_rating(self, date, is_russian, count): if date < self.date_from or date > utils.today(): return () valid_rating = self._get_valid_rating(date, is_russian, count) return {x.position_mixed: x.artist_name for x in valid_rating} if is_russian is None \ else {x.position: x.artist_name for x in valid_rating}
def _get_rating(self, for_date=None): service = TotalRatingService() count = 50 date = for_date if for_date else utils.today() return { 'russian_rating': service.get_rating(date, True, count), 'foreign_rating': service.get_rating(date, False, count), 'mix_rating': service.get_rating(date, None, 100), }
def get_rating(self, date=None): d = date if date else utils.today() result = self._get_nearest_rating(d) # move all artists with delta == 0 to the rating end for r in itertools.ifilter(lambda x: x.relative_interest_delta == 0, result): r.relative_interest_delta = -sys.maxint return sorted(result, key=attrgetter('relative_interest_delta'), reverse=True)
def _get_valid_rating(self, date, is_russian, count): rating = self._query_rating(date, is_russian, count) # if rating version is valid if rating and rating[0].formula_version == self.formula.version: return rating # if today's rating is not ready return rating for the previous day elif not rating and date == utils.today(): return self._get_valid_rating(date - datetime.timedelta(days=1), is_russian, count) # calculate rating for the date if rating is empty or not valid self._recalculate(date) # query valid rating for date return self._query_rating(date, is_russian, count)
def calculate_total_rating(): ratings = [ YandexWordStatRatingService(), VkRatingService(), #GoogleTrendsRatingService(), YouTubeRatingService(), LastFmRatingService() ] total_rating = TotalRatingService() if not total_rating.is_today_completed(): if all(x.is_today_completed()[0] for x in ratings) and not total_rating.is_today_completed(): TotalRatingService().calculate(utils.today()) send_completed() else: logging.info( "TOTAL RATING: Not all rating data is ready to calculate")
def send_completed(): body = rating.settings.URL_BASE mail.send( "{} Рейтинг артистов сформирован".format(utils.to_moscow( utils.today())), body, rating.settings.ADMIN_EMAIL_1)
def send_not_completed(): body = rating.settings.URL_BASE mail.send( "{} Не удалось сформировать рейтинг".format( utils.to_moscow(utils.today())), body, rating.settings.ADMIN_EMAIL_1)
def __init__(self): self.today = utils.today() self.status = None
def is_today_completed(self): return TotalRating.query().filter( TotalRating.date == utils.today()).count() > 0