Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
deal with expired token of wallabag
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Demah committed Apr 30, 2016
1 parent 523347e commit 86e2594
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ for pelican support

for wallabag support

* `wallabag_api <https://pypi.python.org/pypi/wallabag_api>`_ == 1.0.1
* `wallabag_api <https://pypi.python.org/pypi/wallabag_api>`_ == 1.1.0

and finally :

Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion django_th/__init__.py
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
]

extras_require_wallabag = [
'wallabag_api==1.0.1',
'wallabag_api==1.1.0',
]

extras_require_all = extras_require_github\
Expand Down
58 changes: 45 additions & 13 deletions th_wallabag/my_wallabag.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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
"""
Expand All @@ -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(
Expand All @@ -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):
Expand Down Expand Up @@ -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)

0 comments on commit 86e2594

Please sign in to comment.