Skip to content

Create instagram_pic #3945

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
merged 5 commits into from
Nov 24, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions web_programming/instagram_pic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import requests
from bs4 import BeautifulSoup
import datetime
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import requests
from bs4 import BeautifulSoup
import datetime
import requests
from bs4 import BeautifulSoup
from datetime import datetime

Please run isort on the imports.


if __name__ == "__main__":
url = input("Enter image url: ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
url = input("Enter image url: ")
url = input("Enter image url: ").strip()

As discussed in CONTRIBUTING.md.

print("Downloading image...")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print("Downloading image...")
print(f"Downloading image from {url} ...")

Use f-strings as discussed in CONTRIBUTING.md

req = requests.get(url)
soup = BeautifulSoup(req.content, "html.parser")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
req = requests.get(url)
soup = BeautifulSoup(req.content, "html.parser")
soup = BeautifulSoup(requests.get(url).content, "html.parser")

Avoid creating variables that are only used on the next line unless:

  1. The lines are already close to the max_line_length of 88 chars per line -- or --
  2. The variable name clarifies something that is unclear

metaTag = soup.find_all("meta", {"property": "og:image"}) #get all meta tags
imgURL = metaTag[0]["content"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
metaTag = soup.find_all("meta", {"property": "og:image"}) #get all meta tags
imgURL = metaTag[0]["content"]
# The image URL is in the content field of the first meta tag with the property og:image
image_url = soup.find("meta", {"property": "og:image"})["content"]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python variable names are in snake_case as discussed in CONTRIBUTING.md.

r = requests.get(imgURL)
fileName = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S") + ".jpg"
with open(fileName, "wb") as fp:
fp.write(r.content)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
r = requests.get(imgURL)
fileName = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S") + ".jpg"
with open(fileName, "wb") as fp:
fp.write(r.content)
image_data = requests.get(imgURL).content
file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg"
with open(file_name, "wb") as fp:
fp.write(image_data)

Avoid single-letter variable names -- They make code look like it was written in the 1970's. The reader of this code does not care about the response, but they do care about the image_data so focus their attention on that.

Use f-strings which are more expressive especially with complex types like datetimes.

print("Done. Image saved to disk as " + fileName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print("Done. Image saved to disk as " + fileName)
print(f"Done. Image saved to disk as {file_name}.")