feat: encode member usernames in URLs and add docstrings
All checks were successful
Docker CI/CD / Docker Build and Push to Docker Hub (push) Successful in 2m31s
Python Package CI/CD / Publish to PyPI (push) Successful in 1m9s

Added URL encoding for member usernames to prevent errors with special characters in profile and Instructables URLs. Improved code readability by including docstrings for route functions, and refined HTML templates to conditionally display member location only if it exists. Updated the project version to 0.3.8.
This commit is contained in:
Kumi 2024-07-01 15:27:23 +02:00
parent f576555005
commit cfb81fada1
Signed by: kumi
GPG key ID: ECBCC9082395383F
4 changed files with 35 additions and 12 deletions

View file

@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "structables"
version = "0.3.7"
version = "0.3.8"
authors = [
{ name="Private.coffee Team", email="support@private.coffee" },
]

View file

@ -1,14 +1,32 @@
from flask import render_template, abort
from urllib.request import urlopen
from urllib.error import HTTPError
from urllib.parse import quote
from ..utils.helpers import proxy, member_header
from bs4 import BeautifulSoup
from urllib.request import Request
def init_member_routes(app):
"""This function initializes all the routes related to Instructables member profiles.
Args:
app (Flask): The Flask app instance.
"""
@app.route("/member/<member>/instructables/")
def route_member_instructables(member):
"""Route to display a member's Instructables.
Args:
member (str): The member's username.
Returns:
Response: The rendered HTML page.
"""
member = quote(member)
try:
data = urlopen(
f"https://www.instructables.com/member/{member}/instructables/"
@ -55,13 +73,18 @@ def init_member_routes(app):
@app.route("/member/<member>/")
def route_member(member):
headers = {
"User-Agent": "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/113.0"
}
"""Route to display a member's profile.
request = Request(
f"https://www.instructables.com/member/{member}/", headers=headers
)
Args:
member (str): The member's username.
Returns:
Response: The rendered HTML page.
"""
member = quote(member)
request = Request(f"https://www.instructables.com/member/{member}/")
try:
data = urlopen(request)
@ -103,9 +126,9 @@ def init_member_routes(app):
"div.achievements-section.main-achievements.contest-achievements div.achievement-item:not(.two-column-filler)"
):
try:
ach_title = ach.select("div.achievement-info span.achievement-title")[
0
].text
ach_title = ach.select(
"div.achievement-info span.achievement-title"
)[0].text
ach_desc = ach.select(
"div.achievement-info span.achievement-description"
)[0].text

View file

@ -4,7 +4,7 @@
<center>
<img width=150px height=150px style="display:inline-block;" src="{{ header_content.avatar }}" alt="{{ header_content.title }}">
<h1>{{ header_content.title }}</h1>
<span>{{ header_content.location }}&nbsp;&nbsp;</span>
<span>{% if header_content.location %}{{ header_content.location }}&nbsp;&nbsp;{% endif %}</span>
<span>{{ header_content.signup }}</span>
<br>
<span>{{ header_content.instructables }} Instructables&nbsp;&nbsp;</span>

View file

@ -4,7 +4,7 @@
<center>
<img width=150px height=150px style="display:inline-block;" src="{{ header_content.avatar }}" alt="{{ header_content.title }}">
<h1>{{ header_content.title }}</h1>
<span>{{ header_content.location }}&nbsp;&nbsp;</span>
<span>{% if header_content.location %}{{ header_content.location }}&nbsp;&nbsp;{% endif %}</span>
<span>{{ header_content.signup }}</span>
<br>
<span>{{ header_content.instructables }} Instructables&nbsp;&nbsp;</span>