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

Create instagram_pic #3945

merged 5 commits into from
Nov 24, 2020

Conversation

Epic-R-R
Copy link
Contributor

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@Epic-R-R Epic-R-R requested a review from cclauss as a code owner November 24, 2020 06:34
@ghost ghost added the awaiting reviews This PR is ready to be reviewed label Nov 24, 2020
@ghost
Copy link

ghost commented Nov 24, 2020

Pull Request Report

@Epic-R-R Hello! I'm a bot made to check all the pull request Python files. First of all, I want to say thank you for your time and interest in this project and for opening a pull request.

I have detected errors in some of the Python files submitted in this pull request. Please read through the report and make the necessary changes. You can take a look at the relevant links provided after the report.

What are node paths?

The report contain headings and a checklist where the items are paths to the class/function/parameter where the error is present. Node paths are double colon :: separated names and can be any of the following format:

  • Class path: [file_name]::[class_name]
  • Function path: [file_name]::[function_name]
  • Function parameter path: [file_name]::[function_name]::[parameter_name]
  • Method path: [file_name]::[class_name]::[function_name]
  • Method parameter path: [file_name]::[class_name]::[function_name]::[parameter_name]

Following functions require tests [doctest/unittest/pytest]:

  • web_programming/instagram_pic.py::DownloadSingleFile

Following function parameters require type hints:

  • web_programming/instagram_pic.py::DownloadSingleFile::URL

Relevant links:

@ghost ghost added require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html and removed awaiting reviews This PR is ready to be reviewed labels Nov 24, 2020
@ghost ghost added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass and removed require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html awaiting reviews This PR is ready to be reviewed labels Nov 24, 2020
Comment on lines 1 to 3
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: ")
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

Comment on lines 8 to 9
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

Comment on lines 10 to 11
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.

Comment on lines 12 to 15
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.

fileName = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S") + ".jpg"
with open(fileName, "wb") as fp:
fp.write(r.content)
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}.")

@ghost ghost added the awaiting changes A maintainer has requested changes to this PR label Nov 24, 2020
import datetime

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.

@erdum
Copy link
Contributor

erdum commented Nov 24, 2020

Plz add type hints in your function parameters like this, def test(a: int, b: int) -> int:
Like that you have to define the types of parameters and the type of return of the function,
And plz jus install doctest pip3 install doctest, then read this tutorial on how to use doctest in your code https://www.geeksforgeeks.org/testing-in-python-using-doctest-module/, then commit changes and hope pre-commit check will be clear thanks.

@Epic-R-R
Copy link
Contributor Author

I corrected the code and commit it, but it still gives an error in pre-commit isort field

@erdum
Copy link
Contributor

erdum commented Nov 24, 2020

Yes i saw that your flake8 and isort is failing

@erdum
Copy link
Contributor

erdum commented Nov 24, 2020

Install isort in your machine and run isort in this file locally

@erdum
Copy link
Contributor

erdum commented Nov 24, 2020

And also install flake8 to see what styling mistake you done and if you can't corrected manually then install black and run black on this file it will correct all your styling mistake and flake8 check should be clear

@ghost ghost removed the tests are failing Do not merge until tests pass label Nov 24, 2020
@Epic-R-R
Copy link
Contributor Author

thanks, I use isort and then All checks have passed

@ghost ghost removed the awaiting changes A maintainer has requested changes to this PR label Nov 24, 2020
@cclauss cclauss merged commit e031ad3 into TheAlgorithms:master Nov 24, 2020
@cclauss
Copy link
Member

cclauss commented Nov 24, 2020

Nice work! Thanks for doing this.

@erdum
Copy link
Contributor

erdum commented Nov 24, 2020

cclauss please also review my pull request

stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Create instagram_pic

* Update instagram_pic

* Update instagram_pic

* isort

* Update instagram_pic.py

Co-authored-by: Christian Clauss <[email protected]>
peRFectBeliever pushed a commit to peRFectBeliever/Python that referenced this pull request Apr 1, 2021
* Create instagram_pic

* Update instagram_pic

* Update instagram_pic

* isort

* Update instagram_pic.py

Co-authored-by: Christian Clauss <[email protected]>
Panquesito7 pushed a commit to Panquesito7/Python that referenced this pull request May 13, 2021
* Create instagram_pic

* Update instagram_pic

* Update instagram_pic

* isort

* Update instagram_pic.py

Co-authored-by: Christian Clauss <[email protected]>
shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
* Create instagram_pic

* Update instagram_pic

* Update instagram_pic

* isort

* Update instagram_pic.py

Co-authored-by: Christian Clauss <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants