remove backported Counter class
[mygpo.git] / mygpo / admin / clients.py
bloba916e44cf7ece516bcce540b5224dca8d11acf16
1 import re
2 from collections import namedtuple, Counter
4 from mygpo.users.models import User
5 from mygpo.db.couchdb.user import user_agent_stats
8 Client = namedtuple('Client', 'client client_version lib lib_version os os_version')
11 class UserAgentStats(object):
12 """ Provides User-Agent statistics """
14 def __init__(self):
15 self._useragents = None
18 def get_entries(self):
19 if self._useragents is None:
20 self._useragents = user_agent_stats()
22 return self._useragents
25 @property
26 def max_users(self):
27 uas = self.get_entries()
28 if uas:
29 return uas.most_common(1)[0][1]
30 else:
31 return 0
33 @property
34 def total_users(self):
35 uas = self.get_entries()
36 if uas:
37 return sum(uas.values())
38 else:
39 return 0
42 # regular expressions for detecting a client application from a User-Agent
43 RE_GPODROID = re.compile('GpodRoid ([0-9.]+) Mozilla/5.0 \(Linux; U; Android ([0-9a-z.-]+);')
44 RE_GPODDER = re.compile('mygpoclient/([0-9.]+) \([^)]+\) gPodder/([0-9.]+)')
45 RE_MYGPOCLIENT = re.compile('mygpoclient/([0-9.]+) \([^)]+\)')
46 RE_CLEMENTINE = re.compile('Clementine ([0-9a-z.-]+)')
47 RE_AMAROK = re.compile('amarok/([0-9.]+)')
48 RE_GPNACCOUNT = re.compile('GPodder.net Account for Android')
51 class ClientStats(UserAgentStats):
52 """ Provides statistics about client applications """
54 def __init__(self):
55 self._clients = None
56 super(ClientStats, self).__init__()
59 def get_entries(self):
61 if self._clients is None:
62 self._clients = Counter()
64 uas = super(ClientStats, self).get_entries()
65 for ua_string, count in uas.items():
66 client = self.parse_ua_string(ua_string) or ua_string
67 self._clients[client] += count
69 return self._clients
72 def parse_ua_string(self, ua_string):
74 m = RE_GPODROID.search(ua_string)
75 if m:
76 return Client('gpodroid', m.group(1), None, None, 'android', m.group(2))
78 m = RE_GPODDER.search(ua_string)
79 if m:
80 return Client('gpodder', m.group(2), 'mygpoclient', m.group(1), None, None)
82 m = RE_MYGPOCLIENT.search(ua_string)
83 if m:
84 return Client(None, None, 'mygpoclient', m.group(1), None, None)
86 m = RE_CLEMENTINE.search(ua_string)
87 if m:
88 return Client('clementine', m.group(1), None, None, None, None)
90 m = RE_AMAROK.search(ua_string)
91 if m:
92 return Client('amarok', m.group(1), None, None, None, None)
94 m = RE_GPNACCOUNT.search(ua_string)
95 if m:
96 return Client(None, None, 'gpodder.net-account-android', None, 'android', None)
98 return None