fix: Make teachers project pages return results

Signed-off-by: Skylar "The Cobra" Widulski <cobra@vern.cc>
This commit is contained in:
Skylar "The Cobra" Widulski 2023-06-08 18:04:44 -04:00
parent 30f142ce15
commit 9cf9257554
Signed by: cobra
GPG Key ID: 4FD8F812083FF6F9
1 changed files with 32 additions and 14 deletions

46
main.py
View File

@ -84,7 +84,7 @@ def member_header(header):
return [avatar, title, location, signup, instructables, views, comments, followers, bio]
def search(path, args, query='*'):
def search(path, args, query='*', teachers=False):
sort_by = "publishDate"
if args.get("sort"):
if args.get("sort").lower() == "views":
@ -102,21 +102,31 @@ def search(path, args, query='*'):
split_path = path.split('/')
category = "category%3A%3D" + split_path[1] if len(split_path) > 1 and split_path[1] != '' and split_path[1] != 'projects' and not split_path[1].startswith("?x") else ''
if not type_ == '':
category = "+%26%26+" + category
channel = "channel%3A%3D" + split_path[2] if len(split_path) > 2 and split_path[2] != '' and split_path[2] != 'projects' and not split_path[2].startswith("?x") else ''
if not type_ == '' or not category == '':
channel = "+%26%26+" + channel
page = 1
if args.get("page"):
page = args.get("page")
search_url = f"https://www.instructables.com/api_proxy/search/collections/projects/documents/search?q={query}&query_by=title%2CstepBody%2CscreenName&page={page}&sort_by={sort_by}%3Adesc&include_fields=title%2CurlString%2CcoverImageUrl%2CscreenName%2Cfavorites%2Cviews%2CprimaryClassification%2CfeatureFlag%2CprizeLevel%2CIMadeItCount"
search_url = f"https://www.instructables.com/api_proxy/search/collections/projects/documents/search?q={query}&query_by=title%2CstepBody%2CscreenName&page={page}&sort_by={sort_by}%3Adesc&include_fields=title%2CurlString%2CcoverImageUrl%2CscreenName%2Cfavorites%2Cviews%2CprimaryClassification%2CfeatureFlag%2CprizeLevel%2CIMadeItCount&filter_by={type_}{category}{channel}&per_page=60"
category = f"{'category' if not teachers else 'teachers'}%3A%3D" + split_path[1] \
if len(split_path) > 1 and \
split_path[1] != '' and \
split_path[1] != 'projects' and \
not split_path[1].startswith("?x") \
else ''
if not type_ == '':
category = "+%26%26+" + category
channel = f"{'channel' if not teachers else 'teachers'}%3A%20" + split_path[2] \
if len(split_path) > 2 and \
split_path[2] != '' and \
split_path[2] != 'projects' and \
not split_path[2].startswith("?x") \
else ''
if not type_ == '' or not category == '':
channel = "+%26%26+" + channel
search_url += f"&filter_by={type_}{category}{channel}&per_page=60"
search_data = requests.get(search_url, headers={"X-Typesense-API-Key": typesense_key})
if search_data.status_code != 200:
@ -190,7 +200,7 @@ def teachers_page(path, name):
return render_template("teachers.html", data=[name, channels, ibles, contests, path])
def project_list(path, head, args=None, realpath=None):
def project_list(path, head, args=None, realpath=None, teachers=False):
if not realpath:
realpath = path
data = requests.get("https://www.instructables.com" + path)
@ -200,7 +210,7 @@ def project_list(path, head, args=None, realpath=None):
head = f"{head + ' ' if head != '' else ''}Projects"
soup = BeautifulSoup(data.text, "html.parser")
searched = search(path, args)
searched = search(path, args, teachers=teachers)
if type(searched) == Response:
return searched
@ -392,7 +402,15 @@ def route_outside():
@app.route('/teachers/')
def route_teachers():
return teachers_page("/teachers/?x", "Teachers")
return teachers_page("/teachers/", "Teachers")
@app.route('/teachers/projects/')
def route_teachers_projects():
return project_list("/teachers/projects/?x", "Teachers", request.args, request.full_path, True)
@app.route('/teachers/<category>/projects/')
def route_teachers_category_projects(category):
return project_list(f"/teachers/{category}/projects/?x", "Teachers", request.args, request.full_path, True)
@app.route('/member/<member>/instructables/')
def route_member_instructables(member):