That's a web-app for reading with translations. Try it out.
commit
321f41e8fd
|
@ -0,0 +1,147 @@
|
|||
# all the imports
|
||||
from __future__ import with_statement
|
||||
from contextlib import closing
|
||||
import sqlite3
|
||||
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash, jsonify
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
import re
|
||||
from email.mime.text import MIMEText
|
||||
from subprocess import Popen, PIPE
|
||||
import smtplib
|
||||
|
||||
# configuration
|
||||
DATABASE = '/tmp/dysprosium.db'
|
||||
DEBUG = True
|
||||
SECRET_KEY = 'development key'
|
||||
USERNAME = 'admin'
|
||||
PASSWORD = 'dupa.8'
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(__name__)
|
||||
|
||||
|
||||
def connect_db():
|
||||
return sqlite3.connect(app.config['DATABASE'])
|
||||
|
||||
def init_db():
|
||||
with closing(connect_db()) as db:
|
||||
with app.open_resource('schema.sql') as f:
|
||||
db.cursor().executescript(f.read())
|
||||
db.commit()
|
||||
|
||||
@app.before_request
|
||||
def before_request():
|
||||
g.db = connect_db()
|
||||
|
||||
@app.teardown_request
|
||||
def teardown_request(exception):
|
||||
g.db.close()
|
||||
|
||||
@app.route('/')
|
||||
def ask_for_text():
|
||||
|
||||
return render_template('ask_for_text.html')
|
||||
|
||||
@app.route('/options', methods=['POST'])
|
||||
def add_entry():
|
||||
if not session.get('logged_in'):
|
||||
abort(401)
|
||||
g.db.execute('insert into entries (title, text) values (?, ?)',
|
||||
[request.form['title'], request.form['text']])
|
||||
g.db.commit()
|
||||
flash('New entry was successfully posted')
|
||||
return redirect(url_for('ask_for_text.html'))
|
||||
|
||||
|
||||
@app.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
error = None
|
||||
if request.method == 'POST':
|
||||
if request.form['username'] != app.config['USERNAME']:
|
||||
error = 'Invalid username'
|
||||
elif request.form['password'] != app.config['PASSWORD']:
|
||||
error = 'Invalid password'
|
||||
else:
|
||||
session['logged_in'] = True
|
||||
flash('You were logged in')
|
||||
return redirect(url_for('ask_for_text'))
|
||||
return render_template('login.html', error=error)
|
||||
|
||||
|
||||
@app.route('/logout')
|
||||
def logout():
|
||||
session.pop('logged_in', None)
|
||||
flash('You were logged out')
|
||||
return redirect(url_for('ask_for_text'))
|
||||
|
||||
|
||||
def get_exact_translation(retrieved_json):
|
||||
try:
|
||||
response = retrieved_json["term0"]["PrincipalTranslations"]["0"]["FirstTranslation"]["term"]
|
||||
if len(response) == 0:
|
||||
response = "No translation"
|
||||
except:
|
||||
response = "No translation"
|
||||
return response
|
||||
|
||||
@app.route('/_find_translations')
|
||||
def find_translations():
|
||||
text_for_translation = request.args.get('text', 0)
|
||||
language = request.args.get('language', 0)
|
||||
dictionary_link = "http://api.wordreference.com/a95ae/json/" + language + "/"
|
||||
words = text_for_translation.split(" ")
|
||||
translations = []
|
||||
for word in words:
|
||||
translation = get_exact_translation(requests.get(dictionary_link + strip_punctuation(word)).json)
|
||||
translations.append(translation)
|
||||
|
||||
return jsonify(result=translations, words=words, html_output=prepare_html_output(words))
|
||||
|
||||
|
||||
@app.route('/_send_mail')
|
||||
def send_mail():
|
||||
from_address = "attero@hackerspace.pl"
|
||||
to_address = request.args.get('mail_address', 0)
|
||||
message_text = request.args.get('message', 0)
|
||||
msg = ("From: %s\r\nTo: %s\r\nSubject: Lanre\r\n" % (from_address, to_address))
|
||||
server = smtplib.SMTP('hackerspace.pl', 587)
|
||||
server.starttls()
|
||||
server.login("attero", "dupa.8")
|
||||
server.set_debuglevel(1)
|
||||
print(from_address)
|
||||
print(to_address)
|
||||
print(msg + message_text)
|
||||
print("somethin wrong in loging to server")
|
||||
server.sendmail(from_address, to_address, msg + message_text)
|
||||
server.quit()
|
||||
return jsonify(result="success")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def prepare_html_output(words):
|
||||
words_with_html = ["<span class='word' id='" + str(i) +"'>" + word + "</span>" for i, word in enumerate(words)]
|
||||
output = " ".join(words_with_html)
|
||||
return output
|
||||
|
||||
|
||||
def strip_punctuation(word):
|
||||
try:
|
||||
if(word[len(word)-1].isalpha()):
|
||||
return word
|
||||
else:
|
||||
return word[0:(len(word)-1)]
|
||||
except:
|
||||
return word
|
||||
|
||||
|
||||
#a95ae api key for word reference is: a95ae
|
||||
#http://www.wordreference.com/docs/api.aspx
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
init_db()
|
||||
app.run()
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
drop table if exists entries;
|
||||
create table entries (
|
||||
id integer primary key autoincrement,
|
||||
title string not null,
|
||||
text string not null
|
||||
);
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
Binary file not shown.
After Width: | Height: | Size: 8.6 KiB |
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
|
@ -0,0 +1 @@
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
{% extends "layout.html" %}
|
||||
{% block body %}
|
||||
<div class="row-fluid">
|
||||
<ul class="nav nav-pills" id="language-options">
|
||||
<li id="lifren" class="active">
|
||||
<a href="#language-options">French English</a>
|
||||
</li>
|
||||
<li id="liiten">
|
||||
<a href="#language-options">Italian English</a>
|
||||
</li>
|
||||
<li id="lienit">
|
||||
<a href="#language-options">English Italian</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="row-fluid">
|
||||
<div class="span10">
|
||||
<p>Put your text below:</p>
|
||||
<textarea id="input-text" cols="20" rows="30"></textarea>
|
||||
</br>
|
||||
<div >
|
||||
<p id="translated-text"></p>
|
||||
</div>
|
||||
<button class="btn btn-info btn-large" id="submit-text">Translate it for me!</button>
|
||||
<button class="btn-info btn" id="change-text" >I want to translate something else...</button>
|
||||
<button class="btn-info btn" id="change-language-translate">Translate it again, in different language!</button>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<div class="span2">
|
||||
<p id="communicates" class="alert alert-info"></p>
|
||||
<div id="current-translation" class="alert alert-success"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
|
|
@ -0,0 +1,224 @@
|
|||
<!doctype html>
|
||||
<title>Lanre</title>
|
||||
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='bootstrap.css') }}">
|
||||
|
||||
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
|
||||
<style type="text/css">
|
||||
body {
|
||||
padding-top: 40px;
|
||||
padding-bottom: 40px;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="container">
|
||||
|
||||
<div class="hero-unit">
|
||||
<h1>Designed for your pleasure of reading foreign texts</h1>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="row-fluid">
|
||||
|
||||
<div class="span8">
|
||||
{% for message in get_flashed_messages() %}
|
||||
<div class=flash>{{ message }}</div>
|
||||
{% endfor %}
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="row-fluid">
|
||||
{% block body %}{% endblock %}
|
||||
</div><div class="row-fluid">
|
||||
|
||||
|
||||
<div class=" well-small">
|
||||
<button class="btn btn-info" id="show-me-my-words">I added words. Now what?</button>
|
||||
</br>
|
||||
|
||||
<div id="show-words" class="well-large well">
|
||||
<div class="alert alert-info" id="my-words">
|
||||
</div>
|
||||
|
||||
<textarea cols="20" rows="1" placeholder="me@email.com" id="email"></textarea>
|
||||
<button class="btn btn-info btn-large" id="send-words">Send them to me!</button>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<script type=text/javascript>
|
||||
$SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
|
||||
$LANGUAGE = "fren";
|
||||
</script>
|
||||
|
||||
<script type="text/javascript">
|
||||
var communicates = "Here will be placed other info. Hover to get translation. Click to add for further reference. Insert your text and translate it."
|
||||
var translated_words;
|
||||
var original_words;
|
||||
var message;
|
||||
var words_to_be_remembered = new Array();
|
||||
$(document).ready( function() {
|
||||
$("#change-text").hide();
|
||||
$("#change-language-translate").hide();
|
||||
$("#current-translation").hide();
|
||||
$("#show-words").hide();
|
||||
$("#show-me-my-words").hide();
|
||||
change_communicat(communicates);
|
||||
|
||||
|
||||
$("#send-words").click(function() {
|
||||
var address;
|
||||
address = $("#email").val();
|
||||
console.log(address);
|
||||
console.log(message);
|
||||
send_mail(address, message);
|
||||
var text_for_translation;
|
||||
});
|
||||
|
||||
$("#submit-text").click(function() {
|
||||
|
||||
text_for_translation = $("#input-text").val();
|
||||
|
||||
//some magic
|
||||
get_translation(text_for_translation);
|
||||
|
||||
//var translated_text = text_for_translation;
|
||||
//$("#translated-text").html(translated_text);
|
||||
$("#submit-text").hide();
|
||||
$("#change-text").show();
|
||||
$("#change-language-translate").show();
|
||||
$("#input-text").hide();
|
||||
});
|
||||
|
||||
$("#change-language-translate").click(function() {
|
||||
|
||||
var text_for_translation;
|
||||
text_for_translation = $("#input-text").val();
|
||||
|
||||
|
||||
//some magic
|
||||
get_translation(text_for_translation);
|
||||
|
||||
//var translated_text = text_for_translation;
|
||||
//$("#translated-text").html(translated_text);
|
||||
$("#submit-text").hide();
|
||||
$("#change-text").show();
|
||||
$("#input-text").hide();
|
||||
$("#show-words").hide();
|
||||
$("#show-me-my-words").hide();
|
||||
});
|
||||
|
||||
$("#change-text").click(function() {
|
||||
|
||||
$("#translated-text").html("");
|
||||
$("#input-text").show().html("");
|
||||
$("#submit-text").show();
|
||||
$("#change-text").hide();
|
||||
$("#change-language-translate").hide();
|
||||
change_communicat("Insert some text!" );
|
||||
$("#current-translation").hide();
|
||||
$("#show-words").hide();
|
||||
$("#show-me-my-words").hide();
|
||||
});
|
||||
|
||||
$("#lifren").click(function() {
|
||||
$LANGUAGE = "fren";
|
||||
$(this).addClass("active");
|
||||
$("#liiten").removeClass("active");
|
||||
$("#lienit").removeClass("active");
|
||||
});
|
||||
|
||||
$("#liiten").click(function() {
|
||||
$LANGUAGE = "iten";
|
||||
$(this).addClass("active");
|
||||
$("#lifren").removeClass("active");
|
||||
$("#lienit").removeClass("active");
|
||||
});
|
||||
|
||||
$("#lienit").click(function() {
|
||||
$LANGUAGE = "enit";
|
||||
$(this).addClass("active");
|
||||
$("#lifren").removeClass("active");
|
||||
$("#liiten").removeClass("active");
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
$("#show-me-my-words").click( function() {
|
||||
show_added_words();
|
||||
$(this).hide();
|
||||
|
||||
});
|
||||
|
||||
function show_added_words() {
|
||||
$("#show-words").show();
|
||||
var words = "My words are:\n";
|
||||
var i = 0;
|
||||
console.log(words_to_be_remembered);
|
||||
var local_message = "Words for you from Lanre:\n";
|
||||
words_to_be_remembered.forEach(function(word, index){
|
||||
words += "<p>" +index + ": " + word + "</p>";
|
||||
local_message += index + ": " + word + "\n";
|
||||
});
|
||||
message = local_message;
|
||||
$("#my-words").html(words)
|
||||
}
|
||||
|
||||
function change_communicat(communicat) {
|
||||
communicates = communicat;
|
||||
$("#communicates").text(communicates);
|
||||
}
|
||||
|
||||
function get_translation(text_for_translation) {
|
||||
|
||||
$(function() {
|
||||
change_communicat("It may take some time..." );
|
||||
$.getJSON( $SCRIPT_ROOT + '/_find_translations', {
|
||||
text:text_for_translation,
|
||||
language:$LANGUAGE
|
||||
}, function(data) {
|
||||
$("#translated-text").html(data.html_output);
|
||||
translated_words = data.result;
|
||||
original_words = data.words;
|
||||
change_communicat("Done! Now you can hover on words or click them, for further reference." );
|
||||
$(".word").click(function() {
|
||||
var which_one = this.id;
|
||||
var word_pair = original_words[which_one] +": " +translated_words[which_one];
|
||||
words_to_be_remembered.push(word_pair);
|
||||
$("#show-me-my-words").show();
|
||||
change_communicat("You've just added a " + word_pair + " for further reference" );
|
||||
$("#current-translation").show();
|
||||
$("#current-translation").text(translated_words[which_one]);
|
||||
});
|
||||
|
||||
$(".word").hover(function() {
|
||||
var which_one = this.id;
|
||||
$("#current-translation").show();
|
||||
$("#current-translation").text(translated_words[which_one]);
|
||||
});
|
||||
|
||||
});
|
||||
return false;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function send_mail(address, message) {
|
||||
change_communicat("I'm sending you mail..." );
|
||||
$.getJSON( $SCRIPT_ROOT + '/_send_mail', {
|
||||
mail_address:address,
|
||||
message:message
|
||||
}, function(data) {
|
||||
change_communicat("Mail should be sent by now." );
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
</script>
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
{% extends "layout.html" %}
|
||||
{% block log %}
|
||||
<h2>Login</h2>
|
||||
{% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %}
|
||||
<form action="{{ url_for('login') }}" method=post>
|
||||
<dl>
|
||||
<dt>Username:
|
||||
<dd><input type=text name=username>
|
||||
<dt>Password:
|
||||
<dd><input type=password name=password>
|
||||
<dd><input type=submit value=Login>
|
||||
</dl>
|
||||
</form>
|
||||
{% endblock %}
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -0,0 +1 @@
|
|||
|
Loading…
Reference in New Issue