Added Search | RSS by author

This commit is contained in:
BlenderDumbass 2024-11-29 16:20:48 +02:00
parent f38e020de4
commit 9ff45581d9
2 changed files with 274 additions and 8 deletions

View file

@ -345,7 +345,20 @@ def MainPage(server):
"""+config.get("tagline", "")+"""
<br>
<br>
<form action="/search">
<input name="text" class="button" placeholder="Search..." >
<button class="button" type="submit">
<img style="vertical-align: middle" src="/icon/search">
Search
</button>
</form>
<br>
"""
Tabs = tabs()
@ -543,12 +556,36 @@ def ArticlePage(server, url):
# RSS
rsslink = "https://"+config.get("domain", "")+"/rss"
authorrsslink = rsslink+"?author="+Articles.get(article, {}).get("author","")
html = html + """
<div class="dark_box">
<center>
<details>
<summary class="button">
<img style="vertical-align: middle" src="/icon/rss">
Subscribe RSS
"""+ Button("Subscribe RSS", "/rss", "rss") +"""
</summary>
<br>
<img style="vertical-align: middle" src="/icon/user">
<input class="button" style="width:50%" value='"""+authorrsslink+"""'>
"""+Button("Author", authorrsslink, "link")+"""
<br>
<img style="vertical-align: middle" src="/icon/internet">
<input class="button" style="width:50%" value='"""+rsslink+"""'>
"""+Button("Website", rsslink, "link")+"""
</details>
</center>
</div>
@ -1886,7 +1923,8 @@ def UpdatePublicationRights(server):
text = text + ".<br>"
Notify(account, "/account/"+account, text)
if granted or revoked:
Notify(account, "/account/"+account, text)
def DoComment(server):
@ -2263,27 +2301,41 @@ def RSS(server):
favicon = config.get("favicon", "")
if favicon.startswith("/"):
favicon = "https://"+config.get("domain", "example.com")+favicon
author = server.parsed.get("author", [""])[0]
title = config.get("title", "My Website")
if author:
Accounts = accounts()
account = Accounts.get(author, {})
title = account.get("title", author)+" at: "+title
rss = """
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>"""+config.get("title", "My Website")+"""</title>
<title>"""+title+"""</title>
<link>https://"""+config.get("domain", "example.com")+"""</link>
<description>"""+config.get("tagline", "")+"""</description>
<image>
<url>"""+favicon+"""</url>
<title>"""+config.get("title", "My Website")+"""</title>
<title>"""+title+"""</title>
<link>https://"""+config.get("domain", "example.com")+"""</link>
</image>
"""
for n, article in enumerate(Articles):
n = 0
for article in Articles:
if author and author != Articles[article].get("author", ""):
continue
n += 1
if n > 10:
break
@ -2310,3 +2362,214 @@ def RSS(server):
"""
send(server, rss, 200)
def Search(server):
Articles = allArticles()
Tabs = tabs()
config = Set.Load()
# Generating <head>
html = head(title = "Search",
description = "",
config = config
)
html = html + Button(config.get("title", "My Website"), "/", image=config.get("favicon", "/icon/internet"))
# The place where you can type your search
text = server.parsed.get("text",[""])[0]
try: page = int(server.parsed.get("page", ["0"])[0])
except Exception as e:
print(e)
page = 0
searchtitle = server.parsed.get("title",[""])[0]
searchauthor = server.parsed.get("author",[""])[0]
searchpost = server.parsed.get("post",[""])[0]
searchdescription = server.parsed.get("description",[""])[0]
searchcomments = server.parsed.get("comments",[""])[0]
# Supporting legacy search links
if not any([searchtitle,
searchauthor,
searchpost,
searchdescription,
searchcomments
]):
searchtitle = True
searchpost = True
searchdescription = True
searchcomments = True
checkedtitle = ""
if searchtitle: checkedtitle = " checked "
checkedauthor = ""
if searchauthor: checkedauthor = " checked "
checkedpost = ""
if searchpost: checkedpost = " checked "
checkeddescription = ""
if searchdescription: checkeddescription = " checked "
checkedcomments = ""
if searchcomments: checkedcomments = " checked "
html = html + """
<center>
<form action="/search">
<div class="toot">
<input name="text" class="button" placeholder="Search..." value='"""+text+"""'>
<button class="button" type="submit">
<img style="vertical-align: middle" src="/icon/search">
Search
</button>
<br>
<div class="button">
<input type="checkbox" """+checkedtitle+""" name="title"> Title
</div>
<div class="button">
<input type="checkbox" """+checkedauthor+""" name="author"> Author
</div>
<div class="button">
<input type="checkbox" """+checkedpost+""" name="post"> Post Text
</div>
<div class="button">
<input type="checkbox" """+checkeddescription+""" name="description"> Description
</div>
<div class="button">
<input type="checkbox" """+checkedcomments+""" name="comments"> Comments
</div>
</div>
<form>
</center>
"""
# Acutally doing the searching
counted = []
for article in Articles:
points = 0
# Title x 100 points
if searchtitle:
title = Articles[article].get("title", article)
points += title.lower().count(text.lower()) * 100
# Description x 10 points
if searchdescription:
description = Articles[article].get("description", "")
points += description.lower().count(text.lower()) * 10
# Author
if searchauthor:
author = Articles[article].get("author", "")
if author == text:
points += 2 # Perfect match with username preffered
# People might also look at the username
Accounts = accounts()
if text.lower() == Accounts.get(author, {}).get("title", "").lower():
points += 1
# Comments x 1
if searchcomments:
comments = Articles[article].get("comments", {}).get("comments", {})
for comment in comments:
commentText = comment.get("text", "")
points += commentText.lower().count(text.lower())
# Post Text x 1
if searchpost:
try:
f = Set.Folder()
url = Articles[article].get("url")
postText = open(f+"/tabs/"+url+"/text.md").read()
points += postText.lower().count(text.lower())
except Exception as e:
print(e)
if points:
counted.append([points, article])
counted = list(reversed(sorted(counted)))
Buffer = 16
From = Buffer*page
To = From+Buffer
urlNoPage = server.path
if "page=" in urlNoPage: urlNoPage = urlNoPage[:urlNoPage.rfind("&")]
if len(list(counted)) > Buffer:
if page > 0:
html = html + Button(str(page-1), urlNoPage+"&page="+str(page-1), "left")
html = html + '<div class="button">'+str(page)+'</div>'
if To < len(list(counted))-1:
html = html + Button(str(page+1), urlNoPage+"&page="+str(page+1), "right")
html = html + """
<br>
<br>
<!-- Article previews are neatly positioned into a grid here -->
<div class="flexity">
"""
rendered = 0
for n, article in enumerate(counted):
points, article = article
if n < From: continue
if n >= To: break
html = html + ArticlePreview(Articles[article], Tabs, server.cookie)
rendered += 1
html = html + '</div><br>'
if len(list(counted)) > Buffer:
if page > 0:
html = html + Button(str(page-1), urlNoPage+"&page="+str(page-1), "left")
html = html + '<div class="button">'+str(page)+'</div>'
if To < len(list(counted))-1:
html = html + Button(str(page+1), urlNoPage+"&page="+str(page+1), "right")
html = html + LoginButton(server)
send(server, html, 200)

View file

@ -186,6 +186,9 @@ class handler(BaseHTTPRequestHandler):
elif self.path[1:].startswith("read_notification"):
Render.ReadNotification(self)
elif self.path[1:].startswith("search"):
Render.Search(self)
elif self.path.startswith("/graph/"):
@ -193,7 +196,7 @@ class handler(BaseHTTPRequestHandler):
if "?" in url: url = url[:url.find("?")]
Render.Graph(self, url)
elif self.path == "/rss":
elif self.path.startswith("/rss"):
Render.RSS(self)
elif self.path.startswith("/pictures/"):