mirror of
https://github.com/aquatix/digimarks.git
synced 2025-12-07 09:35:10 +01:00
Initial version, based on great example found at
http://charlesleifer.com/blog/building-bookmarking-service-python-and-phantomjs/
This commit is contained in:
70
digimarks.py
Normal file
70
digimarks.py
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
import datetime
|
||||||
|
import hashlib
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
from flask import Flask, abort, redirect, render_template, request
|
||||||
|
from flask_peewee.db import Database
|
||||||
|
from flask_peewee.utils import object_list
|
||||||
|
from peewee import *
|
||||||
|
|
||||||
|
# app configuration
|
||||||
|
APP_ROOT = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
MEDIA_ROOT = os.path.join(APP_ROOT, 'static')
|
||||||
|
MEDIA_URL = '/static/'
|
||||||
|
DATABASE = {
|
||||||
|
'name': os.path.join(APP_ROOT, 'bookmarks.db'),
|
||||||
|
'engine': 'peewee.SqliteDatabase',
|
||||||
|
}
|
||||||
|
PASSWORD = 'shh'
|
||||||
|
PHANTOM = '/usr/local/bin/phantomjs'
|
||||||
|
SCRIPT = os.path.join(APP_ROOT, 'screenshot.js')
|
||||||
|
|
||||||
|
# create our flask app and a database wrapper
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.config.from_object(__name__)
|
||||||
|
db = Database(app)
|
||||||
|
|
||||||
|
class Bookmark(db.Model):
|
||||||
|
url = CharField()
|
||||||
|
created_date = DateTimeField(default=datetime.datetime.now)
|
||||||
|
image = CharField(default='')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
ordering = (('created_date', 'desc'),)
|
||||||
|
|
||||||
|
def fetch_image(self):
|
||||||
|
url_hash = hashlib.md5(self.url).hexdigest()
|
||||||
|
filename = 'bookmark-%s.png' % url_hash
|
||||||
|
|
||||||
|
outfile = os.path.join(MEDIA_ROOT, filename)
|
||||||
|
params = [PHANTOM, SCRIPT, self.url, outfile]
|
||||||
|
|
||||||
|
exitcode = subprocess.call(params)
|
||||||
|
if exitcode == 0:
|
||||||
|
self.image = os.path.join(MEDIA_URL, filename)
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
return object_list('index.html', Bookmark.select())
|
||||||
|
|
||||||
|
@app.route('/add/')
|
||||||
|
def add():
|
||||||
|
password = request.args.get('password')
|
||||||
|
if password != PASSWORD:
|
||||||
|
abort(404)
|
||||||
|
|
||||||
|
url = request.args.get('url')
|
||||||
|
if url:
|
||||||
|
bookmark = Bookmark(url=url)
|
||||||
|
bookmark.fetch_image()
|
||||||
|
bookmark.save()
|
||||||
|
return redirect(url)
|
||||||
|
abort(404)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
# create the bookmark table if it does not exist
|
||||||
|
Bookmark.create_table(True)
|
||||||
|
|
||||||
|
# run the application
|
||||||
|
app.run()
|
||||||
34
templates/index.html
Normal file
34
templates/index.html
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Bookmarks</title>
|
||||||
|
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/bootstrap.min.css') }}" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<div class="row">
|
||||||
|
<div class="page-header">
|
||||||
|
<h1>Bookmarks</h1>
|
||||||
|
</div>
|
||||||
|
<ul class="thumbnails">
|
||||||
|
{% for bookmark in object_list %}
|
||||||
|
<li class="span6">
|
||||||
|
<div class="thumbnail">
|
||||||
|
<a href="{{ bookmark.url }}" title="{{ bookmark.url }}">
|
||||||
|
<img style="width:450px;" src="{{ bookmark.image }}" />
|
||||||
|
</a>
|
||||||
|
<p><a href="{{ bookmark.url }}">{{ bookmark.url|urlize(25) }}</a></p>
|
||||||
|
<p>{{ bookmark.created_date.strftime("%m/%d/%Y %H:%M") }}</p>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<div class="pagination">
|
||||||
|
{% if page > 1 %}<a href="./?page={{ page - 1 }}">Previous</a>{% endif %}
|
||||||
|
{% if pagination.get_pages() > page %}<a href="./?page={{ page + 1 }}">Next</a>{% endif %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user