Skip to content

Commit

Permalink
New/Patch
Browse files Browse the repository at this point in the history
Patch library var name:
type -> data_type
crawl infinity loop make server can't start
wikipedia image sometime will not available
new suggestions for search bar
  • Loading branch information
EndermanPC committed May 12, 2024
1 parent 4d659a9 commit cbdbe18
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 32 deletions.
10 changes: 8 additions & 2 deletions app/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ def result_js():
)

@app.route('/search/script.js')
def searcH_js():
def search_js():
return render_template(
'/search/script.js'
)

@app.route('/search.js')
def search_suggestions_js():
return render_template(
'/search.js'
)

@app.route('/search.css')
def searcH_css():
return render_template(
Expand All @@ -23,4 +29,4 @@ def searcH_css():
def log_txt():
return render_template(
'/log.txt'
)
)
7 changes: 4 additions & 3 deletions app/frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@

<br><br><br><form action="/search" method="GET">
<div class="search-bar-main">
<input type="search" id="search-input" value="{{ query }}" name="q" placeholder="{% trans %}Search something...{% endtrans %}">
<input type="search" id="search-input" value="{{ query }}" name="q" placeholder="{% trans %}Search something...{% endtrans %}" oninput="getSuggestions(this.value)">
<div class="icon-container">
<input type="submit" id="search-button" value="&#xF002;" style="font-family:FontAwesome;">
<input type="button" id="voice-button" value="&#xf130;" style="font-family:FontAwesome;">
</div>
</div>
</div><br>
<div id="suggestions"></div>
<p style="text-align: center; color: white;">{% trans %}Neutron is a Search Engine with: Private, Safe, Secure, Flexible!{% endtrans %}</p>
</form>

<script src="/search.js"></script>
</body>
<br><br><br>
<footer>
Expand Down
36 changes: 36 additions & 0 deletions app/frontend/search.css
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,42 @@ input[type="search"]::-webkit-search-results-decoration {
cursor: pointer;
}

#suggestions {
text-align: left;
margin: auto;
position: absolute;
left: 0;
right: 0;
border-radius: 4px;
display: none;
width: 35%;
background-color: white;
border: 2px solid #ccc;
z-index: 1;
}

#suggestionsspecial {
text-align: left;
margin: auto;
position: absolute;
left: 230px;
border-radius: 4px;
display: none;
width: 35%;
background-color: white;
border: 2px solid #ccc;
z-index: 1;
}

.suggestion {
padding: 12px;
cursor: pointer;
}

.suggestion:hover {
background-color: #ddd;
}

/* WIKIPEDIA */
.wikipedia {
display: none;
Expand Down
59 changes: 59 additions & 0 deletions app/frontend/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function getSuggestions(query) {
if (query.length > 0) {
fetch('/api/search/suggestions', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({keyword: query})
})
.then(response => response.json())
.then(data => {
let suggestions = document.getElementById('suggestions');
suggestions.innerHTML = '';
suggestions.style.display = 'block';
for (let i = 0; i < data.length; i++) {
let div = document.createElement('div');
div.innerHTML = data[i];
div.className = 'suggestion';
div.onclick = function() {
document.getElementById('search-input').value = this.innerHTML;
suggestions.style.display = 'none';
};
suggestions.appendChild(div);
}
});
} else {
document.getElementById('suggestions').style.display = 'none';
}
}

function getspecialSuggestions(query) {
if (query.length > 0) {
fetch('/api/search/suggestions', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({keyword: query})
})
.then(response => response.json())
.then(data => {
let suggestions = document.getElementById('suggestionsspecial');
suggestions.innerHTML = '';
suggestions.style.display = 'block';
for (let i = 0; i < data.length; i++) {
let div = document.createElement('div');
div.innerHTML = data[i];
div.className = 'suggestion';
div.onclick = function() {
document.getElementById('search-input').value = this.innerHTML;
suggestions.style.display = 'none';
};
suggestions.appendChild(div);
}
});
} else {
document.getElementById('suggestionsspecial').style.display = 'none';
}
}
7 changes: 4 additions & 3 deletions app/frontend/search/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
<a style="float: right; margin-top: 1%;" href="/account">{{ User }}</a>
<form action="/search" method="GET">
<div class="search-bar-result">
<input style="float: left;" type="search" id="search-input" value="{{ query }}" name="q" placeholder="{% trans %}Search something...{% endtrans %}">
<input style="float: left;" type="search" id="search-input" value="{{ query }}" name="q" placeholder="{% trans %}Search something...{% endtrans %}" oninput="getspecialSuggestions(this.value)">
<div class="icon-container">
<input type="button" id="search-button" value="&#xF002;" style="font-family:FontAwesome;">
<input type="button" id="voice-button" value="&#xf130;" style="font-family:FontAwesome;">
</div>
</div>
</form>
</div>
<br>
</div><br>
<div id="suggestionsspecial"></div>
<div class="navbar">
<div id="tagList">
<input style="border-bottom: none;" id="TextTag" type="button" class="tag" value="{% trans %}Text{% endtrans %}" data-type="Text">
Expand Down Expand Up @@ -139,6 +139,7 @@ <h3>{{ wikipedia_title }}</h3>
});
</script>
<script src="/result.js"></script>
<script src="/search.js"></script>
<script src="/search/script.js"></script>
</body>
<br><br><br>
Expand Down
2 changes: 1 addition & 1 deletion app/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def search():
ws = request.args.get('ws', '')
wl = request.args.get('wl', '')

