From ea63a0087fe80baf6421d8fcee1bb53072a4a407 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Wed, 25 Oct 2023 22:37:13 +0530 Subject: [PATCH 01/20] Update instagram_pic.py --- web_programming/instagram_pic.py | 43 ++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 8521da674d7d..328b3818bfdc 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -1,16 +1,37 @@ -from datetime import datetime - +import os import requests from bs4 import BeautifulSoup +def download_image(url): + try: + response = requests.get(url) + response.raise_for_status() # Check for HTTP errors + + soup = BeautifulSoup(response.text, "html.parser") + image_meta_tag = soup.find("meta", {"property": "og:image"}) + + if image_meta_tag: + image_url = image_meta_tag.get("content") + if image_url: + image_data = requests.get(image_url).content + + if image_data: + file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg" + with open(file_name, "wb") as file: + file.write(image_data) + print(f"Image downloaded and saved as {file_name}") + else: + print("Failed to download the image.") + else: + print("Image URL not found in the meta tag.") + else: + print("No meta tag with 'og:image' property found.") + except requests.exceptions.RequestException as e: + print(f"An error occurred during the HTTP request: {e}") + except Exception as e: + print(f"An unexpected error occurred: {e}") + if __name__ == "__main__": - url = input("Enter image url: ").strip() + url = input("Enter image URL: ").strip() print(f"Downloading image from {url} ...") - soup = BeautifulSoup(requests.get(url).content, "html.parser") - # The image URL is in the content field of the first meta tag with property og:image - image_url = soup.find("meta", {"property": "og:image"})["content"] - image_data = requests.get(image_url).content - file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg" - with open(file_name, "wb") as fp: - fp.write(image_data) - print(f"Done. Image saved to disk as {file_name}.") + download_image(url) From 37eac4b5f4c069420d485dd941f756cc0406d7aa Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Wed, 25 Oct 2023 22:44:06 +0530 Subject: [PATCH 02/20] Update instagram_pic.py --- web_programming/instagram_pic.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 328b3818bfdc..0aea982e0490 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -1,4 +1,4 @@ -import os +from datetime import datetime import requests from bs4 import BeautifulSoup @@ -28,8 +28,6 @@ def download_image(url): print("No meta tag with 'og:image' property found.") except requests.exceptions.RequestException as e: print(f"An error occurred during the HTTP request: {e}") - except Exception as e: - print(f"An unexpected error occurred: {e}") if __name__ == "__main__": url = input("Enter image URL: ").strip() From d2311c01012f6c3088f428632e5be61c11f96ebf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Oct 2023 17:14:10 +0000 Subject: [PATCH 03/20] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/instagram_pic.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 0aea982e0490..1798e67b2128 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -2,6 +2,7 @@ import requests from bs4 import BeautifulSoup + def download_image(url): try: response = requests.get(url) @@ -29,6 +30,7 @@ def download_image(url): except requests.exceptions.RequestException as e: print(f"An error occurred during the HTTP request: {e}") + if __name__ == "__main__": url = input("Enter image URL: ").strip() print(f"Downloading image from {url} ...") From ec14e854afab086b0a69a570827ec2d95324807b Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Wed, 25 Oct 2023 22:45:45 +0530 Subject: [PATCH 04/20] Update instagram_pic.py --- web_programming/instagram_pic.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 1798e67b2128..0aea982e0490 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -2,7 +2,6 @@ import requests from bs4 import BeautifulSoup - def download_image(url): try: response = requests.get(url) @@ -30,7 +29,6 @@ def download_image(url): except requests.exceptions.RequestException as e: print(f"An error occurred during the HTTP request: {e}") - if __name__ == "__main__": url = input("Enter image URL: ").strip() print(f"Downloading image from {url} ...") From 8d4554646ba4312c3f4390ea45ede1ad9c72471d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Oct 2023 17:16:47 +0000 Subject: [PATCH 05/20] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/instagram_pic.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 0aea982e0490..1798e67b2128 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -2,6 +2,7 @@ import requests from bs4 import BeautifulSoup + def download_image(url): try: response = requests.get(url) @@ -29,6 +30,7 @@ def download_image(url): except requests.exceptions.RequestException as e: print(f"An error occurred during the HTTP request: {e}") + if __name__ == "__main__": url = input("Enter image URL: ").strip() print(f"Downloading image from {url} ...") From 324060d630c55b960a54feca64432a0fb7a2a3fe Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Wed, 25 Oct 2023 22:51:27 +0530 Subject: [PATCH 06/20] Update instagram_pic.py --- web_programming/instagram_pic.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 1798e67b2128..0aea982e0490 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -2,7 +2,6 @@ import requests from bs4 import BeautifulSoup - def download_image(url): try: response = requests.get(url) @@ -30,7 +29,6 @@ def download_image(url): except requests.exceptions.RequestException as e: print(f"An error occurred during the HTTP request: {e}") - if __name__ == "__main__": url = input("Enter image URL: ").strip() print(f"Downloading image from {url} ...") From 1b25147e80a9d5efc72d421835dfa9a45d9a444e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Oct 2023 17:22:03 +0000 Subject: [PATCH 07/20] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/instagram_pic.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 0aea982e0490..1798e67b2128 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -2,6 +2,7 @@ import requests from bs4 import BeautifulSoup + def download_image(url): try: response = requests.get(url) @@ -29,6 +30,7 @@ def download_image(url): except requests.exceptions.RequestException as e: print(f"An error occurred during the HTTP request: {e}") + if __name__ == "__main__": url = input("Enter image URL: ").strip() print(f"Downloading image from {url} ...") From 6636eaef27920814814f2c3d4a3dd008e8987dd4 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:22:04 +0530 Subject: [PATCH 08/20] Update instagram_pic.py --- web_programming/instagram_pic.py | 67 +++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 1798e67b2128..a20d3c397386 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -1,12 +1,48 @@ from datetime import datetime +from typing import Optional import requests from bs4 import BeautifulSoup -def download_image(url): +def validate_url(url: str) -> bool: + """ + Validates the given URL. + >>> validate_url("https://www.example.com") + True + """ + # Add URL validation logic here + return True + + +def download_image_data(image_url: str) -> Optional[bytes]: + """ + Downloads image data from the given URL. + >>> download_image_data("https://www.example.com/image.jpg") # This is a hypothetical example; actual output may vary. + b'...' + """ + try: + return requests.get(image_url).content + except requests.exceptions.RequestException: + return None + + +def save_image(image_data: bytes, file_name: str) -> None: + """ + Saves the image data to a file. + """ + with open(file_name, "wb") as file: + file.write(image_data) + + +def download_image(url: str) -> str: + """ + Downloads an image from the given URL and saves it to a file. + >>> download_image("https://www.example.com") # This is a hypothetical example; actual output may vary. + 'Image downloaded and saved as image_2023-10-29_12:34:56.jpg' + """ try: response = requests.get(url) - response.raise_for_status() # Check for HTTP errors + response.raise_for_status() soup = BeautifulSoup(response.text, "html.parser") image_meta_tag = soup.find("meta", {"property": "og:image"}) @@ -14,24 +50,19 @@ def download_image(url): if image_meta_tag: image_url = image_meta_tag.get("content") if image_url: - image_data = requests.get(image_url).content - + image_data = download_image_data(image_url) if image_data: - file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg" - with open(file_name, "wb") as file: - file.write(image_data) - print(f"Image downloaded and saved as {file_name}") - else: - print("Failed to download the image.") - else: - print("Image URL not found in the meta tag.") - else: - print("No meta tag with 'og:image' property found.") - except requests.exceptions.RequestException as e: - print(f"An error occurred during the HTTP request: {e}") + file_name = f"image_{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg" + save_image(image_data, file_name) + return f"Image downloaded and saved as {file_name}" + except requests.exceptions.RequestException: + return "An error occurred during the HTTP request." if __name__ == "__main__": url = input("Enter image URL: ").strip() - print(f"Downloading image from {url} ...") - download_image(url) + if validate_url(url): + result = download_image(url) + print(result) + else: + print("Invalid URL. Please try again.") From 6e4d1711f4424dda0c07b876f61c62e1f8b4a8c0 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:25:41 +0530 Subject: [PATCH 09/20] Update instagram_pic.py --- web_programming/instagram_pic.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index a20d3c397386..2b6ac4e4b064 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -10,15 +10,12 @@ def validate_url(url: str) -> bool: >>> validate_url("https://www.example.com") True """ - # Add URL validation logic here return True -def download_image_data(image_url: str) -> Optional[bytes]: +def download_image_data(image_url: str) -> bytes | None: """ Downloads image data from the given URL. - >>> download_image_data("https://www.example.com/image.jpg") # This is a hypothetical example; actual output may vary. - b'...' """ try: return requests.get(image_url).content @@ -37,8 +34,6 @@ def save_image(image_data: bytes, file_name: str) -> None: def download_image(url: str) -> str: """ Downloads an image from the given URL and saves it to a file. - >>> download_image("https://www.example.com") # This is a hypothetical example; actual output may vary. - 'Image downloaded and saved as image_2023-10-29_12:34:56.jpg' """ try: response = requests.get(url) From d457f7cf9b874bf174102b7ca700f58ae64559f3 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:28:15 +0530 Subject: [PATCH 10/20] Update instagram_pic.py --- web_programming/instagram_pic.py | 62 ++++++++++---------------------- 1 file changed, 18 insertions(+), 44 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 2b6ac4e4b064..1798e67b2128 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -1,43 +1,12 @@ from datetime import datetime -from typing import Optional import requests from bs4 import BeautifulSoup -def validate_url(url: str) -> bool: - """ - Validates the given URL. - >>> validate_url("https://www.example.com") - True - """ - return True - - -def download_image_data(image_url: str) -> bytes | None: - """ - Downloads image data from the given URL. - """ - try: - return requests.get(image_url).content - except requests.exceptions.RequestException: - return None - - -def save_image(image_data: bytes, file_name: str) -> None: - """ - Saves the image data to a file. - """ - with open(file_name, "wb") as file: - file.write(image_data) - - -def download_image(url: str) -> str: - """ - Downloads an image from the given URL and saves it to a file. - """ +def download_image(url): try: response = requests.get(url) - response.raise_for_status() + response.raise_for_status() # Check for HTTP errors soup = BeautifulSoup(response.text, "html.parser") image_meta_tag = soup.find("meta", {"property": "og:image"}) @@ -45,19 +14,24 @@ def download_image(url: str) -> str: if image_meta_tag: image_url = image_meta_tag.get("content") if image_url: - image_data = download_image_data(image_url) + image_data = requests.get(image_url).content + if image_data: - file_name = f"image_{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg" - save_image(image_data, file_name) - return f"Image downloaded and saved as {file_name}" - except requests.exceptions.RequestException: - return "An error occurred during the HTTP request." + file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg" + with open(file_name, "wb") as file: + file.write(image_data) + print(f"Image downloaded and saved as {file_name}") + else: + print("Failed to download the image.") + else: + print("Image URL not found in the meta tag.") + else: + print("No meta tag with 'og:image' property found.") + except requests.exceptions.RequestException as e: + print(f"An error occurred during the HTTP request: {e}") if __name__ == "__main__": url = input("Enter image URL: ").strip() - if validate_url(url): - result = download_image(url) - print(result) - else: - print("Invalid URL. Please try again.") + print(f"Downloading image from {url} ...") + download_image(url) From 3b9f9f3e0c7056f9990218056a25f75ed24e86ed Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:38:22 +0530 Subject: [PATCH 11/20] Update instagram_pic.py --- web_programming/instagram_pic.py | 33 ++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 1798e67b2128..61439b4c9b7f 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -1,12 +1,19 @@ -from datetime import datetime -import requests from bs4 import BeautifulSoup +import requests - -def download_image(url): +def download_image(url: str) -> str: + """ + Download an image from a given URL by scraping the 'og:image' meta tag. + + Parameters: + url (str): The URL to scrape. + + Returns: + str: A message indicating the result of the operation. + """ try: response = requests.get(url) - response.raise_for_status() # Check for HTTP errors + response.raise_for_status() soup = BeautifulSoup(response.text, "html.parser") image_meta_tag = soup.find("meta", {"property": "og:image"}) @@ -15,23 +22,21 @@ def download_image(url): image_url = image_meta_tag.get("content") if image_url: image_data = requests.get(image_url).content - if image_data: file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg" with open(file_name, "wb") as file: file.write(image_data) - print(f"Image downloaded and saved as {file_name}") + return f"Image downloaded and saved as {file_name}" else: - print("Failed to download the image.") + return "Failed to download the image." else: - print("Image URL not found in the meta tag.") + return "Image URL not found in the meta tag." else: - print("No meta tag with 'og:image' property found.") + return "No meta tag with 'og:image' property found." except requests.exceptions.RequestException as e: - print(f"An error occurred during the HTTP request: {e}") - + return f"An error occurred during the HTTP request: {e}" if __name__ == "__main__": url = input("Enter image URL: ").strip() - print(f"Downloading image from {url} ...") - download_image(url) + result = download_image(url) + print(result) From 5378fffb5702975da9b2a08254b5695989b8ac50 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 29 Oct 2023 06:08:57 +0000 Subject: [PATCH 12/20] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/instagram_pic.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 61439b4c9b7f..ee2e4cc0491b 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -1,13 +1,14 @@ from bs4 import BeautifulSoup import requests + def download_image(url: str) -> str: """ Download an image from a given URL by scraping the 'og:image' meta tag. - + Parameters: url (str): The URL to scrape. - + Returns: str: A message indicating the result of the operation. """ @@ -36,6 +37,7 @@ def download_image(url: str) -> str: except requests.exceptions.RequestException as e: return f"An error occurred during the HTTP request: {e}" + if __name__ == "__main__": url = input("Enter image URL: ").strip() result = download_image(url) From 7ce43b30d7361dcd33fd0ec4e5ce9f426ad769ac Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:44:20 +0530 Subject: [PATCH 13/20] Update instagram_pic.py --- web_programming/instagram_pic.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index ee2e4cc0491b..1584fb963a08 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -1,14 +1,14 @@ -from bs4 import BeautifulSoup +from datetime import datetime import requests - +from bs4 import BeautifulSoup def download_image(url: str) -> str: """ Download an image from a given URL by scraping the 'og:image' meta tag. - + Parameters: url (str): The URL to scrape. - + Returns: str: A message indicating the result of the operation. """ @@ -37,7 +37,6 @@ def download_image(url: str) -> str: except requests.exceptions.RequestException as e: return f"An error occurred during the HTTP request: {e}" - if __name__ == "__main__": url = input("Enter image URL: ").strip() result = download_image(url) From 83920a5d403fa307559e83d67121afc5c969fbbd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 29 Oct 2023 06:14:54 +0000 Subject: [PATCH 14/20] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/instagram_pic.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 1584fb963a08..34389eed55ce 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -2,13 +2,14 @@ import requests from bs4 import BeautifulSoup + def download_image(url: str) -> str: """ Download an image from a given URL by scraping the 'og:image' meta tag. - + Parameters: url (str): The URL to scrape. - + Returns: str: A message indicating the result of the operation. """ @@ -37,6 +38,7 @@ def download_image(url: str) -> str: except requests.exceptions.RequestException as e: return f"An error occurred during the HTTP request: {e}" + if __name__ == "__main__": url = input("Enter image URL: ").strip() result = download_image(url) From b5dc19e56f2984259d71bd8d66dcb5982391ee4f Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:49:31 +0530 Subject: [PATCH 15/20] Update instagram_pic.py --- web_programming/instagram_pic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 34389eed55ce..c062252022d2 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -1,7 +1,7 @@ from datetime import datetime -import requests -from bs4 import BeautifulSoup +from bs4 import BeautifulSoup +import requests def download_image(url: str) -> str: """ From ccbd03f3c337973eb99fd106fa5fbe405eb359d1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 29 Oct 2023 06:20:06 +0000 Subject: [PATCH 16/20] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/instagram_pic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index c062252022d2..bca626433c63 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -3,6 +3,7 @@ from bs4 import BeautifulSoup import requests + def download_image(url: str) -> str: """ Download an image from a given URL by scraping the 'og:image' meta tag. From f300b47f4c21db7392e91e60b17bd5a4bd032d98 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:55:01 +0530 Subject: [PATCH 17/20] Update instagram_pic.py --- web_programming/instagram_pic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index bca626433c63..3e5c132b6126 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -1,8 +1,8 @@ from datetime import datetime from bs4 import BeautifulSoup -import requests +import requests def download_image(url: str) -> str: """ From 991ac68ff24593806d3caa064485b064094cedab Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 29 Oct 2023 06:25:36 +0000 Subject: [PATCH 18/20] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/instagram_pic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index 3e5c132b6126..e5b02201ac14 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -4,6 +4,7 @@ import requests + def download_image(url: str) -> str: """ Download an image from a given URL by scraping the 'og:image' meta tag. From c8f89ee2a10f72e926828a41ebcda367911a91be Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 08:23:06 +0100 Subject: [PATCH 19/20] Fast fail instead of nested ifs and PEP8: Keep try/except blocks small --- web_programming/instagram_pic.py | 52 ++++++++++++++++---------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index e5b02201ac14..ddcf285c447d 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -1,8 +1,7 @@ from datetime import datetime -from bs4 import BeautifulSoup - import requests +from bs4 import BeautifulSoup def download_image(url: str) -> str: @@ -10,38 +9,39 @@ def download_image(url: str) -> str: Download an image from a given URL by scraping the 'og:image' meta tag. Parameters: - url (str): The URL to scrape. + url: The URL to scrape. Returns: - str: A message indicating the result of the operation. + A message indicating the result of the operation. """ try: response = requests.get(url) response.raise_for_status() + except requests.exceptions.RequestException as e: + return f"An error occurred during the HTTP request to {url}: {e!r}" - soup = BeautifulSoup(response.text, "html.parser") - image_meta_tag = soup.find("meta", {"property": "og:image"}) - - if image_meta_tag: - image_url = image_meta_tag.get("content") - if image_url: - image_data = requests.get(image_url).content - if image_data: - file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg" - with open(file_name, "wb") as file: - file.write(image_data) - return f"Image downloaded and saved as {file_name}" - else: - return "Failed to download the image." - else: - return "Image URL not found in the meta tag." - else: - return "No meta tag with 'og:image' property found." + soup = BeautifulSoup(response.text, "html.parser") + image_meta_tag = soup.find("meta", {"property": "og:image"}) + if not image_meta_tag: + return "No meta tag with property 'og:image' was found." + + image_url = image_meta_tag.get("content") + if not image_url: + return f"Image URL not found in meta tag {image_meta_tag}." + + try: + image_data = requests.get(image_url).content except requests.exceptions.RequestException as e: - return f"An error occurred during the HTTP request: {e}" + return f"An error occurred during the HTTP request to {image_url}: {e!r}" + if not image_data: + return f"Failed to download the image from {image_url}." + + file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg" + with open(file_name, "wb") as file: + file.write(image_data) + return f"Image downloaded and saved in the file {file_name}" if __name__ == "__main__": - url = input("Enter image URL: ").strip() - result = download_image(url) - print(result) + url = input("Enter image URL: ").strip() or "https://www.instagram.com" + print(f"download_image({url}): {download_image(url)}") From 245c2db9416f781bb339f5c9576a3a75295815e4 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 08:26:58 +0100 Subject: [PATCH 20/20] Update instagram_pic.py --- web_programming/instagram_pic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py index ddcf285c447d..2630c8659232 100644 --- a/web_programming/instagram_pic.py +++ b/web_programming/instagram_pic.py @@ -37,8 +37,8 @@ def download_image(url: str) -> str: return f"Failed to download the image from {image_url}." file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg" - with open(file_name, "wb") as file: - file.write(image_data) + with open(file_name, "wb") as out_file: + out_file.write(image_data) return f"Image downloaded and saved in the file {file_name}"