novi fajli za imporatnje in prikazovanje linkov

master
janko 2023-04-08 23:43:52 +02:00
parent db037c62fa
commit be6a3242f9
9 changed files with 204 additions and 0 deletions

63
import_link.py 100644
View File

@ -0,0 +1,63 @@
from flask import (
Blueprint, render_template, request, session, flash
)
from create_db import Card, get_session
from link_handler import check_response
bp = Blueprint('import_link', __name__, url_prefix='/import_link')
@bp.route("/", methods=["GET", "POST"])
def index():
username = session["username"]
return render_template("import_link.html", username=username)
@bp.route('/import', methods = ('GET', 'POST'))
def import_link():
username = session['username']
user_id = session['user_id']
if request.method == 'POST':
import_type = request.form.get("import_type", False)
print(import_type)
if import_type == "import_txt":
print("!2313232321")
else:
link = request.form.get("link", False)
if check_response(link) == 1:
print(link, 'is ok')
commit_link(link=link, user_id=user_id)
else:
print(link, 'is not ok')
return render_template("import_link.html", user_id=user_id, username=username)
def commit_link(link, user_id):
dbsession = get_session()
existing_link = dbsession.query(Card).filter_by(owner_id=user_id, item_location=link).first()
if existing_link:
flash(f"{link} has already been imported.")
else:
#add card
#TODO ni razlike med title in vsebino urlja ??
card = Card(title=link,
interest_rate=-1.0,
owner_id=user_id,
item_location=link,
last_review=None,
card_type="URL",
share_id=0)
dbsession.add(card)
dbsession.commit()
flash(f"{link} imported successfully")
dbsession.close()
def import_list_of_links(list_of_links, user_id):
#TODO nekak bi bilo dobro, da pokaže kateri so failali....
for link in list_of_links:
commit_link(link, user_id)

63
link_handler.py 100644
View File

@ -0,0 +1,63 @@
import requests
import re
from flask import render_template
from urllib.parse import urlparse
def check_response(link):
"""
responds 1 if link is ok
2 if there's conncetion error
3 if link is invalid (no "http" for example)
"""
parsed_url = urlparse(link)
if parsed_url.scheme and parsed_url.netloc:
try:
response = requests.head(link)
if response.status_code == 200:
return 1
else:
#TODO drugi statusi code?
return "WEIRD"
except requests.exceptions.RequestException:
print("conncetion error")
return 2
else:
print("link not valid (https:// missing??)")
return 3
# kako vse možne linke embeddat?
def render_embed_link(cm_username, card, maybe_in, no_in):
url = card.item_location
if re.match(r'^https?://(www\.)?twitter\.com/.*/status/\d+$', url):
tweet_id = url.split("/")[-1]
return render_template("embed_link/twitter_embed.html",
card=card,
tweet_id=tweet_id,
username=cm_username,
maybe_in=maybe_in,
no_in=no_in)
elif re.match(r'^https?://(www\.)?youtube\.com/watch\?.*v=([-\w]+)', url):
youtube_id = re.search(r'v=([-\w]+)', url).group(1)
return render_template("embed_link/youtube_embed.html",
youtube_id=youtube_id,
card=card,
username=cm_username,
maybe_in=maybe_in,
no_in=no_in)
#TODO kill me wtf about the instances.........
elif re.match(r'^https?://(www\.)?mastodon\.social/@[a-zA-Z0-9]+/\d+', url):
username, post_id = url.split("/")[-3:-1]
return render_template("embed_link/mastodon_embed.html", username=username, post_id=post_id)
else:
#TODO what if iframe fails
return render_template("embed_link/iframe_embed.html",
url=url,
card=card,
username=cm_username,
maybe_in=maybe_in,
no_in=no_in)

View File

@ -0,0 +1,5 @@
{% extends 'link.html' %}
{% block link %}
<iframe src="{{ card.item_location }}" width="100%" height="600px"></iframe>
{% endblock %}

View File

@ -0,0 +1,11 @@
{% extends 'link.html' %}
{% block link %}
<div class="embed">
<blockquote class="twitter-tweet" data-dnt="true">
<a href="https://twitter.com/twitter/status/{{ tweet_id }}"></a>
</blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
{% endblock %}

View File

@ -0,0 +1,6 @@
{% extends 'link.html' %}
{% block link %}
<iframe width="560" height="315" src="https://www.youtube.com/embed/{{ youtube_id }}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
{% endblock %}

View File

@ -0,0 +1,16 @@
{% extends 'partials/base.html' %}
{% block name %}upload{% endblock %}
{% block content %}
<main>
<form method="post" enctype="multipart/form-data" action="/import_link/import">
<ol>
<li> <input placeholder="insert link" type="link" name="import_type"/></li>
<li> <button name="import_type" type="submit" value="import_txt">.txt file list of links</button></li>
<li> <button name="import_type" type="submit" value="import_twitter_bookmarks">import twitter bookmarks</button></li>
<li> <button name="import_type" type="submit" value="import_raindrop_links">import links from raindrop.io</button></li>
</ol>
</form>
</main>
{% endblock %}

View File

@ -0,0 +1,14 @@
{% extends 'partials/base.html' %}
{% block name %}import{% endblock %}
{% block content %}
<main>
<form class="dropzone dropzone-form" method="post" enctype="multipart/form-data" action="/upload/uploader">
<span id="dropzone-txt" class="dropzone-txt">Drop file here or click to upload.</span>
<input class="dropzone-input" type="file" name="file" runat="server" accept=".txt" multiple >
</form>
</main>
<script src="{{ url_for('static', filename='js/dropzone.js') }}"></script>
{% endblock %}

View File

@ -0,0 +1,26 @@
{% extends 'partials/base.html' %}
{% block name %}deck{% endblock %}
{% block content %}
<main>
<article>
{% block link %}{% endblock %}
<footer>
<form method="post">
<input type="hidden" name="card_id" value="{{ card['id'] }}">
<!-- <span id="buttonContainer" style="display: flex; justify-content: space-around;"> -->
<span id="buttonContainer">
<button type="submit" name="rate" value="Yes" accesskey="1">Yes (1)</button> <!--ti keyi so alt+shit+key...-->
<button type="submit" name="rate" value="Maybe" accesskey="2">Maybe ({{maybe_in}})</button>
<button type="submit" name="rate" value="No" accesskey="3">No ({{no_in}})</button>
<button type="submit" name="rate" value="Delete" accesskey="d">Delete</button>
<button type="submit" name="share" value="Share" accesskey="s">Share</button>
</span>
</form>
</footer>
</article>
</main>
{% endblock %}