From aee0515eaefbad67413581a9ffcbe36104172bec Mon Sep 17 00:00:00 2001 From: Michiel Scholten Date: Fri, 23 Mar 2018 14:21:15 +0100 Subject: [PATCH] Support for fallback icon, clear empty icon; don't re-download existing --- digimarks.py | 15 +++++++++++++-- static/faviconfallback.png | Bin 0 -> 243 bytes templates/cards.html | 2 ++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 static/faviconfallback.png diff --git a/digimarks.py b/digimarks.py index 4bf60da..e952fa5 100644 --- a/digimarks.py +++ b/digimarks.py @@ -306,7 +306,6 @@ class Bookmark(BaseModel): def _set_favicon_with_iconsbetterideaorg(self, domain): """ Fetch favicon for the domain """ - # TODO: if file exists, don't re-download it fileextension = '.png' meta = requests.head('http://icons.better-idea.org/icon?size=60&url=' + domain, allow_redirects=True, headers={'User-Agent': DIGIMARKS_USER_AGENT}) if meta.url[-3:].lower() == 'ico': @@ -329,7 +328,6 @@ class Bookmark(BaseModel): def _set_favicon_with_realfavicongenerator(self, domain): """ Fetch favicon for the domain """ - # TODO: if file exists, don't re-download it response = requests.get( 'https://realfavicongenerator.p.mashape.com/favicon/icon?platform=android_chrome&site=' + domain, stream=True, @@ -374,6 +372,14 @@ class Bookmark(BaseModel): """ Fetch favicon for the domain """ u = urlparse(self.url) domain = u.netloc + if os.path.isfile(os.path.join(MEDIA_ROOT, 'favicons/' + domain + '.png')): + # If file exists, don't re-download it + self.favicon = domain + '.png' + return + if os.path.isfile(os.path.join(MEDIA_ROOT, 'favicons/' + domain + '.ico')): + # If file exists, don't re-download it + self.favicon = domain + '.ico' + return #self._set_favicon_with_iconsbetterideaorg(domain) self._set_favicon_with_realfavicongenerator(domain) @@ -963,7 +969,12 @@ def findmissingfavicons(systemkey): try: if not bookmark.favicon or not os.path.isfile(os.path.join(MEDIA_ROOT, 'favicons/' + bookmark.favicon)): # This favicon is missing + # Clear favicon, so fallback can be used instead of showing a broken image + bookmark.favicon = None + bookmark.save() + # Try to fetch and save new favicon bookmark.set_favicon() + bookmark.save() except OSError as e: print(e) return redirect('/') diff --git a/static/faviconfallback.png b/static/faviconfallback.png new file mode 100644 index 0000000000000000000000000000000000000000..69ac5d6dcfbd216ebcafeae0cdf4b65dcc887d38 GIT binary patch literal 243 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I0wfs{c7_5;rX+877Y2q^y~;*F9%q3^WHAGS zz6uC4_J;iE0t&L1c>21sKVlZ=v1I#v`icoqsKwL8F(l*O+nWoy8Vp2QA6iESH@n6= z?0&HLz2Y~Pr0p|BVy;TY79BJ=_oaF {% if bookmark.favicon %}
+ {% else %} +
{% endif %} {% if bookmark.http_status != 200 and bookmark.http_status != 304 %}
report_problem