| |
@@ -1,9 +1,9 @@
|
| |
from __future__ import unicode_literals
|
| |
|
| |
- import operator
|
| |
import requests
|
| |
+ import arrow
|
| |
+ from collections import defaultdict, OrderedDict
|
| |
|
| |
- from hubs.utils import validators
|
| |
from hubs.widgets.base import Widget
|
| |
from hubs.widgets.view import RootWidgetView
|
| |
from hubs.widgets.caching import CachedFunction
|
| |
@@ -13,13 +13,13 @@
|
| |
|
| |
name = "badges"
|
| |
position = "right"
|
| |
- parameters = [dict(
|
| |
- name="username",
|
| |
- label="Username",
|
| |
- default=None,
|
| |
- validator=validators.Username,
|
| |
- help="A FAS username.",
|
| |
- )]
|
| |
+ hub_types = ["stream", "user"]
|
| |
+
|
| |
+ def get_template_environment(self):
|
| |
+ env = super(Badges, self).get_template_environment()
|
| |
+ # Add a filter
|
| |
+ env.filters['humanize'] = lambda d: arrow.get(d).humanize()
|
| |
+ return env
|
| |
|
| |
|
| |
class BaseView(RootWidgetView):
|
| |
@@ -32,6 +32,9 @@
|
| |
|
| |
|
| |
class GetBadges(CachedFunction):
|
| |
+ def __init__(self, instance):
|
| |
+ self.instance = instance
|
| |
+ self.invalidate()
|
| |
|
| |
def execute(self):
|
| |
username = self.instance.config["username"]
|
| |
@@ -39,11 +42,36 @@
|
| |
url = url.format(username=username)
|
| |
response = requests.get(url)
|
| |
try:
|
| |
- assertions = response.json()['assertions']
|
| |
+ response = response.json()
|
| |
+ assertions = response['assertions']
|
| |
except (KeyError, ValueError):
|
| |
- assertions = []
|
| |
- key = operator.itemgetter('issued')
|
| |
- return dict(assertions=sorted(assertions, key=key, reverse=True))
|
| |
+ return {}
|
| |
+ tag_totals = defaultdict(int)
|
| |
+ latest_badge = None
|
| |
+ for badge in assertions:
|
| |
+ if (latest_badge is None or
|
| |
+ badge["issued"] > latest_badge["issued"]):
|
| |
+ latest_badge = badge
|
| |
+
|
| |
+ tags = filter(None, badge["tags"].split(','))
|
| |
+ for tag in tags:
|
| |
+ tag_totals[tag] += 1
|
| |
+
|
| |
+ tag_totals = OrderedDict(sorted(tag_totals.items(),
|
| |
+ key=lambda t: t[1],
|
| |
+ reverse=True))
|
| |
+
|
| |
+ tag_totals = {t: tag_totals[t] for t in list(tag_totals)[:5]}
|
| |
+
|
| |
+ return dict(
|
| |
+ username=username,
|
| |
+ total_users=response["user_count"],
|
| |
+ rank=response["rank"],
|
| |
+ tag_totals=tag_totals,
|
| |
+ latest_badge=latest_badge,
|
| |
+ total_badges=len(assertions),
|
| |
+ badge_percentage=round(response["percent_earned"], 1)
|
| |
+ )
|
| |
|
| |
def should_invalidate(self, message):
|
| |
if message['topic'].endswith('hubs.widget.update'):
|
| |
we don't habe the "rarest" badge info from the API
but everything else is there and works.
fixes #435