fix: update file path handling in main.py and simplify get_path function

This commit is contained in:
patilk 2024-11-16 00:42:11 +01:00
parent b509a852f1
commit a8136a7ff4
Signed by: s500042
GPG Key ID: 1921AD722E7392EE
2 changed files with 6 additions and 20 deletions

View File

@ -7,14 +7,14 @@ hrefs = []
def main(): def main():
directory = "dist" directory = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(directory, "data.json") file_path = os.path.join(directory, "dist", "data.json")
scrap(urls[0]) scrap(urls[0])
data = [] data = []
try: try:
os.mkdir("dist") os.mkdir(os.path.join(directory, "dist"))
except FileExistsError: except FileExistsError:
pass pass
with open(file_path, "w", encoding="utf-8") as file: with open(file_path, "w", encoding="utf-8") as file:

View File

@ -1,24 +1,10 @@
import os import os
from run_command import run_command
def get_path(): def get_path():
pwd = run_command("pwd") current_path = os.path.dirname(os.path.abspath(__file__))
splitted = pwd.split("/") path = "/".join(current_path.split("/")[:-1])
splitted[-1] = splitted[-1].replace("\n", "") return path
if splitted.count("webscraper") > 1 and "webscraper" in splitted:
for i in range(len(splitted) - 1, -1, -1):
potential_path = "/".join(splitted[: i + 1])
if "webscraper" in potential_path:
script_path = f"{potential_path}/scripts"
if os.path.isdir(script_path):
return potential_path
else:
return "This is not a valid webscraper project."
else:
path = "/".join(splitted[: splitted.index("webscraper") + 1])
return path
def run_main(): def run_main():