From cfb81fada145f24e2da3046422de3737c8553f59 Mon Sep 17 00:00:00 2001 From: Kumi Date: Mon, 1 Jul 2024 15:27:23 +0200 Subject: [PATCH] feat: encode member usernames in URLs and add docstrings 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. --- pyproject.toml | 2 +- src/structables/routes/member.py | 41 +++++++++++++++---- .../templates/member-instructables.html | 2 +- src/structables/templates/member.html | 2 +- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b6cc162..bdc81c3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" }, ] diff --git a/src/structables/routes/member.py b/src/structables/routes/member.py index d425782..2567af1 100644 --- a/src/structables/routes/member.py +++ b/src/structables/routes/member.py @@ -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//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//") 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 diff --git a/src/structables/templates/member-instructables.html b/src/structables/templates/member-instructables.html index 24fdced..c8c0614 100644 --- a/src/structables/templates/member-instructables.html +++ b/src/structables/templates/member-instructables.html @@ -4,7 +4,7 @@
{{ header_content.title }}

{{ header_content.title }}

- {{ header_content.location }}   + {% if header_content.location %}{{ header_content.location }}  {% endif %} {{ header_content.signup }}
{{ header_content.instructables }} Instructables   diff --git a/src/structables/templates/member.html b/src/structables/templates/member.html index d04ddac..cc097c2 100644 --- a/src/structables/templates/member.html +++ b/src/structables/templates/member.html @@ -4,7 +4,7 @@
{{ header_content.title }}

{{ header_content.title }}

- {{ header_content.location }}   + {% if header_content.location %}{{ header_content.location }}  {% endif %} {{ header_content.signup }}
{{ header_content.instructables }} Instructables