diff options
author | Pacien TRAN-GIRARD | 2014-10-24 01:45:28 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2014-10-24 01:45:28 +0200 |
commit | 5e4f38688e4c14846b8264970a79c482c1ca7012 (patch) | |
tree | 6d150aa8f26eed939aabc9de1db3239a46cb7e05 /banapedia/api/Query.py | |
download | wikistats-5e4f38688e4c14846b8264970a79c482c1ca7012.tar.gz |
Initial commit
Diffstat (limited to 'banapedia/api/Query.py')
-rw-r--r-- | banapedia/api/Query.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/banapedia/api/Query.py b/banapedia/api/Query.py new file mode 100644 index 0000000..7453df9 --- /dev/null +++ b/banapedia/api/Query.py | |||
@@ -0,0 +1,23 @@ | |||
1 | import urllib.parse | ||
2 | import urllib.request | ||
3 | import json | ||
4 | |||
5 | __author__ = 'pacien' | ||
6 | |||
7 | |||
8 | class Query: | ||
9 | def __init__(self, base_url="", params={}, encoding="utf8"): | ||
10 | self.base_url = base_url | ||
11 | self.params = params | ||
12 | self.encoding = encoding | ||
13 | |||
14 | def fetch_raw_result(self): | ||
15 | post_query = urllib.parse.urlencode(self.params) | ||
16 | post_query = post_query.encode(self.encoding) | ||
17 | document = urllib.request.urlopen(self.base_url, post_query) | ||
18 | return document.read().decode(self.encoding) | ||
19 | |||
20 | |||
21 | class JSONQuery(Query): | ||
22 | def fetch_result(self): | ||
23 | return json.loads(self.fetch_raw_result()) | ||