-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
133 lines (110 loc) · 4.95 KB
/
__init__.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# coding=utf-8
"""
Vavag python client
Copyright (C) 2011 Javier Cordero Martinez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
__author__ = "Javier Cordero Martinez ([email protected])"
__copyright__ = "Copyright 2011"
__contributors__ = []
__license__ = "AGPLv3"
__version__ = "0.1"
from httplib2 import Http
# http://vavag.com/help/api/v2
class VavagException(Exception):
status = None
msg = None
def __init__(self, status, msg):
super(self.__class__, self).__init__()
self.status = status
self.msg = msg
class VavagRequest(Http):
URL_get_info_url = 'http://vavag.com/api/%(version)s/%(method)s/%(login)s/%(apikey)s/get_info_url?url='
URL_get_pack = 'http://vavag.com/api/%(version)s/%(method)s/%(login)s/%(apikey)s/get_pack?packhash='
URL_set_pack = 'http://vavag.com/api/%(version)s/%(method)s/%(login)s/%(apikey)s/set_pack?'
headers = { 'User-Agent' : 'GeoRemindMe Vavag: %s' % __version__ }
def __init__(self, login, api_key, version='v2', method = 'json', **kwargs):
super(self.__class__, self).__init__(timeout=3, **kwargs)
self.version = version
self.api_key = api_key
self.login = login
self.method = method
def _encode(self, url):
from urllib import quote
if not 'http://' in url and not 'https://' in url:
url = 'http://' + url
return quote(url)
#from base64 import urlsafe_b64encode
#return urlsafe_b64encode(url)
def get_info(self, url):
request_url = self.URL_get_info_url % {
'version': self.version,
'method': self.method,
'login': self.login,
'apikey': self.api_key
}
request_url = request_url + self._encode(url)
return self._do_request(request_url)
def get_pack(self, packHash):
request_url = self.URL_get_pack % {
'version': self.version,
'method': self.method,
'login': self.login,
'apikey': self.api_key
}
request_url = request_url + packHash
return self._do_request(request_url)
def set_pack(self, pack):
"""
Creates a new pack of urls
"""
request_url = self.URL_set_pack % {
'version': self.version,
'method': self.method,
'login': self.login,
'apikey': self.api_key
}
if type(pack) != type(list()):
type_param = 2
else:
type_param = 1
pack = '|sep|'.join([self._encode(url) for url in pack])
import urllib
request_url = request_url + urllib.urlencode({'packchain': pack,
'type': type_param
})
return self._do_request(request_url)
def _do_request(self, url, method='GET', body=None):
"""
Does a request:
:param url: url to request
:type url: string
:returns: dict with the results from vavag.com
:raises: :class:`VavagException`
"""
try:
response, content = self.request(url, method=method, body=body, headers=self.headers)
except:
raise VavagException()
if int(response['status']) != 200:
raise VavagException(status=response['status'], msg='ERROR IN REQUEST')
try:
import json as simplejson
except:
from django.utils import simplejson
json = simplejson.loads(content)
if json['status'] == 200:
return json['results']
import logging
logger = logging.getLogger(__name__)
logger.error("ERROR EN VAVAG status: %s msg: %s" % (json['status'], json['statusMsg']))
raise VavagException(status=json['status'], msg=json['statusMsg'])