-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Update instagram_pic.py #10957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+41
−10
Merged
Update instagram_pic.py #10957
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ea63a00
Update instagram_pic.py
nababuddin 37eac4b
Update instagram_pic.py
nababuddin d2311c0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ec14e85
Update instagram_pic.py
nababuddin 8d45546
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 324060d
Update instagram_pic.py
nababuddin 1b25147
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6636eae
Update instagram_pic.py
nababuddin 6e4d171
Update instagram_pic.py
nababuddin d457f7c
Update instagram_pic.py
nababuddin 3b9f9f3
Update instagram_pic.py
nababuddin 5378fff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7ce43b3
Update instagram_pic.py
nababuddin 83920a5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b5dc19e
Update instagram_pic.py
nababuddin ccbd03f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f300b47
Update instagram_pic.py
nababuddin 991ac68
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c8f89ee
Fast fail instead of nested ifs and PEP8: Keep try/except blocks small
cclauss 245c2db
Update instagram_pic.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,68 @@ | ||
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 | ||
""" | ||
# 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() | ||
|
||
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 = download_image_data(image_url) | ||
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." | ||
|
||
|
||
if __name__ == "__main__": | ||
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}.") | ||
url = input("Enter image URL: ").strip() | ||
if validate_url(url): | ||
result = download_image(url) | ||
print(result) | ||
else: | ||
print("Invalid URL. Please try again.") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed on Python 3.12
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cclauss tried many times but every time getting this error
Error: web_programming/instagram_pic.py:1:1: I001 Import block is un-sorted or un-formatted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the imports were working fine before, then do not change them.
Click the text I001 to read the rule and how it works.
python3 -m pip install ruff
and thenruff --select=I --fix web_programming/instagram_pic.py