API for Wallabag v2 ready
This commit is contained in:
@@ -17,24 +17,33 @@ class Wallabag(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
host = ''
|
host = ''
|
||||||
api_key = ''
|
token = ''
|
||||||
|
client_id = ''
|
||||||
|
client_secret = ''
|
||||||
user_agent = ''
|
user_agent = ''
|
||||||
format = ''
|
format = ''
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
host='http://v2.wallabag.org',
|
host='',
|
||||||
api_key='',
|
token='',
|
||||||
|
client_id='',
|
||||||
|
client_secret='',
|
||||||
extension='json',
|
extension='json',
|
||||||
user_agent="WallabagPython/1.0 +https://github.com/foxmask/wallabag-api"):
|
user_agent="WallabagPython/1.0 "
|
||||||
|
"+https://github.com/foxmask/wallabag-api"):
|
||||||
"""
|
"""
|
||||||
init variable
|
init variable
|
||||||
:param host: string url to the official API Wallabag
|
:param host: string url to the official API Wallabag
|
||||||
:param api_key: string of the key provided by Wallabag
|
:param token: string of the key provided by Wallabag
|
||||||
|
:param client_id client id
|
||||||
|
:param client_secret client secret
|
||||||
:param extension: json/xml/html
|
:param extension: json/xml/html
|
||||||
:param user_agent
|
:param user_agent
|
||||||
"""
|
"""
|
||||||
self.host = host
|
self.host = host
|
||||||
self.api_key = api_key
|
self.client_id = client_id
|
||||||
|
self.client_secret = client_secret
|
||||||
|
self.token = token
|
||||||
self.format = extension
|
self.format = extension
|
||||||
self.user_agent = user_agent
|
self.user_agent = user_agent
|
||||||
|
|
||||||
@@ -50,23 +59,30 @@ class Wallabag(object):
|
|||||||
Do a query to the System API
|
Do a query to the System API
|
||||||
:param path: url to the API
|
:param path: url to the API
|
||||||
:param method: the kind of query to do
|
:param method: the kind of query to do
|
||||||
:param params: a dict with all the necessary things to query the API
|
:param params: a dict with all the
|
||||||
|
necessary things to query the API
|
||||||
:return json data
|
:return json data
|
||||||
"""
|
"""
|
||||||
# params = params
|
if method in ('get', 'post', 'patch', 'delete', 'put', 'get_token'):
|
||||||
params['key'] = self.api_key
|
if method == 'get':
|
||||||
r = requests.get(self.get_host() + path, params=params)
|
r = requests.get(self.get_host() + path, params=params)
|
||||||
|
elif method == 'post':
|
||||||
if method == 'post':
|
r = requests.post(self.get_host() + path, data=params)
|
||||||
r = requests.post(self.get_host() + path, params=params)
|
|
||||||
elif method == 'patch':
|
elif method == 'patch':
|
||||||
r = requests.patch(self.get_host() + path, params=params)
|
r = requests.patch(self.get_host() + path, data=params)
|
||||||
elif method == 'delete':
|
elif method == 'delete':
|
||||||
r = requests.delete(self.get_host() + path, params=params)
|
r = requests.delete(self.get_host() + path, headers=params)
|
||||||
|
elif method == 'put':
|
||||||
|
r = requests.put(self.get_host() + path, params=params)
|
||||||
|
elif method == 'get_token':
|
||||||
|
r = requests.post(self.get_host() + path, data=params)
|
||||||
# todo : handle case of self.ext is xml or html
|
# todo : handle case of self.ext is xml or html
|
||||||
return self.handle_json_response(r)
|
return self.handle_json_response(r)
|
||||||
|
else:
|
||||||
|
raise ValueError('method expected : get, post, patch, delete or put')
|
||||||
|
|
||||||
def handle_json_response(self, responses):
|
@staticmethod
|
||||||
|
def handle_json_response(responses):
|
||||||
"""
|
"""
|
||||||
get the json data response
|
get the json data response
|
||||||
:param responses: the json response
|
:param responses: the json response
|
||||||
@@ -83,14 +99,13 @@ class Wallabag(object):
|
|||||||
logging.error("Wallabag: {error}".format(error=error_json))
|
logging.error("Wallabag: {error}".format(error=error_json))
|
||||||
return json_data
|
return json_data
|
||||||
|
|
||||||
def get_entries(self, token, **kwargs):
|
def get_entries(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
GET /api/entries.{_format}
|
GET /api/entries.{_format}
|
||||||
|
|
||||||
Retrieve all entries. It could be filtered by many options.
|
Retrieve all entries. It could be filtered by many options.
|
||||||
|
|
||||||
:param token: the token that identified the user to access the API
|
|
||||||
:param kwargs: can contain one of the following filters
|
:param kwargs: can contain one of the following filters
|
||||||
archive: '0' or '1', default '0' filter by archived status.
|
archive: '0' or '1', default '0' filter by archived status.
|
||||||
star: '0' or '1', default '0' filter by starred status.
|
star: '0' or '1', default '0' filter by starred status.
|
||||||
@@ -99,11 +114,12 @@ class Wallabag(object):
|
|||||||
order: 'asc' or 'desc', default 'desc'
|
order: 'asc' or 'desc', default 'desc'
|
||||||
page: int default 1 what page you want
|
page: int default 1 what page you want
|
||||||
perPage: int default 30 result per page
|
perPage: int default 30 result per page
|
||||||
tags: list of tags url encoded. Will returns entries that matches ALL tags
|
tags: list of tags url encoded.
|
||||||
|
Will returns entries that matches ALL tags
|
||||||
:return data related to the ext
|
:return data related to the ext
|
||||||
"""
|
"""
|
||||||
# default values
|
# default values
|
||||||
params = {'token': token,
|
params = {'access_token': self.token,
|
||||||
'archive': 0,
|
'archive': 0,
|
||||||
'star': 0,
|
'star': 0,
|
||||||
'delete': 0,
|
'delete': 0,
|
||||||
@@ -132,59 +148,60 @@ class Wallabag(object):
|
|||||||
|
|
||||||
return self.query(path, "get", **params)
|
return self.query(path, "get", **params)
|
||||||
|
|
||||||
def post_entries(self, token, url, title='', tags=''):
|
def post_entries(self, url, title='', tags='', starred=0, archive=0):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
POST /api/entries.{_format}
|
POST /api/entries.{_format}
|
||||||
|
|
||||||
Create an entry
|
Create an entry
|
||||||
|
|
||||||
:param token: the token that identified the user to access the API
|
|
||||||
:param url: the url of the note to store
|
:param url: the url of the note to store
|
||||||
:param title: Optional, we'll get the title from the page.
|
:param title: Optional, we'll get the title from the page.
|
||||||
:param tags: tag1,tag2,tag3 a comma-separated list of tags.
|
:param tags: tag1,tag2,tag3 a comma-separated list of tags.
|
||||||
|
:param starred entry already starred
|
||||||
|
:param archive entry already archived
|
||||||
:return result
|
:return result
|
||||||
"""
|
"""
|
||||||
params = {'token': token, 'url': url, 'title': title, 'tags': []}
|
params = {'access_token': self.token, 'url': url, 'title': title,
|
||||||
|
'tags': tags, 'starred': starred, 'archive': archive}
|
||||||
if len(tags) > 0 and isinstance(tags, list):
|
if len(tags) > 0 and isinstance(tags, list):
|
||||||
params['tags'] = tags
|
params['tags'] = ', '.join(tags)
|
||||||
path = '/api/entries.{ext}'.format(ext=self.format)
|
path = '/api/entries.{ext}'.format(ext=self.format)
|
||||||
return self.query(path, "post", **params)
|
return self.query(path, "post", **params)
|
||||||
|
|
||||||
def get_entry(self, token, entry):
|
def get_entry(self, entry):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
GET /api/entries/{entry}.{_format}
|
GET /api/entries/{entry}.{_format}
|
||||||
|
|
||||||
Retrieve a single entry
|
Retrieve a single entry
|
||||||
|
|
||||||
:param token: the token that identified the user to access the API
|
|
||||||
:param entry: \w+ an integer The Entry ID
|
:param entry: \w+ an integer The Entry ID
|
||||||
:return data related to the ext
|
:return data related to the ext
|
||||||
"""
|
"""
|
||||||
params = {'token': token}
|
params = {'access_token': self.token}
|
||||||
url = '/api/entries/{entry}.{ext}'.format(entry=entry, ext=self.format)
|
url = '/api/entries/{entry}.{ext}'.format(entry=entry, ext=self.format)
|
||||||
return self.query(url, "get", **params)
|
return self.query(url, "get", **params)
|
||||||
|
|
||||||
def patch_entries(self, token, entry, **kwargs):
|
def patch_entries(self, entry, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
PATCH /api/entries/{entry}.{_format}
|
PATCH /api/entries/{entry}.{_format}
|
||||||
|
|
||||||
Change several properties of an entry
|
Change several properties of an entry
|
||||||
|
|
||||||
:param token: the token that identified the user to access the API
|
|
||||||
:param entry: the entry to 'patch' / update
|
:param entry: the entry to 'patch' / update
|
||||||
:param kwargs: can contain one of the following
|
:param kwargs: can contain one of the following
|
||||||
title: string
|
title: string
|
||||||
tags: a list of tags tag1,tag2,tag3
|
tags: a list of tags tag1,tag2,tag3
|
||||||
archive: '0' or '1', default '0' archived the entry.
|
archive: '0' or '1', default '0' archived the entry.
|
||||||
star: '0' or '1', default '0' starred the entry
|
star: '0' or '1', default '0' starred the entry
|
||||||
delete: '0' or '1', default '0' flag as deleted. In case that you don't want to *really* remove it..
|
delete: '0' or '1', default '0' flag as deleted.
|
||||||
|
In case that you don't want to *really* remove it..
|
||||||
:return data related to the ext
|
:return data related to the ext
|
||||||
"""
|
"""
|
||||||
# default values
|
# default values
|
||||||
params = {'token': token,
|
params = {'access_token': self.token,
|
||||||
'title': '',
|
'title': '',
|
||||||
'archive': 0,
|
'archive': 0,
|
||||||
'tags': [],
|
'tags': [],
|
||||||
@@ -193,121 +210,121 @@ class Wallabag(object):
|
|||||||
if 'title' in kwargs:
|
if 'title' in kwargs:
|
||||||
params['title'] = kwargs['title']
|
params['title'] = kwargs['title']
|
||||||
if 'tags' in kwargs and isinstance(kwargs['tags'], list):
|
if 'tags' in kwargs and isinstance(kwargs['tags'], list):
|
||||||
params['tags'] = kwargs['tags']
|
params['tags'] = ', '.join(kwargs['tags'])
|
||||||
if 'archive' in kwargs and int(kwargs['archive']) in (0, 1):
|
if 'archive' in kwargs and int(kwargs['archive']) in (0, 1):
|
||||||
params['archive'] = int(kwargs['archive'])
|
params['archive'] = int(kwargs['archive'])
|
||||||
if 'star' in kwargs and int(kwargs['star']) in (0, 1):
|
if 'star' in kwargs and int(kwargs['star']) in (0, 1):
|
||||||
params['star'] = int(kwargs['star'])
|
params['star'] = int(kwargs['star'])
|
||||||
if 'delete' in kwargs and int(kwargs['delete']) in (0, 1):
|
if 'delete' in kwargs and int(kwargs['delete']) in (0, 1):
|
||||||
params['delete'] = int(kwargs['delete'])
|
params['delete'] = int(kwargs['delete'])
|
||||||
|
path = '/api/entries/{entry}.{ext}'.format(
|
||||||
path = '/api/entries/{entry}.{ext}'.format(entry=entry, ext=self.format)
|
entry=entry, ext=self.format)
|
||||||
return self.query(path, "patch", **params)
|
return self.query(path, "patch", **params)
|
||||||
|
|
||||||
def delete_entries(self, token, entry):
|
def delete_entries(self, entry):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
DELETE /api/entries/{entry}.{_format}
|
DELETE /api/entries/{entry}.{_format}
|
||||||
|
|
||||||
Delete permanently an entry
|
Delete permanently an entry
|
||||||
|
|
||||||
:param token: the token that identified the user to access the API
|
|
||||||
:param entry: \w+ an integer The Entry ID
|
:param entry: \w+ an integer The Entry ID
|
||||||
:return result
|
:return result
|
||||||
"""
|
"""
|
||||||
params = {'token': token}
|
|
||||||
path = '/api/entries/{entry}.{ext}'.format(entry=entry, ext=self.format)
|
params = {'Authorization': 'Bearer {}'.format(self.token)}
|
||||||
|
path = '/api/entries/{entry}.{ext}'.format(
|
||||||
|
entry=entry, ext=self.format)
|
||||||
return self.query(path, "delete", **params)
|
return self.query(path, "delete", **params)
|
||||||
|
|
||||||
def get_entry_tags(self, token, entry):
|
def get_entry_tags(self, entry):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
GET /api/entries/{entry}/tags.{_format}
|
GET /api/entries/{entry}/tags.{_format}
|
||||||
|
|
||||||
Retrieve all tags for an entry
|
Retrieve all tags for an entry
|
||||||
|
|
||||||
:param token: the token that identified the user to access the API
|
|
||||||
:param entry: \w+ an integer The Entry ID
|
:param entry: \w+ an integer The Entry ID
|
||||||
:return data related to the ext
|
:return data related to the ext
|
||||||
"""
|
"""
|
||||||
params = {'token': token}
|
params = {'access_token': self.token}
|
||||||
url = '/api/entries/{entry}/tags.{ext}'.format(entry=entry, ext=self.format)
|
url = '/api/entries/{entry}/tags.{ext}'.format(
|
||||||
|
entry=entry, ext=self.format)
|
||||||
return self.query(url, "get", **params)
|
return self.query(url, "get", **params)
|
||||||
|
|
||||||
def post_entry_tags(self, token, entry, tags):
|
def post_entry_tags(self, entry, tags):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
POST /api/entries/{entry}/tags.{_format}
|
POST /api/entries/{entry}/tags.{_format}
|
||||||
|
|
||||||
Add one or more tags to an entry
|
Add one or more tags to an entry
|
||||||
|
|
||||||
:param token: the token that identified the user to access the API
|
|
||||||
:param entry: \w+ an integer The Entry ID
|
:param entry: \w+ an integer The Entry ID
|
||||||
:param tags: string
|
:param tags: string
|
||||||
:return result
|
:return result
|
||||||
"""
|
"""
|
||||||
params = {'token': token, 'tags': []}
|
params = {'access_token': self.token, 'tags': []}
|
||||||
if isinstance(tags, list):
|
if isinstance(tags, list):
|
||||||
params['tags'] = tags
|
params['tags'] = tags
|
||||||
path = '/api/entries/{entry}/tags.{ext}'.format(entry=entry, ext=self.format)
|
path = '/api/entries/{entry}/tags.{ext}'.format(
|
||||||
|
entry=entry, ext=self.format)
|
||||||
return self.query(path, "post", **params)
|
return self.query(path, "post", **params)
|
||||||
|
|
||||||
def delete_entry_tag(self, token, entry, tag):
|
def delete_entry_tag(self, entry, tag):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
DELETE /api/entries/{entry}/tags/{tag}.{_format}
|
DELETE /api/entries/{entry}/tags/{tag}.{_format}
|
||||||
|
|
||||||
Permanently remove one tag for an entry
|
Permanently remove one tag for an entry
|
||||||
|
|
||||||
:param token: the token that identified the user to access the API
|
|
||||||
:param entry: \w+ an integer The Entry ID
|
:param entry: \w+ an integer The Entry ID
|
||||||
:param tag: string The Tag
|
:param tag: string The Tag
|
||||||
:return data related to the ext
|
:return data related to the ext
|
||||||
"""
|
"""
|
||||||
params = {'token': token}
|
params = {'access_token': self.token}
|
||||||
url = '/api/entries/{entry}/tags/{tag}.{ext}'.format(entry=entry, tag=tag, ext=self.format)
|
url = '/api/entries/{entry}/tags/{tag}.{ext}'.format(
|
||||||
|
entry=entry, tag=tag, ext=self.format)
|
||||||
return self.query(url, "delete", **params)
|
return self.query(url, "delete", **params)
|
||||||
|
|
||||||
def get_tags(self, token):
|
def get_tags(self):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
GET /api/tags.{_format}
|
GET /api/tags.{_format}
|
||||||
|
|
||||||
Retrieve all tags
|
Retrieve all tags
|
||||||
|
|
||||||
:param token: the token that identified the user to access the API
|
|
||||||
:return data related to the ext
|
:return data related to the ext
|
||||||
"""
|
"""
|
||||||
params = {'token': token}
|
params = {'access_token': self.token}
|
||||||
path = '/api/tags.{ext}'.format(ext=self.format)
|
path = '/api/tags.{ext}'.format(ext=self.format)
|
||||||
return self.query(path, "get", **params)
|
return self.query(path, "get", **params)
|
||||||
|
|
||||||
def get_tag(self, token, tag):
|
def delete_tag(self, tag):
|
||||||
"""
|
|
||||||
|
|
||||||
GET /api/tags/{tag}.{_format}
|
|
||||||
|
|
||||||
Retrieve a single tag
|
|
||||||
|
|
||||||
:param token: the token that identified the user to access the API
|
|
||||||
:param tag: string The Tag
|
|
||||||
:return data related to the ext
|
|
||||||
"""
|
|
||||||
path = '/api/tags/{tag}.{ext}'.format(tag=tag, ext=self.format)
|
|
||||||
params = {'token': token}
|
|
||||||
return self.query(path, "get", **params)
|
|
||||||
|
|
||||||
def delete_tag(self, token, tag):
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
DELETE /api/tags/{tag}.{_format}
|
DELETE /api/tags/{tag}.{_format}
|
||||||
|
|
||||||
Permanently remove one tag from every entry
|
Permanently remove one tag from every entry
|
||||||
|
|
||||||
:param token: the token that identified the user to access the API
|
|
||||||
:param tag: string The Tag
|
:param tag: string The Tag
|
||||||
:return data related to the ext
|
:return data related to the ext
|
||||||
"""
|
"""
|
||||||
path = '/api/tags/{tag}.{ext}'.format(tag=tag, ext=self.format)
|
path = '/api/tags/{tag}.{ext}'.format(tag=tag, ext=self.format)
|
||||||
params = {'token': token}
|
params = {'access_token': self.token}
|
||||||
return self.query(path, "delete", **params)
|
return self.query(path, "delete", **params)
|
||||||
|
|
||||||
|
def get_token(self, params):
|
||||||
|
"""
|
||||||
|
:param params: will contain :
|
||||||
|
|
||||||
|
params = {"grant_type": "password",
|
||||||
|
"client_id": "a string",
|
||||||
|
"client_secret": "a string",
|
||||||
|
"username": "a login",
|
||||||
|
"password": "a password"}
|
||||||
|
|
||||||
|
:return: access token
|
||||||
|
"""
|
||||||
|
path = "/oauth/v2/token"
|
||||||
|
return self.query(path, "get_token", **params)['access_token']
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user