Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
farfun committed Nov 26, 2024
1 parent 3a51f75 commit 9bbe865
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 47 deletions.
2 changes: 1 addition & 1 deletion extbuild/fundrive-lanzou/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "fundrive-lanzou"
version = "1.2.35"
version = "1.2.36"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.8"
Expand Down
3 changes: 3 additions & 0 deletions logs/all.log
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
2024-11-26 14:29:51.312 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-26 15:01:34.049 |INFO | funbuild.core.core : core: 77 | funbuild | - fundrive build
2024-11-26 15:01:34.049 |INFO | funbuild.core.core : core: 65 | funbuild | - fundrive pull
2024-11-26 15:01:45.938 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
3 changes: 3 additions & 0 deletions logs/funbuild.log
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
2024-11-26 14:29:51.312 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-26 15:01:34.049 |INFO | funbuild.core.core : core: 77 | funbuild | - fundrive build
2024-11-26 15:01:34.049 |INFO | funbuild.core.core : core: 65 | funbuild | - fundrive pull
2024-11-26 15:01:45.938 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "fundrive"
version = "1.2.35"
version = "1.2.36"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.8"
Expand Down
3 changes: 2 additions & 1 deletion src/fundrive/drives/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .alipan import AlipanDrive
from .lanzou import LanZouDrive, LanZouSnapshot
from .opendatalab import OpenDataLabDrive
from .openxlab import OpenDataLabDrive, OpenXLabDrive
from .os import OSDrive
from .oss import OSSDrive
from .tianchi import TianChiDrive
Expand All @@ -13,6 +13,7 @@
"download_tsinghua",
"TianChiDrive",
"OSSDrive",
"OpenXLabDrive",
"OpenDataLabDrive",
"LanZouDrive",
"LanZouSnapshot",
Expand Down
4 changes: 4 additions & 0 deletions src/fundrive/drives/openxlab/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .drive import OpenXLabDrive as OpenDataLabDrive
from .drive import OpenXLabDrive

__all__ = ["OpenXLabDrive", "OpenDataLabDrive"]
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
logger = getLogger("fundrive")


class OpenDataLabDrive(BaseDrive):
class OpenXLabDrive(BaseDrive):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.host = "https://openxlab.org.cn"
Expand Down
3 changes: 3 additions & 0 deletions src/fundrive/drives/os/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .drive import OSDrive

__all__ = ["OSDrive"]
File renamed without changes.
Empty file.
4 changes: 2 additions & 2 deletions src/fundrive/drives/oss/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .drive import OSSDrive,public_oss_url
from .drive import OSSDrive, public_oss_url

__all__=['OSSDrive','public_oss_url']
__all__ = ["OSSDrive", "public_oss_url"]
3 changes: 3 additions & 0 deletions src/fundrive/drives/tianchi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .drive import TianChiDrive

__all__ = ["TianChiDrive"]
File renamed without changes.
4 changes: 4 additions & 0 deletions src/fundrive/drives/tsinghua/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .drive import TSingHuaDrive, download
from .drive import download as download_tsinghua

__all__ = ["TSingHuaDrive", "download", "download_tsinghua"]
File renamed without changes.
3 changes: 3 additions & 0 deletions src/fundrive/drives/wenshushu/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .drive import Uploader, Downloader, BaseDrive, WSSDrive

__all__ = ["Uploader", "Downloader", "BaseDrive", "WSSDrive"]
File renamed without changes.
Empty file.
2 changes: 1 addition & 1 deletion src/fundrive/drives/zenodo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .drive import ZenodoDrive

__all__=['ZenodoDrive']
__all__ = ["ZenodoDrive"]
121 changes: 83 additions & 38 deletions src/fundrive/drives/zenodo/drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,70 +12,101 @@


class ZenodoDrive(BaseDrive):

def __init__(self, sandbox=False):
super().__init__()
self.access_token = None
self.base_url = "https://sandbox.zenodo.org/api" if sandbox else "https://zenodo.org/api"
self.base_url = (
"https://sandbox.zenodo.org/api" if sandbox else "https://zenodo.org/api"
)

def check_token(self):
if not isinstance(self.access_token, str) or not self.access_token:
logger.error("Token need to be a string")
r = requests.get(f"{self.base_url}/deposit/depositions", params={"access_token": self.access_token})
r = requests.get(
f"{self.base_url}/deposit/depositions",
params={"access_token": self.access_token},
)
if r.status_code != 200:
logger.error(f"Token accept error, status code: {r.status_code} {r.json()['message']}")
logger.error(
f"Token accept error, status code: {r.status_code} {r.json()['message']}"
)
logger.success("access token success")

def login(self, access_token=None, *args, **kwargs) -> bool:
self.access_token = access_token or read_secret("fundrive", 'zenodo', 'access_token')
self.access_token = access_token or read_secret(
"fundrive", "zenodo", "access_token"
)
return True

def get_file_info(self, fid=None, record_id=None, filepath=None, *args, **kwargs) -> DriveFile:
def get_file_info(
self, fid=None, record_id=None, filepath=None, *args, **kwargs
) -> DriveFile:
url = fid
if record_id is not None and fid is not None:
url = f"{self.base_url}/records/{record_id}/files/{filepath}"
response = requests.get(url, *args, **kwargs).json()
return DriveFile(fid=url, name=response["key"], **response)

