fix: Add escape char for curl rendering and format python files
This commit is contained in:
@@ -3,7 +3,7 @@ import os
|
||||
from cloudflare import Cloudflare
|
||||
from tools import json_response
|
||||
|
||||
app = Blueprint('acme', __name__)
|
||||
app = Blueprint("acme", __name__)
|
||||
|
||||
|
||||
@app.route("/hnsdoh-acme", methods=["POST"])
|
||||
@@ -23,7 +23,9 @@ def post():
|
||||
zone = cf.zones.list(name="hnsdoh.com").to_dict()
|
||||
zone_id = zone["result"][0]["id"] # type: ignore
|
||||
existing_records = cf.dns.records.list(
|
||||
zone_id=zone_id, type="TXT", name="_acme-challenge.hnsdoh.com" # type: ignore
|
||||
zone_id=zone_id,
|
||||
type="TXT",
|
||||
name="_acme-challenge.hnsdoh.com", # type: ignore
|
||||
).to_dict()
|
||||
record_id = existing_records["result"][0]["id"] # type: ignore
|
||||
cf.dns.records.delete(dns_record_id=record_id, zone_id=zone_id)
|
||||
|
||||
@@ -18,7 +18,7 @@ HTTP_NOT_FOUND = 404
|
||||
HTTP_UNSUPPORTED_MEDIA = 415
|
||||
HTTP_SERVER_ERROR = 500
|
||||
|
||||
app = Blueprint('api', __name__, url_prefix='/api/v1')
|
||||
app = Blueprint("api", __name__, url_prefix="/api/v1")
|
||||
# Register solana blueprint
|
||||
app.register_blueprint(sol.app)
|
||||
|
||||
@@ -27,34 +27,38 @@ app.register_blueprint(sol.app)
|
||||
@app.route("/help")
|
||||
def help():
|
||||
"""Provide API documentation and help."""
|
||||
return jsonify({
|
||||
"message": "Welcome to Nathan.Woodburn/ API! This is a personal website. For more information, visit https://nathan.woodburn.au",
|
||||
"endpoints": {
|
||||
"/time": "Get the current time",
|
||||
"/timezone": "Get the current timezone",
|
||||
"/message": "Get the message from the config",
|
||||
"/project": "Get the current project from git",
|
||||
"/version": "Get the current version of the website",
|
||||
"/page_date?url=URL&verbose=BOOL": "Get the last modified date of a webpage (verbose is optional, default false)",
|
||||
"/tools": "Get a list of tools used by Nathan Woodburn",
|
||||
"/playing": "Get the currently playing Spotify track",
|
||||
"/status": "Just check if the site is up",
|
||||
"/ping": "Just check if the site is up",
|
||||
"/ip": "Get your IP address",
|
||||
"/headers": "Get your request headers",
|
||||
"/help": "Get this help message"
|
||||
},
|
||||
"base_url": "/api/v1",
|
||||
"version": getGitCommit(),
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK
|
||||
})
|
||||
return jsonify(
|
||||
{
|
||||
"message": "Welcome to Nathan.Woodburn/ API! This is a personal website. For more information, visit https://nathan.woodburn.au",
|
||||
"endpoints": {
|
||||
"/time": "Get the current time",
|
||||
"/timezone": "Get the current timezone",
|
||||
"/message": "Get the message from the config",
|
||||
"/project": "Get the current project from git",
|
||||
"/version": "Get the current version of the website",
|
||||
"/page_date?url=URL&verbose=BOOL": "Get the last modified date of a webpage (verbose is optional, default false)",
|
||||
"/tools": "Get a list of tools used by Nathan Woodburn",
|
||||
"/playing": "Get the currently playing Spotify track",
|
||||
"/status": "Just check if the site is up",
|
||||
"/ping": "Just check if the site is up",
|
||||
"/ip": "Get your IP address",
|
||||
"/headers": "Get your request headers",
|
||||
"/help": "Get this help message",
|
||||
},
|
||||
"base_url": "/api/v1",
|
||||
"version": getGitCommit(),
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@app.route("/status")
|
||||
@app.route("/ping")
|
||||
def status():
|
||||
return json_response(request, "200 OK", HTTP_OK)
|
||||
|
||||
|
||||
@app.route("/version")
|
||||
def version():
|
||||
"""Get the current version of the website."""
|
||||
@@ -68,45 +72,44 @@ def time():
|
||||
timezone_offset = datetime.timedelta(hours=nc_config["time-zone"])
|
||||
timezone = datetime.timezone(offset=timezone_offset)
|
||||
current_time = datetime.datetime.now(tz=timezone)
|
||||
return jsonify({
|
||||
"timestring": current_time.strftime("%A, %B %d, %Y %I:%M %p"),
|
||||
"timestamp": current_time.timestamp(),
|
||||
"timezone": nc_config["time-zone"],
|
||||
"timeISO": current_time.isoformat(),
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK
|
||||
})
|
||||
return jsonify(
|
||||
{
|
||||
"timestring": current_time.strftime("%A, %B %d, %Y %I:%M %p"),
|
||||
"timestamp": current_time.timestamp(),
|
||||
"timezone": nc_config["time-zone"],
|
||||
"timeISO": current_time.isoformat(),
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@app.route("/timezone")
|
||||
def timezone():
|
||||
"""Get the current timezone setting."""
|
||||
nc_config = get_nc_config()
|
||||
return jsonify({
|
||||
"timezone": nc_config["time-zone"],
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK
|
||||
})
|
||||
return jsonify(
|
||||
{
|
||||
"timezone": nc_config["time-zone"],
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@app.route("/message")
|
||||
def message():
|
||||
"""Get the message from the configuration."""
|
||||
nc_config = get_nc_config()
|
||||
return jsonify({
|
||||
"message": nc_config["message"],
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK
|
||||
})
|
||||
return jsonify(
|
||||
{"message": nc_config["message"], "ip": getClientIP(request), "status": HTTP_OK}
|
||||
)
|
||||
|
||||
|
||||
@app.route("/ip")
|
||||
def ip():
|
||||
"""Get the client's IP address."""
|
||||
return jsonify({
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK
|
||||
})
|
||||
return jsonify({"ip": getClientIP(request), "status": HTTP_OK})
|
||||
|
||||
|
||||
@app.route("/email", methods=["POST"])
|
||||
@@ -114,7 +117,9 @@ def email_post():
|
||||
"""Send an email via the API (requires API key)."""
|
||||
# Verify json
|
||||
if not request.is_json:
|
||||
return json_response(request, "415 Unsupported Media Type", HTTP_UNSUPPORTED_MEDIA)
|
||||
return json_response(
|
||||
request, "415 Unsupported Media Type", HTTP_UNSUPPORTED_MEDIA
|
||||
)
|
||||
|
||||
# Check if api key sent
|
||||
data = request.json
|
||||
@@ -137,7 +142,7 @@ def project():
|
||||
git = get_git_latest_activity()
|
||||
repo_name = git["repo"]["name"].lower()
|
||||
repo_description = git["repo"]["description"]
|
||||
|
||||
|
||||
gitinfo = {
|
||||
"name": repo_name,
|
||||
"description": repo_description,
|
||||
@@ -145,13 +150,16 @@ def project():
|
||||
"website": git["repo"].get("website"),
|
||||
}
|
||||
|
||||
return jsonify({
|
||||
"repo_name": repo_name,
|
||||
"repo_description": repo_description,
|
||||
"repo": gitinfo,
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK
|
||||
})
|
||||
return jsonify(
|
||||
{
|
||||
"repo_name": repo_name,
|
||||
"repo_description": repo_description,
|
||||
"repo": gitinfo,
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@app.route("/tools")
|
||||
def tools():
|
||||
@@ -161,9 +169,10 @@ def tools():
|
||||
except Exception as e:
|
||||
print(f"Error getting tools data: {e}")
|
||||
return json_response(request, "500 Internal Server Error", HTTP_SERVER_ERROR)
|
||||
|
||||
|
||||
return json_response(request, {"tools": tools}, HTTP_OK)
|
||||
|
||||
|
||||
@app.route("/playing")
|
||||
def playing():
|
||||
"""Get the currently playing Spotify track."""
|
||||
@@ -185,16 +194,12 @@ def headers():
|
||||
if key.startswith("X-"):
|
||||
# Remove from headers
|
||||
toremove.append(key)
|
||||
|
||||
|
||||
for key in toremove:
|
||||
headers.pop(key)
|
||||
|
||||
return jsonify({
|
||||
"headers": headers,
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK
|
||||
})
|
||||
return jsonify({"headers": headers, "ip": getClientIP(request), "status": HTTP_OK})
|
||||
|
||||
|
||||
@app.route("/page_date")
|
||||
def page_date():
|
||||
@@ -211,33 +216,33 @@ def page_date():
|
||||
r = requests.get(url, timeout=5)
|
||||
r.raise_for_status()
|
||||
except requests.exceptions.RequestException as e:
|
||||
return json_response(request, f"400 Bad Request 'url' unreachable: {e}", HTTP_BAD_REQUEST)
|
||||
return json_response(
|
||||
request, f"400 Bad Request 'url' unreachable: {e}", HTTP_BAD_REQUEST
|
||||
)
|
||||
|
||||
page_text = r.text
|
||||
|
||||
# Remove ordinal suffixes globally
|
||||
page_text = re.sub(r'(\d+)(st|nd|rd|th)', r'\1', page_text, flags=re.IGNORECASE)
|
||||
page_text = re.sub(r"(\d+)(st|nd|rd|th)", r"\1", page_text, flags=re.IGNORECASE)
|
||||
# Remove HTML comments
|
||||
page_text = re.sub(r'<!--.*?-->', '', page_text, flags=re.DOTALL)
|
||||
page_text = re.sub(r"<!--.*?-->", "", page_text, flags=re.DOTALL)
|
||||
|
||||
date_patterns = [
|
||||
r'(\d{4})[/-](\d{1,2})[/-](\d{1,2})', # YYYY-MM-DD
|
||||
r'(\d{1,2})[/-](\d{1,2})[/-](\d{4})', # DD-MM-YYYY
|
||||
r'(?:Last updated:|Updated:|Updated last:)?\s*(\d{1,2})\s+([A-Za-z]{3,9})[, ]?\s*(\d{4})', # DD Month YYYY
|
||||
r'(?:\b\w+\b\s+){0,3}([A-Za-z]{3,9})\s+(\d{1,2}),?\s*(\d{4})', # Month DD, YYYY with optional words
|
||||
r'\b(\d{4})(\d{2})(\d{2})\b', # YYYYMMDD
|
||||
r'(?:Last updated:|Updated:|Last update)?\s*([A-Za-z]{3,9})\s+(\d{4})', # Month YYYY only
|
||||
r"(\d{4})[/-](\d{1,2})[/-](\d{1,2})", # YYYY-MM-DD
|
||||
r"(\d{1,2})[/-](\d{1,2})[/-](\d{4})", # DD-MM-YYYY
|
||||
r"(?:Last updated:|Updated:|Updated last:)?\s*(\d{1,2})\s+([A-Za-z]{3,9})[, ]?\s*(\d{4})", # DD Month YYYY
|
||||
r"(?:\b\w+\b\s+){0,3}([A-Za-z]{3,9})\s+(\d{1,2}),?\s*(\d{4})", # Month DD, YYYY with optional words
|
||||
r"\b(\d{4})(\d{2})(\d{2})\b", # YYYYMMDD
|
||||
r"(?:Last updated:|Updated:|Last update)?\s*([A-Za-z]{3,9})\s+(\d{4})", # Month YYYY only
|
||||
]
|
||||
|
||||
|
||||
|
||||
# Structured data patterns
|
||||
json_date_patterns = {
|
||||
r'"datePublished"\s*:\s*"([^"]+)"': "published",
|
||||
r'"dateModified"\s*:\s*"([^"]+)"': "modified",
|
||||
r'<meta\s+(?:[^>]*?)property\s*=\s*"article:published_time"\s+content\s*=\s*"([^"]+)"': "published",
|
||||
r'<meta\s+(?:[^>]*?)property\s*=\s*"article:modified_time"\s+content\s*=\s*"([^"]+)"': "modified",
|
||||
r'<time\s+datetime\s*=\s*"([^"]+)"': "published"
|
||||
r'<time\s+datetime\s*=\s*"([^"]+)"': "published",
|
||||
}
|
||||
|
||||
found_dates = []
|
||||
@@ -255,7 +260,7 @@ def page_date():
|
||||
for match in re.findall(pattern, page_text):
|
||||
try:
|
||||
dt = date_parser.isoparse(match)
|
||||
formatted_date = dt.strftime('%Y-%m-%d')
|
||||
formatted_date = dt.strftime("%Y-%m-%d")
|
||||
found_dates.append([[formatted_date], -1, date_type])
|
||||
except (ValueError, TypeError):
|
||||
continue
|
||||
@@ -264,7 +269,9 @@ def page_date():
|
||||
return json_response(request, "Date not found on page", HTTP_BAD_REQUEST)
|
||||
|
||||
today = datetime.date.today()
|
||||
tolerance_date = today + datetime.timedelta(days=1) # Allow for slight future dates (e.g., time zones)
|
||||
tolerance_date = today + datetime.timedelta(
|
||||
days=1
|
||||
) # Allow for slight future dates (e.g., time zones)
|
||||
# When processing dates
|
||||
processed_dates = []
|
||||
for date_groups, pattern_format, date_type in found_dates:
|
||||
@@ -285,18 +292,32 @@ def page_date():
|
||||
date_obj = {"date": dt.strftime("%Y-%m-%d"), "type": date_type}
|
||||
if verbose:
|
||||
if pattern_format == -1:
|
||||
date_obj.update({"source": "metadata", "pattern_used": pattern_format, "raw": date_groups[0]})
|
||||
date_obj.update(
|
||||
{
|
||||
"source": "metadata",
|
||||
"pattern_used": pattern_format,
|
||||
"raw": date_groups[0],
|
||||
}
|
||||
)
|
||||
else:
|
||||
date_obj.update({"source": "content", "pattern_used": pattern_format, "raw": " ".join(date_groups)})
|
||||
date_obj.update(
|
||||
{
|
||||
"source": "content",
|
||||
"pattern_used": pattern_format,
|
||||
"raw": " ".join(date_groups),
|
||||
}
|
||||
)
|
||||
processed_dates.append(date_obj)
|
||||
|
||||
if not processed_dates:
|
||||
if verbose:
|
||||
return jsonify({
|
||||
"message": "No valid dates found on page",
|
||||
"found_dates": found_dates,
|
||||
"processed_dates": processed_dates
|
||||
}), HTTP_BAD_REQUEST
|
||||
return jsonify(
|
||||
{
|
||||
"message": "No valid dates found on page",
|
||||
"found_dates": found_dates,
|
||||
"processed_dates": processed_dates,
|
||||
}
|
||||
), HTTP_BAD_REQUEST
|
||||
return json_response(request, "No valid dates found on page", HTTP_BAD_REQUEST)
|
||||
# Sort dates and return latest
|
||||
processed_dates.sort(key=lambda x: x["date"])
|
||||
|
||||
@@ -6,7 +6,7 @@ import re
|
||||
from functools import lru_cache
|
||||
from tools import isCLI, getClientIP, getHandshakeScript
|
||||
|
||||
app = Blueprint('blog', __name__, url_prefix='/blog')
|
||||
app = Blueprint("blog", __name__, url_prefix="/blog")
|
||||
|
||||
|
||||
@lru_cache(maxsize=32)
|
||||
@@ -14,11 +14,13 @@ def list_page_files():
|
||||
blog_pages = os.listdir("data/blog")
|
||||
# Sort pages by modified time, newest first
|
||||
blog_pages.sort(
|
||||
key=lambda x: os.path.getmtime(os.path.join("data/blog", x)), reverse=True)
|
||||
key=lambda x: os.path.getmtime(os.path.join("data/blog", x)), reverse=True
|
||||
)
|
||||
|
||||
# Remove .md extension
|
||||
blog_pages = [page.removesuffix(".md")
|
||||
for page in blog_pages if page.endswith(".md")]
|
||||
blog_pages = [
|
||||
page.removesuffix(".md") for page in blog_pages if page.endswith(".md")
|
||||
]
|
||||
|
||||
return blog_pages
|
||||
|
||||
@@ -28,7 +30,7 @@ def get_blog_content(date):
|
||||
"""Get and cache blog content."""
|
||||
if not os.path.exists(f"data/blog/{date}.md"):
|
||||
return None
|
||||
|
||||
|
||||
with open(f"data/blog/{date}.md", "r") as f:
|
||||
return f.read()
|
||||
|
||||
@@ -37,7 +39,8 @@ def get_blog_content(date):
|
||||
def render_markdown_to_html(content):
|
||||
"""Convert markdown to HTML with caching."""
|
||||
html = markdown.markdown(
|
||||
content, extensions=['sane_lists', 'codehilite', 'fenced_code'])
|
||||
content, extensions=["sane_lists", "codehilite", "fenced_code"]
|
||||
)
|
||||
# Add target="_blank" to all links
|
||||
html = html.replace('<a href="', '<a target="_blank" href="')
|
||||
html = html.replace("<h4", "<h4 style='margin-bottom:0px;'")
|
||||
@@ -65,18 +68,18 @@ def render_page(date, handshake_scripts=None):
|
||||
|
||||
|
||||
def fix_numbered_lists(html):
|
||||
soup = BeautifulSoup(html, 'html.parser')
|
||||
soup = BeautifulSoup(html, "html.parser")
|
||||
|
||||
# Find the <p> tag containing numbered steps
|
||||
paragraphs = soup.find_all('p')
|
||||
paragraphs = soup.find_all("p")
|
||||
for p in paragraphs:
|
||||
content = p.decode_contents() # type: ignore
|
||||
|
||||
# Check for likely numbered step structure
|
||||
if re.search(r'1\.\s', content):
|
||||
if re.search(r"1\.\s", content):
|
||||
# Split into pre-list and numbered steps
|
||||
# Match: <br>, optional whitespace, then a number and dot
|
||||
parts = re.split(r'(?:<br\s*/?>)?\s*(\d+)\.\s', content)
|
||||
parts = re.split(r"(?:<br\s*/?>)?\s*(\d+)\.\s", content)
|
||||
|
||||
# Result: [pre-text, '1', step1, '2', step2, ..., '10', step10]
|
||||
pre_text = parts[0].strip()
|
||||
@@ -85,10 +88,9 @@ def fix_numbered_lists(html):
|
||||
# Assemble the ordered list
|
||||
ol_items = []
|
||||
for i in range(0, len(steps), 2):
|
||||
if i+1 < len(steps):
|
||||
step_html = steps[i+1].strip()
|
||||
ol_items.append(
|
||||
f"<li style='list-style: auto;'>{step_html}</li>")
|
||||
if i + 1 < len(steps):
|
||||
step_html = steps[i + 1].strip()
|
||||
ol_items.append(f"<li style='list-style: auto;'>{step_html}</li>")
|
||||
|
||||
# Build the final list HTML
|
||||
ol_html = "<ol>\n" + "\n".join(ol_items) + "\n</ol>"
|
||||
@@ -97,7 +99,7 @@ def fix_numbered_lists(html):
|
||||
new_html = f"{pre_text}<br />\n{ol_html}" if pre_text else ol_html
|
||||
|
||||
# Replace old <p> with parsed version
|
||||
new_fragment = BeautifulSoup(new_html, 'html.parser')
|
||||
new_fragment = BeautifulSoup(new_html, "html.parser")
|
||||
p.replace_with(new_fragment)
|
||||
break # Only process the first matching <p>
|
||||
|
||||
@@ -134,16 +136,23 @@ def index():
|
||||
blog_pages = list_page_files()
|
||||
# Create a html list of pages
|
||||
blog_pages = [
|
||||
{"name": page.replace("_", " "), "url": f"/blog/{page}", "download": f"/blog/{page}.md"} for page in blog_pages
|
||||
{
|
||||
"name": page.replace("_", " "),
|
||||
"url": f"/blog/{page}",
|
||||
"download": f"/blog/{page}.md",
|
||||
}
|
||||
for page in blog_pages
|
||||
]
|
||||
|
||||
# Render the template
|
||||
return jsonify({
|
||||
"status": 200,
|
||||
"message": "Check out my various blog postsa",
|
||||
"ip": getClientIP(request),
|
||||
"blogs": blog_pages
|
||||
}), 200
|
||||
return jsonify(
|
||||
{
|
||||
"status": 200,
|
||||
"message": "Check out my various blog postsa",
|
||||
"ip": getClientIP(request),
|
||||
"blogs": blog_pages,
|
||||
}
|
||||
), 200
|
||||
|
||||
|
||||
@app.route("/<path:path>")
|
||||
@@ -158,14 +167,16 @@ def path(path):
|
||||
|
||||
# Get the title from the file name
|
||||
title = path.replace("_", " ")
|
||||
return jsonify({
|
||||
"status": 200,
|
||||
"message": f"Blog post: {title}",
|
||||
"ip": getClientIP(request),
|
||||
"title": title,
|
||||
"content": content,
|
||||
"download": f"/blog/{path}.md"
|
||||
}), 200
|
||||
return jsonify(
|
||||
{
|
||||
"status": 200,
|
||||
"message": f"Blog post: {title}",
|
||||
"ip": getClientIP(request),
|
||||
"title": title,
|
||||
"content": content,
|
||||
"download": f"/blog/{path}.md",
|
||||
}
|
||||
), 200
|
||||
|
||||
|
||||
@app.route("/<path:path>.md")
|
||||
@@ -175,4 +186,4 @@ def path_md(path):
|
||||
return render_template("404.html"), 404
|
||||
|
||||
# Return the raw markdown file
|
||||
return content, 200, {'Content-Type': 'text/plain; charset=utf-8'}
|
||||
return content, 200, {"Content-Type": "text/plain; charset=utf-8"}
|
||||
|
||||
@@ -8,7 +8,7 @@ from bs4 import BeautifulSoup
|
||||
import re
|
||||
|
||||
# Create blueprint
|
||||
app = Blueprint('now', __name__, url_prefix='/now')
|
||||
app = Blueprint("now", __name__, url_prefix="/now")
|
||||
|
||||
|
||||
@lru_cache(maxsize=16)
|
||||
@@ -55,7 +55,10 @@ def render(date, handshake_scripts=None):
|
||||
|
||||
date_formatted = datetime.datetime.strptime(date, "%y_%m_%d")
|
||||
date_formatted = date_formatted.strftime("%A, %B %d, %Y")
|
||||
return render_template(f"now/{date}.html", DATE=date_formatted, handshake_scripts=handshake_scripts)
|
||||
return render_template(
|
||||
f"now/{date}.html", DATE=date_formatted, handshake_scripts=handshake_scripts
|
||||
)
|
||||
|
||||
|
||||
def render_curl(date=None):
|
||||
# If the date is not available, render the latest page
|
||||
@@ -71,12 +74,12 @@ def render_curl(date=None):
|
||||
# Format the date nicely
|
||||
date_formatted = datetime.datetime.strptime(date, "%y_%m_%d")
|
||||
date_formatted = date_formatted.strftime("%A, %B %d, %Y")
|
||||
|
||||
|
||||
# Load HTML
|
||||
with open(f"templates/now/{date}.html", "r", encoding="utf-8") as f:
|
||||
raw_html = f.read().replace("{{ date }}", date_formatted)
|
||||
soup = BeautifulSoup(raw_html, 'html.parser')
|
||||
|
||||
soup = BeautifulSoup(raw_html, "html.parser")
|
||||
|
||||
posts = []
|
||||
|
||||
# Find divs matching your pattern
|
||||
@@ -86,12 +89,12 @@ def render_curl(date=None):
|
||||
|
||||
for div in divs:
|
||||
# header could be h1/h2/h3 inside the div
|
||||
header_tag = div.find(["h1", "h2", "h3"]) # type: ignore
|
||||
header_tag = div.find(["h1", "h2", "h3"]) # type: ignore
|
||||
# content is usually one or more <p> tags inside the div
|
||||
p_tags = div.find_all("p") # type: ignore
|
||||
p_tags = div.find_all("p") # type: ignore
|
||||
|
||||
if header_tag and p_tags:
|
||||
header_text = header_tag.get_text(strip=True) # type: ignore
|
||||
header_text = header_tag.get_text(strip=True) # type: ignore
|
||||
content_lines = []
|
||||
|
||||
for p in p_tags:
|
||||
@@ -99,15 +102,15 @@ def render_curl(date=None):
|
||||
text = p.get_text(strip=False)
|
||||
|
||||
# Extract any <a> links in the paragraph
|
||||
links = [a.get("href") for a in p.find_all("a", href=True)] # type: ignore
|
||||
links = [a.get("href") for a in p.find_all("a", href=True)] # type: ignore
|
||||
# Set max width for text wrapping
|
||||
|
||||
|
||||
# Wrap text manually
|
||||
wrapped_lines = []
|
||||
for line in text.splitlines():
|
||||
while len(line) > MAX_WIDTH:
|
||||
# Find last space within max_width
|
||||
split_at = line.rfind(' ', 0, MAX_WIDTH)
|
||||
split_at = line.rfind(" ", 0, MAX_WIDTH)
|
||||
if split_at == -1:
|
||||
split_at = MAX_WIDTH
|
||||
wrapped_lines.append(line[:split_at].rstrip())
|
||||
@@ -116,7 +119,7 @@ def render_curl(date=None):
|
||||
text = "\n".join(wrapped_lines)
|
||||
|
||||
if links:
|
||||
text += "\nLinks: " + ", ".join(links) # type: ignore
|
||||
text += "\nLinks: " + ", ".join(links) # type: ignore
|
||||
|
||||
content_lines.append(text)
|
||||
|
||||
@@ -128,8 +131,9 @@ def render_curl(date=None):
|
||||
for post in posts:
|
||||
response += f"[1m{post['header']}[0m\n\n{post['content']}\n\n"
|
||||
|
||||
return render_template("now.ascii", date=date_formatted, content=response, header=get_header())
|
||||
|
||||
return render_template(
|
||||
"now.ascii", date=date_formatted, content=response, header=get_header()
|
||||
)
|
||||
|
||||
|
||||
@app.route("/", strict_slashes=False)
|
||||
@@ -157,8 +161,9 @@ def old():
|
||||
date_fmt = datetime.datetime.strptime(date, "%y_%m_%d")
|
||||
date_fmt = date_fmt.strftime("%A, %B %d, %Y")
|
||||
response += f"{date_fmt} - /now/{link}\n"
|
||||
return render_template("now.ascii", date="Old Now Pages", content=response, header=get_header())
|
||||
|
||||
return render_template(
|
||||
"now.ascii", date="Old Now Pages", content=response, header=get_header()
|
||||
)
|
||||
|
||||
html = '<ul class="list-group">'
|
||||
html += f'<a style="text-decoration:none;" href="/now"><li class="list-group-item" style="background-color:#000000;color:#ffffff;">{get_latest_date(True)}</li></a>'
|
||||
@@ -171,7 +176,9 @@ def old():
|
||||
|
||||
html += "</ul>"
|
||||
return render_template(
|
||||
"now/old.html", handshake_scripts=getHandshakeScript(request.host), now_pages=html
|
||||
"now/old.html",
|
||||
handshake_scripts=getHandshakeScript(request.host),
|
||||
now_pages=html,
|
||||
)
|
||||
|
||||
|
||||
@@ -189,7 +196,7 @@ def rss():
|
||||
link = page.strip(".html")
|
||||
date = datetime.datetime.strptime(link, "%y_%m_%d")
|
||||
date = date.strftime("%A, %B %d, %Y")
|
||||
rss += f'<item><title>What\'s Happening {date}</title><link>{host}/now/{link}</link><description>Latest updates for {date}</description><guid>{host}/now/{link}</guid></item>'
|
||||
rss += f"<item><title>What's Happening {date}</title><link>{host}/now/{link}</link><description>Latest updates for {date}</description><guid>{host}/now/{link}</guid></item>"
|
||||
rss += "</channel></rss>"
|
||||
return make_response(rss, 200, {"Content-Type": "application/rss+xml"})
|
||||
|
||||
@@ -200,6 +207,17 @@ def json():
|
||||
host = "https://" + request.host
|
||||
if ":" in request.host:
|
||||
host = "http://" + request.host
|
||||
now_pages = [{"url": host+"/now/"+page.strip(".html"), "date": datetime.datetime.strptime(page.strip(".html"), "%y_%m_%d").strftime(
|
||||
"%A, %B %d, %Y"), "title": "What's Happening "+datetime.datetime.strptime(page.strip(".html"), "%y_%m_%d").strftime("%A, %B %d, %Y")} for page in now_pages]
|
||||
now_pages = [
|
||||
{
|
||||
"url": host + "/now/" + page.strip(".html"),
|
||||
"date": datetime.datetime.strptime(
|
||||
page.strip(".html"), "%y_%m_%d"
|
||||
).strftime("%A, %B %d, %Y"),
|
||||
"title": "What's Happening "
|
||||
+ datetime.datetime.strptime(page.strip(".html"), "%y_%m_%d").strftime(
|
||||
"%A, %B %d, %Y"
|
||||
),
|
||||
}
|
||||
for page in now_pages
|
||||
]
|
||||
return jsonify(now_pages)
|
||||
|
||||
@@ -2,7 +2,8 @@ from flask import Blueprint, make_response, request
|
||||
from tools import error_response
|
||||
import requests
|
||||
|
||||
app = Blueprint('podcast', __name__)
|
||||
app = Blueprint("podcast", __name__)
|
||||
|
||||
|
||||
@app.route("/ID1")
|
||||
def index():
|
||||
|
||||
@@ -9,12 +9,12 @@ import binascii
|
||||
import base64
|
||||
import os
|
||||
|
||||
app = Blueprint('sol', __name__)
|
||||
app = Blueprint("sol", __name__)
|
||||
|
||||
SOLANA_HEADERS = {
|
||||
"Content-Type": "application/json",
|
||||
"X-Action-Version": "2.4.2",
|
||||
"X-Blockchain-Ids": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"
|
||||
"X-Blockchain-Ids": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
|
||||
}
|
||||
|
||||
SOLANA_ADDRESS = None
|
||||
@@ -23,15 +23,19 @@ if os.path.isfile(".well-known/wallets/SOL"):
|
||||
address = file.read()
|
||||
SOLANA_ADDRESS = Pubkey.from_string(address.strip())
|
||||
|
||||
|
||||
def create_transaction(sender_address: str, amount: float) -> str:
|
||||
if SOLANA_ADDRESS is None:
|
||||
raise ValueError("SOLANA_ADDRESS is not set. Please ensure the .well-known/wallets/SOL file exists and contains a valid address.")
|
||||
raise ValueError(
|
||||
"SOLANA_ADDRESS is not set. Please ensure the .well-known/wallets/SOL file exists and contains a valid address."
|
||||
)
|
||||
# Create transaction
|
||||
sender = Pubkey.from_string(sender_address)
|
||||
transfer_ix = transfer(
|
||||
TransferParams(
|
||||
from_pubkey=sender, to_pubkey=SOLANA_ADDRESS, lamports=int(
|
||||
amount * 1000000000)
|
||||
from_pubkey=sender,
|
||||
to_pubkey=SOLANA_ADDRESS,
|
||||
lamports=int(amount * 1000000000),
|
||||
)
|
||||
)
|
||||
solana_client = Client("https://api.mainnet-beta.solana.com")
|
||||
@@ -50,10 +54,14 @@ def create_transaction(sender_address: str, amount: float) -> str:
|
||||
base64_string = base64.b64encode(raw_bytes).decode("utf-8")
|
||||
return base64_string
|
||||
|
||||
|
||||
def get_solana_address() -> str:
|
||||
if SOLANA_ADDRESS is None:
|
||||
raise ValueError("SOLANA_ADDRESS is not set. Please ensure the .well-known/wallets/SOL file exists and contains a valid address.")
|
||||
return str(SOLANA_ADDRESS)
|
||||
raise ValueError(
|
||||
"SOLANA_ADDRESS is not set. Please ensure the .well-known/wallets/SOL file exists and contains a valid address."
|
||||
)
|
||||
return str(SOLANA_ADDRESS)
|
||||
|
||||
|
||||
@app.route("/donate", methods=["GET", "OPTIONS"])
|
||||
def sol_donate():
|
||||
@@ -103,7 +111,6 @@ def sol_donate_amount(amount):
|
||||
|
||||
@app.route("/donate/<amount>", methods=["POST"])
|
||||
def sol_donate_post(amount):
|
||||
|
||||
if not request.json:
|
||||
return jsonify({"message": "Error: No JSON data provided"}), 400, SOLANA_HEADERS
|
||||
|
||||
@@ -122,4 +129,8 @@ def sol_donate_post(amount):
|
||||
return jsonify({"message": "Error: Amount too small"}), 400, SOLANA_HEADERS
|
||||
|
||||
transaction = create_transaction(sender, amount)
|
||||
return jsonify({"message": "Success", "transaction": transaction}), 200, SOLANA_HEADERS
|
||||
return (
|
||||
jsonify({"message": "Success", "transaction": transaction}),
|
||||
200,
|
||||
SOLANA_HEADERS,
|
||||
)
|
||||
|
||||
@@ -5,7 +5,7 @@ import requests
|
||||
import time
|
||||
import base64
|
||||
|
||||
app = Blueprint('spotify', __name__, url_prefix='/spotify')
|
||||
app = Blueprint("spotify", __name__, url_prefix="/spotify")
|
||||
|
||||
CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID")
|
||||
CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRET")
|
||||
@@ -21,6 +21,7 @@ ACCESS_TOKEN = None
|
||||
REFRESH_TOKEN = os.getenv("SPOTIFY_REFRESH_TOKEN")
|
||||
TOKEN_EXPIRES = 0
|
||||
|
||||
|
||||
def refresh_access_token():
|
||||
"""Refresh Spotify access token when expired."""
|
||||
global ACCESS_TOKEN, TOKEN_EXPIRES
|
||||
@@ -52,6 +53,7 @@ def refresh_access_token():
|
||||
TOKEN_EXPIRES = time.time() + token_info.get("expires_in", 3600)
|
||||
return ACCESS_TOKEN
|
||||
|
||||
|
||||
@app.route("/login")
|
||||
def login():
|
||||
auth_query = (
|
||||
@@ -60,6 +62,7 @@ def login():
|
||||
)
|
||||
return redirect(auth_query)
|
||||
|
||||
|
||||
@app.route("/callback")
|
||||
def callback():
|
||||
code = request.args.get("code")
|
||||
@@ -76,12 +79,14 @@ def callback():
|
||||
response = requests.post(SPOTIFY_TOKEN_URL, data=data)
|
||||
token_info = response.json()
|
||||
if "access_token" not in token_info:
|
||||
return json_response(request, {"error": "Failed to obtain token", "details": token_info}, 400)
|
||||
return json_response(
|
||||
request, {"error": "Failed to obtain token", "details": token_info}, 400
|
||||
)
|
||||
|
||||
access_token = token_info["access_token"]
|
||||
me = requests.get(
|
||||
"https://api.spotify.com/v1/me",
|
||||
headers={"Authorization": f"Bearer {access_token}"}
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
).json()
|
||||
|
||||
if me.get("id") != ALLOWED_SPOTIFY_USER_ID:
|
||||
@@ -93,12 +98,14 @@ def callback():
|
||||
print("Refresh Token:", REFRESH_TOKEN)
|
||||
return redirect(url_for("spotify.currently_playing"))
|
||||
|
||||
|
||||
@app.route("/", strict_slashes=False)
|
||||
@app.route("/playing")
|
||||
def currently_playing():
|
||||
"""Public endpoint showing your current track."""
|
||||
track = get_spotify_track()
|
||||
return json_response(request, {"spotify":track}, 200)
|
||||
return json_response(request, {"spotify": track}, 200)
|
||||
|
||||
|
||||
def get_spotify_track():
|
||||
"""Internal function to get current playing track without HTTP context."""
|
||||
@@ -124,7 +131,7 @@ def get_spotify_track():
|
||||
"album_name": data["item"]["album"]["name"],
|
||||
"album_art": data["item"]["album"]["images"][0]["url"],
|
||||
"is_playing": data["is_playing"],
|
||||
"progress_ms": data.get("progress_ms",0),
|
||||
"duration_ms": data["item"].get("duration_ms",1)
|
||||
"progress_ms": data.get("progress_ms", 0),
|
||||
"duration_ms": data["item"].get("duration_ms", 1),
|
||||
}
|
||||
return track
|
||||
return track
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
from flask import Blueprint, request
|
||||
from tools import json_response
|
||||
|
||||
app = Blueprint('template', __name__)
|
||||
app = Blueprint("template", __name__)
|
||||
|
||||
|
||||
@app.route("/", strict_slashes=False)
|
||||
def index():
|
||||
return json_response(request, "Success", 200)
|
||||
return json_response(request, "Success", 200)
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
from flask import Blueprint, make_response, request, jsonify, send_from_directory, redirect
|
||||
from flask import (
|
||||
Blueprint,
|
||||
make_response,
|
||||
request,
|
||||
jsonify,
|
||||
send_from_directory,
|
||||
redirect,
|
||||
)
|
||||
from tools import error_response
|
||||
import os
|
||||
|
||||
app = Blueprint('well-known', __name__, url_prefix='/.well-known')
|
||||
app = Blueprint("well-known", __name__, url_prefix="/.well-known")
|
||||
|
||||
|
||||
@app.route("/<path:path>")
|
||||
@@ -12,7 +19,7 @@ def index(path):
|
||||
|
||||
@app.route("/wallets/<path:path>")
|
||||
def wallets(path):
|
||||
if path[0] == "." and 'proof' not in path:
|
||||
if path[0] == "." and "proof" not in path:
|
||||
return send_from_directory(
|
||||
".well-known/wallets", path, mimetype="application/json"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user