Skip to content
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

Adding support to download the Files #9

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/pyzenodo3/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .utils import download_file
from .base import Record, Zenodo
14 changes: 13 additions & 1 deletion src/pyzenodo3/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from bs4 import BeautifulSoup
from bs4.element import Tag
from urllib.parse import urlencode

from .utils import download_file
BASE_URL = "https://zenodo.org/api/"


Expand Down Expand Up @@ -67,6 +67,18 @@ def original_version(self):
return re.match(r".*/tree/(.*$)", identifier["identifier"]).group(1)
return None

def download(self, root="./"):


for file in self.data['files']:
url = file['links']['self']
checksum_md5 = file['checksum'].lstrip("md5:")

# now we will download the files to the root.
download_file(root= root,url= url, checksum = checksum_md5)



def __str__(self):
return str(self.data)

Expand Down
12 changes: 12 additions & 0 deletions src/pyzenodo3/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ def test_meta():
zup.meta(metain)

assert metain.with_suffix(".json").is_file()


def test_download(zen):
rec = zen.get_record("888276") # this is record related to scivision query search.

rec.download("./")

file_downloaded = Path("./Geomagnetic%20Model%20of%20Brazil.ipynb")

assert file_downloaded.is_file()

file_downloaded.unlink()
70 changes: 70 additions & 0 deletions src/pyzenodo3/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import requests
from tqdm import tqdm
import hashlib
import urllib
from pathlib import Path
#FILE_PATH_TYPE = Union(str, pathlib.Path )
USER_AGENT = "pyzenodo3"

def calculate_md5(file_path, chunk_size =1024):
"""
A function to calculate the md5 hash of a file.

"""


m = hashlib.md5()
with open(file_path, "rb") as f:
for chunk in iter(lambda : f.read(chunk_size), b""):
m.update(chunk)
return m.hexdigest();



def check_md5(file_loc, checksum:str)->bool:

if (calculate_md5(file_loc)!=checksum):
return False
return True

# check if the given root exist, if not make the root, then create the

def download_file(url, checksum, root = "./"):
root = Path(root)

if (not root.is_dir()):
root.mkdir()

file_name = url.split("/")[-1]
file_loc = root/file_name

if (file_loc.is_file() and check_md5(file_loc, checksum)):
print(f"File {file_name} already downloaded at location {file_loc}")
return

_urlretrieve(url, file_loc)

if (not check_md5(file_loc, checksum)):
file_loc.unlink()
print(f"MD5 checksum did not match for {url}. Deleting the {file_loc}. If you trust the file, please manually download.")
return
print(f"file {file_loc} downloaded successfully")

def _save_response_content(
content,
destination,
length= None,
) :
with open(destination, "wb") as fh, tqdm(total=length) as pbar:
for chunk in content:
# filter out keep-alive new chunks
if not chunk:
continue

fh.write(chunk)
pbar.update(len(chunk))


def _urlretrieve(url, file_loc, chunk_size = 1024 * 32):
with urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": USER_AGENT})) as response:
_save_response_content(iter(lambda: response.read(chunk_size), b""), file_loc, length=response.length)