def get_file_list(self, record_id, *args, **kwargs) -> List[DriveFile]:
url = f'{self.base_url}/records/{record_id}'
url = f"{self.base_url}/records/{record_id}"
response = requests.get(url).json()
print(response)
result = []
for file in response["files"]:
result.append(DriveFile(
fid=file["links"]['self'].rstrip('/content'),
path=file["key"],
name=file['key'],
size=file["size"],
url=file["links"]['self']))
result.append(
DriveFile(
fid=file["links"]["self"].rstrip("/content"),
path=file["key"],
name=file["key"],
size=file["size"],
url=file["links"]["self"],
)
)
return result

def get_dir_list(self, record_id, *args, **kwargs) -> List[DriveFile]:
return self.get_file_list(record_id, *args, **kwargs)

def download_file(self, record_id, local_dir, overwrite=False, *args, **kwargs) -> bool:
def download_file(
self, record_id, local_dir, overwrite=False, *args, **kwargs
) -> bool:
for file in self.get_file_list(record_id, *args, **kwargs):
simple_download(url=file['url'], filepath=f"{local_dir}/{file['key']}")
simple_download(url=file["url"], filepath=f"{local_dir}/{file['key']}")
return True

def download_dir(self, record_id, local_dir, recursion=True, overwrite=False, *args, **kwargs) -> bool:
return self.download_file(record_id, local_dir, overwrite=overwrite, *args, **kwargs)
def download_dir(
self, record_id, local_dir, recursion=True, overwrite=False, *args, **kwargs
) -> bool:
return self.download_file(
record_id, local_dir, overwrite=overwrite, *args, **kwargs
)

def mkdir(self, fid=None, name=None, return_if_exist=True, *args, **kwargs) -> dict:
r = requests.post(f"{self.base_url}/deposit/depositions",
params={"access_token": self.access_token},
json={},
headers={"Content-Type": "application/json"})
r = requests.post(
f"{self.base_url}/deposit/depositions",
params={"access_token": self.access_token},
json={},
headers={"Content-Type": "application/json"},
)

if r.status_code != 201:
logger.error(f"Error in creation, status code: {r.status_code} {r.json()['message']}")
logger.error(
f"Error in creation, status code: {r.status_code} {r.json()['message']}"
)
# deposition_id = r.json()["id"]
# bucket_url = r.json()["links"]["bucket"]
return r.json()

def upload_file(self, local_path, record_id=None, bucket_url=None, recursion=True, overwrite=False, *args,
**kwargs) -> bool:
def upload_file(
self,
local_path,
record_id=None,
bucket_url=None,
recursion=True,
overwrite=False,
*args,
**kwargs,
) -> bool:
filename = os.path.basename(local_path)
# old API
if record_id is not None:
Expand All @@ -86,7 +117,9 @@ def upload_file(self, local_path, record_id=None, bucket_url=None, recursion=Tru
files={"file": open(local_path, "rb")},
)
if r.status_code != 201:
logger.error(f"Error in data upload, status code: {r.status_code} {r.json()['message']}")
logger.error(
f"Error in data upload, status code: {r.status_code} {r.json()['message']}"
)
return False
return True

Expand All @@ -97,26 +130,38 @@ def upload_file(self, local_path, record_id=None, bucket_url=None, recursion=Tru
with open(local_path, "rb") as fp:
r = requests.put(
url=f"{bucket_url}/{os.path.basename(local_path)}",
params={'access_token': self.access_token},
params={"access_token": self.access_token},
data=fp,
)
print(json.dumps(r.json(), indent=2))
logger.success(f"{local_path} ID = {record_id} (DOI: 10.5281/zenodo.{record_id})")
logger.success(
f"{local_path} ID = {record_id} (DOI: 10.5281/zenodo.{record_id})"
)
return True

def update_meta(self, record_id=None, title='My first upload', description='My first upload', names='fundrive',
creators=None):
def update_meta(
self,
record_id=None,
title="My first upload",
description="My first upload",
names="fundrive",
creators=None,
):
data = {
'metadata': {
'title': title,
'upload_type': 'poster',
'description': description,
'creators': creators or [{'name': name, 'affiliation': 'farfarfun'} for name in names.split(',')]
"metadata": {
"title": title,
"upload_type": "poster",
"description": description,
"creators": creators
or [
{"name": name, "affiliation": "farfarfun"}
for name in names.split(",")
],
}
}
r = requests.put(
f'https://zenodo.org/api/deposit/depositions/{record_id}',
params={'access_token': self.access_token},
f"https://zenodo.org/api/deposit/depositions/{record_id}",
params={"access_token": self.access_token},
data=json.dumps(data),
headers={"Content-Type": "application/json"},
)
Expand All @@ -125,8 +170,8 @@ def update_meta(self, record_id=None, title='My first upload', description='My f

def publish(self, record_id=None):
r = requests.post(
url=f'{self.base_url}/deposit/depositions/{record_id}/actions/publish',
params={'access_token': self.access_token}
url=f"{self.base_url}/deposit/depositions/{record_id}/actions/publish",
params={"access_token": self.access_token},
)
if r.status_code != 202:
logger.error("publish error")
Expand Down
4 changes: 2 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9bbe865

Please sign in to comment.