From 42e6415807340c49175d6603275d9cac8076f7d5 Mon Sep 17 00:00:00 2001 From: fundor333 Date: Mon, 14 Apr 2025 00:56:09 +0200 Subject: [PATCH] Fixing scripts --- .pre-commit-config.yaml | 11 +++-------- hydra.py | 31 ++++++++++++++++++------------- make-post.py | 24 ++++++++++++++++-------- makefile | 1 + mastodon2hugo.py | 18 +++++++++--------- send_webmention.py | 2 +- 6 files changed, 48 insertions(+), 39 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a66c6375..a63f1478 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,20 +1,15 @@ fail_fast: true repos: - repo: https://github.com/adamchainz/djade-pre-commit - rev: "1.3.2" # Replace with the latest tag on GitHub + rev: "1.4.0" # Replace with the latest tag on GitHub hooks: - id: djade args: [--target-version, "4.2"] # Replace with Django version - - repo: https://github.com/pycqa/bandit - rev: 1.8.3 - hooks: - - id: bandit - args: ["-iii", "-ll"] - repo: https://github.com/psf/black rev: 25.1.0 hooks: - id: black - language_version: python3.9 + language_version: python3.12 - repo: https://github.com/pre-commit/pre-commit-hooks rev: v5.0.0 hooks: @@ -38,7 +33,7 @@ repos: rev: v3.19.1 hooks: - id: pyupgrade - args: [--py311-plus] + args: [--py312-plus] - repo: https://github.com/PyCQA/flake8 rev: 7.2.0 hooks: diff --git a/hydra.py b/hydra.py index 18038855..94b9eca0 100644 --- a/hydra.py +++ b/hydra.py @@ -26,7 +26,7 @@ class Config: if config_filename != "": # Update settings if there is a config file - with open(config_filename, "r") as file: + with open(config_filename) as file: file_text = file.read() config_json = json.loads(file_text) self.tags = config_json.get("tags", self.tags) @@ -37,7 +37,9 @@ class Config: self.threads = config_json.get("threads", self.threads) self.timeout = config_json.get("timeout", self.timeout) self.OK = config_json.get("OK", self.OK) - self.graceful_exit = config_json.get("graceful_exit", self.graceful_exit) + self.graceful_exit = config_json.get( + "graceful_exit", self.graceful_exit + ) def __str__(self): text = ( @@ -55,7 +57,7 @@ class Parser(HTMLParser): """Parse tags found in webpages to get more links to check""" def __init__(self, config): - super(Parser, self).__init__() + super().__init__() self.links = [] self.config = config @@ -101,7 +103,7 @@ class Checker: self.broken = [] self.domain = extract_domain(url) self.visited = set() - self.mailto_links = list() + self.mailto_links = [] self.pool = futures.ThreadPoolExecutor(max_workers=self.config.threads) self.report = "" @@ -120,8 +122,8 @@ class Checker: self.broken.append(entry) def load_url(self, page, timeout): - """ Try to retrieve contents of a page and record result - Store the link to be checked and its parent in the result + """Try to retrieve contents of a page and record result + Store the link to be checked and its parent in the result """ result = { "url": page["url"], @@ -138,7 +140,7 @@ class Checker: }, ) - try: + try: # noqa: B025 http_response = request.urlopen(r, timeout=self.config.timeout) encoding = http_response.headers.get("Content-Encoding") @@ -205,10 +207,10 @@ class Checker: parser = Parser(self.config) links = parser.feed_me(page["data"]) new_links = [x for x in links if x not in self.visited] - full_links = [parse.urljoin(parent, l) for l in new_links] - for l in full_links: - if l not in self.visited: - li = {"parent": parent, "url": l} + full_links = [parse.urljoin(parent, element) for element in new_links] + for element in full_links: + if element not in self.visited: + li = {"parent": parent, "url": element} self.TO_PROCESS.put(li) def make_report(self): @@ -223,7 +225,10 @@ class Checker: self.report += "\n---\n" sorted_list = sorted(self.broken, key=lambda k: k["code"], reverse=True) for link in sorted_list: - self.report += f"\n- code: {link['code']}\n url: {link['link']}\n parent: {link['parent']}\n error: {link['err']}\n" + self.report += ( + f"\n- code: {link['code']}\n url: {link['link']}\n" + + f" parent: {link['parent']}\n error: {link['err']}\n" + ) return self.report def run(self): @@ -232,7 +237,7 @@ class Checker: try: target_url = self.TO_PROCESS.get(block=True, timeout=4) if target_url["url"].startswith("mailto:"): - email = target_url["url"][len("mailto:") :] + email = target_url["url"][len("mailto:") :] # noqa: E203 self.mailto_links.append(email) elif target_url["url"] not in self.visited: diff --git a/make-post.py b/make-post.py index 1b3625f8..64f070a6 100644 --- a/make-post.py +++ b/make-post.py @@ -1,28 +1,31 @@ import datetime import os -from os import listdir -from os.path import isfile, join -import random def post_photo(): # get the current year as variable year = str(datetime.datetime.now().year) name = input("Give me the title\n") - os.system(f"hugo new photos/{year}/{name.replace(' ','-').replace(',','').lower()}/index.md") + os.system( + f"hugo new photos/{year}/{name.replace(' ', '-').replace(',', '').lower()}/index.md" + ) def post_redirect(): # get the current year as variable name = input("Give me the title\n") - os.system(f"hugo new redirect/{name.replace(' ','-').replace(',','').lower()}/index.md") + os.system( + f"hugo new redirect/{name.replace(' ', '-').replace(',', '').lower()}/index.md" + ) def post_fc(): # get the current year as variable year = str(datetime.datetime.now().year) name = input("Give me the title\n") - os.system(f"hugo new post/{year}/{name.replace(' ','-').replace(',','').lower()}/index.md") + os.system( + f"hugo new post/{year}/{name.replace(' ', '-').replace(',', '').lower()}/index.md" + ) def micro_fc(): @@ -30,13 +33,18 @@ def micro_fc(): name = input("Give me the title\n") year = str(datetime.datetime.now().year) month = str(datetime.datetime.now().month) - generated = f"{year.ljust(4,"0")}/{month.ljust(2,"0")}/{name.replace(' ', '-').replace(',', '').lower()}" + generated = f"{year.ljust(4, "0")}/{month.ljust(2, "0")}/{name.replace(' ', '-').replace(',', '').lower()}" os.system(f"hugo new micro/{generated}/index.md") print(f"Generated {generated}/index.md") -ANSWER = {"post": post_fc, "micro": micro_fc, 'photo': post_photo, 'redirect': post_redirect} +ANSWER = { + "post": post_fc, + "micro": micro_fc, + "photo": post_photo, + "redirect": post_redirect, +} def main_checker(): diff --git a/makefile b/makefile index 7a06a115..674955ac 100644 --- a/makefile +++ b/makefile @@ -30,6 +30,7 @@ gomodule: ## Update Go Module .PHONY: hydra hydra: ## Check links @python hydra.py http://localhost:1313/ --config ./hydra-config.json + @python hydra.py http://fundor333.com/ --config ./hydra-config.json .PHONY: syntax syntax: ## Build the style of the code diff --git a/mastodon2hugo.py b/mastodon2hugo.py index df191a67..ec6e35ef 100644 --- a/mastodon2hugo.py +++ b/mastodon2hugo.py @@ -2,9 +2,9 @@ # By Francesco Maida (https://github.com/fmaida) # # This script will download a copy of the -# .well-known/webfinger file from your mastodon account +# .well-known/webfinger file from your mastodon account # and will place it under your 'static' directory -# +# # Why you should use it? # ---------------------- # This way, if any user try to search on mastodon by using @@ -20,7 +20,7 @@ from urllib.request import urlretrieve from urllib.error import URLError -# Let's try to fetch the Mastodon account +# Let's try to fetch the Mastodon account # from the command line argument if len(sys.argv) > 1: account = sys.argv[1] @@ -46,7 +46,7 @@ else: # Now let's check if the user provided a valid account if not valid_account: # Sorry, it's a no go. - print(f"'{account}' doesn't look like a valid Mastodon account.") + print(f"{account!r} doesn't look like a valid Mastodon account.") exit(-1) # If the user provided a valid account, @@ -64,14 +64,14 @@ webfinger_file = os.path.join(well_known_dir, "webfinger") # Let's check if a 'static' directory already exists if not os.path.exists(static_dir): # Nope. We need to create it - print(f"Creating static directory at '{static_dir}'") + print(f"Creating static directory at {static_dir!r}") os.makedirs(static_dir) -# Let's check if a '.well-known' sub-directory +# Let's check if a '.well-known' sub-directory # already exists inside of the 'static' directory if not os.path.exists(well_known_dir): # Nope. We need to create it - print(f"Creating static/.well-known directory at '{well_known_dir}'") + print(f"Creating static/.well-known directory at {well_known_dir!r}") os.makedirs(well_known_dir) # Let's download the Mastodon account's webfinger file @@ -80,9 +80,9 @@ try: urlretrieve(url, webfinger_file) except URLError: # Uh Oh, something went wrong - print(f"Couldn't connect to '{url}'") + print(f"Couldn't connect to {url!r}") quit(-1) # Everything is done print("Done! Have a nice day!") -quit(0) \ No newline at end of file +quit(0) diff --git a/send_webmention.py b/send_webmention.py index ecae4837..0fc42a4f 100644 --- a/send_webmention.py +++ b/send_webmention.py @@ -28,6 +28,6 @@ def get_url_from_feed(feed_url: str): send_webmention(link.link) -if __name__ == '__main__': +if __name__ == "__main__": for url in feed_url: get_url_from_feed(url)