Skip to content

Commit

Permalink
First changes for the flattr integration to better work when offline
Browse files Browse the repository at this point in the history
Better flattr integration when offline (bug 1576)

Reset the worker_thread variable when the worker thread is finished (bug 1576)

change the name of the flattr cache database
  • Loading branch information
brot committed Sep 2, 2012
1 parent 50501c8 commit 82d36f3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
61 changes: 60 additions & 1 deletion src/gpodder/flattr.py
Expand Up @@ -23,6 +23,8 @@
# Bernd Schlapsi <brot@gmx.info> 2012-05-26
#

import atexit
import os
import urllib
import urllib2
import urlparse
Expand All @@ -31,14 +33,24 @@
import logging
logger = logging.getLogger(__name__)

from gpodder import minidb
from gpodder import util

import gpodder

_ = gpodder.gettext


class FlattrAction(object):
__slots__ = {'url': str}

def __init__(self, url):
self.url = url


class Flattr(object):
STORE_FILE = 'flattr.cache'

KEY = 'DD2bUSu1TJ7voHz9yNgtC7ld54lKg29Kw2MhL68uG5QUCgT1UZkmXvpSqBtxut7R'
SECRET = 'lJYWGXhcTXWm4FdOvn0iJg1ZIkm3DkKPTzCpmJs5xehrKk55yWe736XCg9vKj5p3'

Expand All @@ -62,6 +74,43 @@ class Flattr(object):
def __init__(self, config):
self._config = config

self._store = minidb.Store(os.path.join(gpodder.home, self.STORE_FILE))
self._worker_thread = None
atexit.register(self._at_exit)

def _at_exit(self):
self._worker_proc()
self._store.close()

def _worker_proc(self):
self._store.commit()
if not self.api_reachable():
self._worker_thread = None
return

logger.debug('Processing stored flattr actions...')
for flattr_action in self._store.load(FlattrAction):
success, message = self.flattr_url(flattr_action.url)
if success:
self._store.remove(flattr_action)
self._store.commit()
self._worker_thread = None

def api_reachable(self):
reachable, response = util.website_reachable(self.API_BASE)
if not reachable:
return False

try:
content = response.readline()
content = json.loads(content)
if 'message' in content and content['message'] == 'hello_world':
return True
except ValueError as err:
pass

return False

def request(self, url, data=None):
headers = {'Content-Type': 'application/json'}

Expand All @@ -78,6 +127,8 @@ def request(self, url, data=None):
response = util.urlopen(url, headers, data)
except urllib2.HTTPError, error:
return {'_gpodder_statuscode': error.getcode()}
except urllib2.URLError, error:
return {'_gpodder_no_connection': False}

if response.getcode() == 200:
return json.loads(response.read())
Expand Down Expand Up @@ -163,6 +214,14 @@ def flattr_url(self, payment_url):
return (True, _('Already flattred or own item'))
else:
return (False, _('Invalid request'))

if '_gpodder_no_connection' in content:
if not self._store.get(FlattrAction, url=payment_url):
flattr_action = FlattrAction(payment_url)
self._store.save(flattr_action)
return (False, _('No internet connection'))

if self._worker_thread is None:
self._worker_thread = util.run_in_background(lambda: self._worker_proc(), True)

return (True, content.get('description', _('No description')))

2 changes: 1 addition & 1 deletion src/gpodder/gtkui/flattr.py
Expand Up @@ -34,7 +34,7 @@


def set_flattr_button(flattr, payment_url, widget_image, widget_button):
if not payment_url:
if not flattr.api_reachable() or not payment_url:
widget_image.hide()
widget_button.hide()
return False
Expand Down
3 changes: 0 additions & 3 deletions src/gpodder/gtkui/interface/shownotes.py
Expand Up @@ -19,9 +19,6 @@

import gtk

import logging
logger = logging.getLogger(__name__)

import gpodder

_ = gpodder.gettext
Expand Down
13 changes: 13 additions & 0 deletions src/gpodder/util.py
Expand Up @@ -1636,3 +1636,16 @@ def run_in_background(function, daemon=False):
thread.start()
return thread


def website_reachable(url='http://www.google.com'):
"""
Check if a specific website is available.
"""
try:
response = urllib2.urlopen(url, timeout=1)
return (True, response)
except urllib2.URLError as err:
pass

return (False, None)

0 comments on commit 82d36f3

Please sign in to comment.