-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffbot_mw.py
54 lines (38 loc) · 1.35 KB
/
diffbot_mw.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
import requests
import json
__version__ = 1.0
__author__ = 'Islam Bahnasy'
__email__ = '[email protected]'
__url__ = 'http://cybertechlab.com'
class DiffbotMWException(Exception):
''' DiffbotMW Exception class
Code 401: Invalid Token
Code 500: Invalid API or wrong URL
'''
def __init__(self, errCode, errMsg):
self.errCode = errCode
self.errMsg = errMsg
def __str__(self):
return ("<DiffbotMWException: %s %s>" % (self.errCode, self.errMsg))
def diffbot_mw(token, url, api, fields, version):
''' Sends HTTP GET request to the DiffbotMW API and returns
JSON object of the result in case of success or
raise 'DiffbotMWException' withe error code and message
token: developer token for authentication
url: target web page
api: type of API like 'article', 'image'
fields: specify desired output fields or '*' to return all fields
version: API version
example:
from diffbot_mw import diffbot_mw
json_obj = diffbot_mw('...', 'http://...', 'article',
fields=['*'], version=2)
'''
payload = { 'token': token, 'url': url, 'fields': fields }
response = requests.get("http://api.diffbot.com/v" + str(version) + "/" +
str(api), params=payload)
json_obj = response.json()
if response.status_code != 200:
raise DiffbotMWException(str(json_obj['errorCode']), \
str(json_obj['error']))
return json_obj