def get_day_language_xp_list(self, since: arrow.Arrow) -> List[DailyLanguageXp]: cache_key = get_key_with_prefix( f"{self.username}_{since.strftime('%Y-%m-%d')}", 'day_language_xp') lock_key = get_key_with_prefix(cache_key, 'lock') data = cache.get(cache_key) # add lock to make sure only one request is fetching response from server (hopefully) lock = cache.cache.inc(lock_key, delta=1) set_redis_expire_seconds(lock_key, 15) while data is None and isinstance(lock, int) and lock > 1: time.sleep(1) data = cache.get(cache_key) lock = cache.get(lock_key) if data is None: post_data = '''{ profile(username: "******") { day_language_xps: dayLanguageXps(since: "%s") {date language xp} } }''' % (self.username, since.strftime('%Y-%m-%d')) response_json = requests.post( 'https://codestats.net/profile-graph', data=post_data).json() current_app.logger.info('response json from %s: %s', self.username, response_json) data = response_json['data']['profile']['day_language_xps'] cache.set(cache_key, data, timeout=30 * 60) current_app.logger.info('set cache [%s]', cache_key) cache.delete(lock_key) self.day_language_xp_list = DailyLanguageXpSchema().load(data, many=True) return self.day_language_xp_list
def formatDate(a: arrow.Arrow, time=True) -> str: if time: return a.strftime("%d.%m.%Y %H:%M") else: return a.strftime("%d.%m.%Y")