if wt == '' or wi == '' or ws == '' or wl == '':
if wt == '' or ws == '' or wl == '':
wikipedia_info = get_wikipedia_info(keyword, language.get_locale())
else:
wikipedia_info = wt, wl, ws, wi
Expand Down
6 changes: 0 additions & 6 deletions crawl.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import hashlib
import os
import threading
import keyboard
import re
from urllib import response
import requests
Expand Down Expand Up @@ -131,8 +130,3 @@ def check_and_create_thread():
thread_id = threading.active_count()
new_thread = threading.Thread(target=ATMT, args=(thread_id,))
new_thread.start()

keyboard.add_hotkey('esc', check_and_create_thread)

while True:
pass
33 changes: 17 additions & 16 deletions library/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,50 @@ def Library_Initializer_Database(cursor):
if Library_Check_Index_Exists(cursor, 'idx_fulltext'):
cursor.execute('''CREATE FULLTEXT INDEX idx_fulltext ON information (link, title, text, description, keywords, shorttext, type)''')

def Library_Get_Data_Count(cursor, type, link):
cursor.execute('''SELECT COUNT(*) FROM information WHERE link = %s AND type = %s''', (link, type,))
def Library_Get_Data_Count(cursor, data_type, link):
cursor.execute('''SELECT COUNT(*) FROM information WHERE link = %s AND type = %s''', (link, data_type,))
return cursor.fetchone()[0]

def content_exists(conn, type, link):
def content_exists(conn, data_type, link):
cursor = conn.cursor()
count = Library_Get_Data_Count(cursor, type, link)
count = Library_Get_Data_Count(cursor, data_type, link)
return count > 0

def Library_Edit_Data(cursor, type, link, title, text, description, keywords, shorttext, added, site_id):
def Library_Edit_Data(cursor, data_type, link, title, text, description, keywords, shorttext, added, site_id):
cursor.execute('''UPDATE information
SET link=%s, title=%s, text=%s, description=%s, keywords=%s, shorttext=%s, added=%s, type=%s
WHERE site_id=%s''',
(link, title, text, description, keywords, shorttext, added, type, site_id,))
(link, title, text, description, keywords, shorttext, added, data_type, site_id,))

def Library_Get_Max_ID(cursor):
cursor.execute("SELECT MAX(site_id) FROM information")
return cursor.fetchone()[0]

def Library_Insert_Data(cursor, type, site_id, link, title, text, description, keywords, shorttext, added):
def Library_Insert_Data(cursor, data_type, site_id, link, title, text, description, keywords, shorttext, added):
cursor.execute('''INSERT INTO information
(site_id, link, title, text, description, keywords, shorttext, added, type)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)''',
(site_id, link, title, text, description, keywords, shorttext, added, type,))
(site_id, link, title, text, description, keywords, shorttext, added, data_type,))

def Library_Remove_Data(cursor, type, site_id):
cursor.execute("DELETE FROM information WHERE site_id = %s AND type = %s", (site_id, type,))
def Library_Remove_Data(cursor, data_type, site_id):
cursor.execute("DELETE FROM information WHERE site_id = %s AND type = %s", (site_id, data_type,))
cursor.execute("UPDATE information SET site_id = site_id - 1 WHERE site_id > %s", (site_id,))

def Library_Get_ID(cursor, type, link):
cursor.execute("SELECT site_id FROM information WHERE link = %s AND type = %s", (link, type,))
def Library_Get_ID(cursor, data_type, link):
cursor.execute("SELECT site_id FROM information WHERE link = %s AND type = %s", (link, data_type,))
return cursor.fetchone()[0]

def Library_Full_Text_Domain_Search(cursor, type, safe_keyword, domain, page):
def Library_Full_Text_Domain_Search(cursor, data_type, safe_keyword, domain, page):
offset = (page - 1) * 15
cursor.execute('''SELECT * FROM information
WHERE MATCH(link, title, text, description, keywords, shorttext, type) AGAINST (%s IN NATURAL LANGUAGE MODE) AND type = %s AND link = %s
LIMIT 15 OFFSET %s''', (safe_keyword, type, domain, offset))
LIMIT 15 OFFSET %s''', (safe_keyword, data_type, domain, offset))
return cursor.fetchall()

def Library_Full_Text_Search(cursor, type, safe_keyword, page):
def Library_Full_Text_Search(cursor, data_type, safe_keyword, page):
offset = (page - 1) * 15
cursor.execute('''SELECT * FROM information
WHERE MATCH(link, title, text, description, keywords, shorttext, type) AGAINST (%s IN NATURAL LANGUAGE MODE) AND type = %s
LIMIT 15 OFFSET %s''', (safe_keyword, type, offset))
LIMIT 15 OFFSET %s''', (safe_keyword, data_type, offset))
return cursor.fetchall()

8 changes: 7 additions & 1 deletion search/index.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import jsonify, request
from app import app, databases
import re
import wikipedia
from library.database import Library_Full_Text_Domain_Search, Library_Full_Text_Search

@app.route('/api/search', methods=['POST'])
Expand All @@ -26,4 +27,9 @@ def Search_Data():
if len(rows) == 0:
return jsonify([])
else:
return rows
return rows

@app.route('/api/search/suggestions', methods=['POST'])
def Search_Suggestions():
data = request.get_json()
return wikipedia.search(data['keyword']) if data['keyword'] else []

0 comments on commit cbdbe18

Please sign in to comment.