diff options
author | Pacien TRAN-GIRARD | 2014-10-24 19:59:05 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2014-10-24 19:59:05 +0200 |
commit | bdf9099df8c2a4636b0ad0e710b73330877eef37 (patch) | |
tree | 63fd704f15f3030f1455aad0ef92403c5d093c70 /wikibania/api | |
parent | 16529a0d212e1387eacd590c0e5e1b1a13dc2641 (diff) | |
download | wikistats-bdf9099df8c2a4636b0ad0e710b73330877eef37.tar.gz |
Very cleaner, much class, such readable, wow
Diffstat (limited to 'wikibania/api')
-rw-r--r-- | wikibania/api/Query.py | 24 | ||||
-rw-r--r-- | wikibania/api/__init__.py | 1 |
2 files changed, 25 insertions, 0 deletions
diff --git a/wikibania/api/Query.py b/wikibania/api/Query.py new file mode 100644 index 0000000..0928478 --- /dev/null +++ b/wikibania/api/Query.py | |||
@@ -0,0 +1,24 @@ | |||
1 | import urllib.parse | ||
2 | import urllib.request | ||
3 | import json | ||
4 | |||
5 | |||
6 | class Query: | ||
7 | def __init__(self, base_url="", params=None, encoding="utf8"): | ||
8 | if params is None: | ||
9 | params = {} | ||
10 | |||
11 | self.base_url = base_url | ||
12 | self.params = params | ||
13 | self.encoding = encoding | ||
14 | |||
15 | def fetch_raw_result(self): | ||
16 | post_query = urllib.parse.urlencode(self.params) | ||
17 | post_query = post_query.encode(self.encoding) | ||
18 | document = urllib.request.urlopen(self.base_url, post_query) | ||
19 | return document.read().decode(self.encoding) | ||
20 | |||
21 | |||
22 | class JSONQuery(Query): | ||
23 | def fetch_result(self): | ||
24 | return json.loads(self.fetch_raw_result()) | ||
diff --git a/wikibania/api/__init__.py b/wikibania/api/__init__.py new file mode 100644 index 0000000..792d600 --- /dev/null +++ b/wikibania/api/__init__.py | |||
@@ -0,0 +1 @@ | |||
# | |||