-
-
Save kagermanov27/f5c1d3d052651dcbc7e0436ffb7542e4 to your computer and use it in GitHub Desktop.
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
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