From 86e2594c56fea977d8430a311099b2ad12a0174b Mon Sep 17 00:00:00 2001 From: Olivier Demah Date: Sun, 1 May 2016 00:38:39 +0200 Subject: [PATCH] deal with expired token of wallabag --- README.rst | 4 +-- django_th/__init__.py | 2 +- requirements.txt | 2 +- setup.py | 2 +- th_wallabag/my_wallabag.py | 58 +++++++++++++++++++++++++++++--------- 5 files changed, 50 insertions(+), 18 deletions(-) diff --git a/README.rst b/README.rst index 8f47eaf..7724d8d 100644 --- a/README.rst +++ b/README.rst @@ -131,7 +131,7 @@ for pelican support for wallabag support -* `wallabag_api `_ == 1.0.1 +* `wallabag_api `_ == 1.1.0 and finally : @@ -145,7 +145,7 @@ or to make your own "recipe" : .. code-block:: bash - pip install django-th[rss,pocket] + pip install django-th[rss,wallabag] pip install django-th[rss,twitter,pocket,github] diff --git a/django_th/__init__.py b/django_th/__init__.py index 3db0a3d..cd61012 100644 --- a/django_th/__init__.py +++ b/django_th/__init__.py @@ -1,4 +1,4 @@ -VERSION = (0, 13, 2) # PEP 386 +VERSION = (0, 13, 3) # PEP 386 __version__ = ".".join([str(x) for x in VERSION]) default_app_config = 'django_th.apps.DjangoThConfig' diff --git a/requirements.txt b/requirements.txt index d027a26..4a1f3d1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,4 +14,4 @@ django-haystack==2.4.1 py-trello==0.4.3 twython==3.3.0 awesome-slugify==1.6.5 -wallabag_api==1.0.1 +wallabag_api==1.1.0 diff --git a/setup.py b/setup.py index afd4e98..5bacc71 100644 --- a/setup.py +++ b/setup.py @@ -38,7 +38,7 @@ ] extras_require_wallabag = [ - 'wallabag_api==1.0.1', + 'wallabag_api==1.1.0', ] extras_require_all = extras_require_github\ diff --git a/th_wallabag/my_wallabag.py b/th_wallabag/my_wallabag.py index a38a3c8..bb03f10 100644 --- a/th_wallabag/my_wallabag.py +++ b/th_wallabag/my_wallabag.py @@ -2,9 +2,8 @@ # add here the call of any native lib of python like datetime etc. # add the python API here if needed -from wallabag_api.wallabag import Wallabag +from wallabag_api.wallabag import Wallabag as Wall # django classes -from django.conf import settings from django.core.urlresolvers import reverse from django.utils.log import getLogger from django.core.cache import caches @@ -42,11 +41,17 @@ def __init__(self, token=None): super(ServiceWallabag, self).__init__(token) self.token = token if token: - us = UserService.objects.get(token=token) - self.wall = Wallabag(host=us.host, - client_secret=us.client_secret, - client_id=us.client_id, - token=token) + us = UserService.objects.get(token=token, name='ServiceWallabag') + self.service_username = us.username + self.service_password = us.password + self.service_host = us.host + self.service_client_secret = us.client_secret + self.service_client_id = us.client_id + + self.wall = Wall(host=self.service_host, + client_secret=self.service_client_secret, + client_id=self.service_client_id, + token=self.token) def read_data(self, **kwargs): """ @@ -80,9 +85,9 @@ def save_data(self, trigger_id, **data): let's save the data :param trigger_id: trigger ID from which to save data - :param **data: the data to check to be used and save + :param data: the data to check to be used and save :type trigger_id: int - :type **data: dict + :type data: dict :return: the status of the save statement :rtype: boolean """ @@ -106,8 +111,24 @@ def save_data(self, trigger_id, **data): logger.debug(sentence) status = True except Exception as e: - logger.critical(e) - status = False + if e.errno == 401: + new_token = self.refresh_token() + old_token = self.token + UserService.objects.filter(token=old_token, name='ServiceWallabag').update(token=new_token) + + # new wallabag session with new token + new_wall = Wall(host=self.service_host, + client_secret=self.service_client_secret, + client_id=self.service_client_id, + token=new_token) + try: + return new_wall.post_entries(url=data['link'], title=title, tags=(trigger.tag.lower())) + except Exception as e: + logger.critical(e.errno, e.strerror) + status = False + else: + logger.critical(e.errno, e.strerror) + status = False else: logger.critical( @@ -126,8 +147,8 @@ def auth(self, request): 'password': service.password, 'client_id': service.client_id, 'client_secret': service.client_secret} - acces_token = Wallabag.get_token(host=service.host, **params) - request.session['oauth_token'] = acces_token + access_token = Wall.get_token(host=service.host, **params) + request.session['oauth_token'] = access_token return callback_url def callback(self, request, **kwargs): @@ -157,3 +178,14 @@ def callback(self, request, **kwargs): return '/' return 'wallabag/callback.html' + + def refresh_token(self): + """ + refresh the expired token + :return: boolean + """ + params = {'username': self.service_username, + 'password': self.service_password, + 'client_id': self.service_client_id, + 'client_secret': self.service_client_secret} + return Wall.get_token(host=self.service_host, **params)