Skip to content

Instantly share code, notes, and snippets.

@kagermanov27
Last active May 19, 2022 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kagermanov27/f5c1d3d052651dcbc7e0436ffb7542e4 to your computer and use it in GitHub Desktop.
Save kagermanov27/f5c1d3d052651dcbc7e0436ffb7542e4 to your computer and use it in GitHub Desktop.
def download(self, link):
print("Downloading {}".format(link))
## Find Latest File Name
if self.query.q in os.listdir("datasets/test"):
file_names = os.listdir("datasets/test/{}".format(self.query.q))
else:
os.mkdir("datasets/test/{}".format(self.query.q))
file_names = os.listdir("datasets/test/{}".format(self.query.q))
if len(file_names) > 0:
file_names = [int(x.split('.')[0]) for x in file_names]
latest_file_name = str(max(file_names) + 1)
else:
latest_file_name = "0"
## Download and guess the file format
r = requests.get(link)
extension = mimetypes.guess_extension(r.headers.get('content-type', '').split(';')[0])
if not extension == ".html":
with open("datasets/test/{}/{}{}".format(self.query.q, latest_file_name, extension or '.jpg'), 'wb') as f:
f.write(r.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment