Parcourir la source

icon fetch backend ready for merge

control il y a 3 ans
Parent
commit
147965e848

BIN
app/database.db


+ 42 - 0
app/iconfetch.py

@@ -0,0 +1,42 @@
+# python function that takes address
+# parses it for the youtube icon
+# via wget site.com/favicon.ico
+# saves it with propername for reference
+# later on
+import subprocess as sp
+# search via regex
+import re, os
+
+def check_icon(sitename):
+    # checks for site icon in storage
+    str1 = sitename.replace('/', '')
+    sitename = f'{str1.replace(":", ".")}.ico'
+    path = 'static/sitecons/'
+
+    # list comp for searching through dir for file
+    result = [os.path.join(root, sitename) for root, dirs, files in os.walk(path) if sitename in files] 
+
+    if result:
+        return f'{sitename}'
+    else:
+        return False
+
+def fetch_icon(sitename):
+    path = 'static/sitecons/'
+
+    # search end of string for leading slash
+    search_slash = re.findall('\/$', sitename)
+
+    # if theres a slash, no need to append, otherwise
+    if search_slash:
+        parsed_name = f'{sitename}favicon.ico'
+    else:
+        parsed_name = f'{sitename}/favicon.ico'
+
+    # remove chars for icon filename
+    str1 = sitename.replace('/', '')
+    sitename = str1.replace(':', '.')
+
+    sp.run(f'wget {parsed_name} -O static/sitecons/{sitename}.ico', shell=True)
+
+    return f'{sitename}.ico'

+ 14 - 3
app/logic.py

@@ -9,6 +9,7 @@ from . import db
 import string, secrets
 import string, secrets
 import csv
 import csv
 import subprocess as sp
 import subprocess as sp
+from .iconfetch import *
 
 
 logic = Blueprint('logic', __name__)
 logic = Blueprint('logic', __name__)
 
 
@@ -41,6 +42,7 @@ def home():
     # owned bookmarks
     # owned bookmarks
     bmarks = Bookmark.query.filter_by(owner=current_user.id).all()
     bmarks = Bookmark.query.filter_by(owner=current_user.id).all()
 
 
+    # populate bookmark dict for show
     bb_dic = dict() # dict of lists
     bb_dic = dict() # dict of lists
     for bg in bgroups:
     for bg in bgroups:
         inter_list = list()
         inter_list = list()
@@ -49,7 +51,7 @@ def home():
                 inter_list.append(bm)
                 inter_list.append(bm)
         bb_dic[bg.group] = inter_list
         bb_dic[bg.group] = inter_list
 
 
-    # request method
+    # POST request method
     if request.method == "POST":
     if request.method == "POST":
         username = request.form.get('username')
         username = request.form.get('username')
         passwd = request.form.get('password')
         passwd = request.form.get('password')
@@ -109,11 +111,11 @@ def shared():
 
 
     return render_template('shared.html', user=current_user, bdic=bb_dic)
     return render_template('shared.html', user=current_user, bdic=bb_dic)
 
 
-# Account management
 @logic.route('/create', methods=['GET', 'POST'])
 @logic.route('/create', methods=['GET', 'POST'])
 @login_required
 @login_required
 def create():
 def create():
 
 
+    # load bookmark groups
     bgroups = Groups.query.filter_by(owner=current_user.id).all()
     bgroups = Groups.query.filter_by(owner=current_user.id).all()
 
 
     if request.method == 'POST':
     if request.method == 'POST':
@@ -124,13 +126,21 @@ def create():
         wstatus = request.form.get('share_public_private')
         wstatus = request.form.get('share_public_private')
 
 
         if wlink and wname and wstatus:
         if wlink and wname and wstatus:
+
+            # fetch/check icon
+            sitecon = check_icon(wlink)
+            if not sitecon:
+                sitecon = fetch_icon(wlink)
+
+            # add bookmark to database
             if wgroup == 'cng-555' and ngroup != '':
             if wgroup == 'cng-555' and ngroup != '':
                 wgroup = ngroup
                 wgroup = ngroup
             new_link = Bookmark(owner=current_user.id,
             new_link = Bookmark(owner=current_user.id,
                                 status=wstatus,
                                 status=wstatus,
                                 name=wname,
                                 name=wname,
                                 group=wgroup,
                                 group=wgroup,
-                                link=wlink
+                                link=wlink,
+                                icon=sitecon
                                 )
                                 )
             group_check = Groups.query.filter_by(group=wgroup).first()
             group_check = Groups.query.filter_by(group=wgroup).first()
             if not group_check:
             if not group_check:
@@ -142,6 +152,7 @@ def create():
 
 
     return render_template('create_bookmark.html', user=current_user, bgroups=bgroups)
     return render_template('create_bookmark.html', user=current_user, bgroups=bgroups)
 
 
+# Account management
 @logic.route('/login', methods=['GET', 'POST'])
 @logic.route('/login', methods=['GET', 'POST'])
 def login():
 def login():
     if request.method == 'POST':
     if request.method == 'POST':

+ 1 - 0
app/models.py

@@ -18,6 +18,7 @@ class Bookmark(db.Model, UserMixin):
    name = db.Column(db.String(150))
    name = db.Column(db.String(150))
    group = db.Column(db.String(150))
    group = db.Column(db.String(150))
    link = db.Column(db.String(150))
    link = db.Column(db.String(150))
+   icon = db.Column(db.String(150))
 
 
 
 
 class Groups(db.Model, UserMixin):
 class Groups(db.Model, UserMixin):

BIN
app/static/sitecons/github.com.ico


BIN
app/static/sitecons/https.superuser.com.ico


BIN
app/static/sitecons/https.www.superuser.com.ico


BIN
app/static/sitecons/https.www.youtube.com.ico


BIN
app/static/sitecons/https.youtube.com.ico


BIN
app/static/sitecons/superuser.com.ico


BIN
app/static/uploads/pficihNwSgqBwnmy.jpeg


+ 1 - 1
app/templates/home.html

@@ -205,7 +205,7 @@ body{
                                     <div class="row">
                                     <div class="row">
                                         <div class="col.md-4">
                                         <div class="col.md-4">
                                             <ul class="list">
                                             <ul class="list">
-                                            <li>(ICON) {{ bm.name }}</li>
+                                                <li><img src="static/sitecons/{{bm.icon}}" height=25 width=25> {{ bm.name }}</li>
                                         </div>
                                         </div>
                                         <div class="col.md-4" style="position:absolute; right:17px;">
                                         <div class="col.md-4" style="position:absolute; right:17px;">
                                             <li class="href_link_button">
                                             <li class="href_link_button">

+ 2 - 2
app/templates/shared.html

@@ -82,7 +82,7 @@ ul {
                         <div class="row">
                         <div class="row">
                             <div class="col.md-4">
                             <div class="col.md-4">
                                 <ul class="list">
                                 <ul class="list">
-                                <li>(ICON) {{ bm.name }}</li>
+                                    <li><img src="static/sitecons/{{bm.icon}}" height=25 width=25> {{ bm.name }}</li>
                             </div>
                             </div>
                             <div class="col.md-4" style="position:absolute; right:17px;">
                             <div class="col.md-4" style="position:absolute; right:17px;">
                                 <li class="href_link_button">
                                 <li class="href_link_button">
@@ -101,4 +101,4 @@ ul {
             </div>
             </div>
             {% endfor %}
             {% endfor %}
         </div>
         </div>
-{% endblock %}
+{% endblock %}