From 2384d062dac79ce407c941a9f87bfc38d4c7d3fa Mon Sep 17 00:00:00 2001 From: Michiel Scholten Date: Tue, 19 Jul 2016 15:23:20 +0200 Subject: [PATCH] Download and save favicon of the domain of a bookmark --- digimarks.py | 23 ++++++++++++++++++++++- static/favicons/.notempty | 0 templates/bookmarks.html | 6 ++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 static/favicons/.notempty diff --git a/digimarks.py b/digimarks.py index a9eac0d..c3ca2ec 100644 --- a/digimarks.py +++ b/digimarks.py @@ -3,7 +3,9 @@ import hashlib import os import sys import requests +import shutil import bs4 +from urlparse import urlparse from utilkit import datetimeutil @@ -66,7 +68,10 @@ class Bookmark(db.Model): url_hash = CharField() tags = CharField() - # favicon http://codingclues.eu/2009/retrieve-the-favicon-for-any-url-thanks-to-google/ + # Website favicon - http://codingclues.eu/2009/retrieve-the-favicon-for-any-url-thanks-to-google/ + favicon = CharField(null=True) + + # Status code: 200 is OK, 404 is not found, for example (showing an error) http_status = IntegerField(default=200) @@ -108,6 +113,18 @@ class Bookmark(db.Model): return self.http_status + def set_favicon(self): + """ Fetch favicon for the domain """ + u = urlparse(self.url) + domain = u.netloc + filename = os.path.join(MEDIA_ROOT, 'favicons/' + domain + '.png') + response = requests.get('http://www.google.com/s2/favicons?domain=' + domain, stream=True) + with open(filename, 'wb') as out_file: + shutil.copyfileobj(response.raw, out_file) + del response + self.favicon = domain + '.png' + + def to_dict(self): result = { 'title': self.title, @@ -201,6 +218,10 @@ def addingbookmark(userkey): bookmark.set_title_from_source() else: bookmark.set_status_code() + + if bookmark.http_status == 200: + bookmark.set_favicon() + bookmark.save() #return redirect(url) return redirect(url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash)) diff --git a/static/favicons/.notempty b/static/favicons/.notempty new file mode 100644 index 0000000..e69de29 diff --git a/templates/bookmarks.html b/templates/bookmarks.html index 11ca9a3..3d14b7c 100644 --- a/templates/bookmarks.html +++ b/templates/bookmarks.html @@ -29,6 +29,12 @@ edit +
+ {% if bookmark.favicon %} + + {% endif %} + edit +
{% endfor %}