-
Notifications
You must be signed in to change notification settings - Fork 1
/
pytunkrank.py
61 lines (40 loc) · 1.22 KB
/
pytunkrank.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
pytunkrank
==========
An interface to TunkRank API
"""
from urllib import FancyURLopener
try:
import json
except ImportError:
try:
import simplejson as json
except ImportError:
raise Exception("A JSON parser is required, e.g., simplejson at " \
"http://pypi.python.org/pypi/simplejson/")
version_info = (0, 2)
__version__ = ".".join(map(str, version_info))
_BASE_URI = "http://www.tunkrank.com"
class TunkRankError(Exception):
pass
class PyTROpener(FancyURLopener):
version = 'pytunkrank/%s' % (__version__,)
urlopen = PyTROpener().open
def score(username):
try:
u = urlopen(_BASE_URI + "/score/%s.json" % (username,))
data = json.loads(u.read())
return data['twitter_user']
except:
raise TunkRankError("Unable to fetch score for %s" % (username,))
def ranking(username):
return score(username)["ranking"]
def raw_score(username):
return score(username)["raw_tunkrank_score"]
def refresh(username):
try:
u = urlopen(_BASE_URI + "/refresh/%s.json" % (username,))
data = json.loads(u.read())
return data['refresh']
except:
raise TunkRankError("Unable to refresh %s" % (username,))