1
0
mirror of https://github.com/aquatix/digimarks.git synced 2025-12-06 20:55:10 +01:00

Download and save favicon of the domain of a bookmark

This commit is contained in:
2016-07-19 15:23:20 +02:00
parent 6f7dbeecba
commit 2384d062da
3 changed files with 28 additions and 1 deletions

View File

@@ -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))

View File

View File

@@ -29,6 +29,12 @@
<a href="{{ url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash) }}">edit</a>
</div>
</div>
<div class="card-action">
{% if bookmark.favicon %}
<img src="{{ url_for('static', filename='favicons/' + bookmark.favicon) }}" />
{% endif %}
<a href="{{ url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash) }}">edit</a>
</div>
</div>
</div>
{% endfor %}