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 6a7f717 commit 3a51f75
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 22 deletions.
Binary file added logs/all.2024-11-24_13-43-56_708498.log.gz
Binary file not shown.
12 changes: 1 addition & 11 deletions logs/all.log
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
2024-11-24 13:43:56.706 |INFO | funbuild.core.core : core: 77 | funbuild | - fundrive build
2024-11-24 13:43:56.709 |INFO | funbuild.core.core : core: 65 | funbuild | - fundrive pull
2024-11-24 13:44:09.740 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-24 13:45:13.541 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-24 13:46:25.644 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-24 13:46:52.557 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-24 15:17:21.882 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-24 21:25:04.117 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-24 21:25:08.783 |INFO | funbuild.core.core : core: 77 | funbuild | - fundrive build
2024-11-24 21:25:08.783 |INFO | funbuild.core.core : core: 65 | funbuild | - fundrive pull
2024-11-24 21:25:20.750 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-26 14:29:51.312 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
Binary file added logs/funbuild.2024-11-24_13-43-56_709576.log.gz
Binary file not shown.
12 changes: 1 addition & 11 deletions logs/funbuild.log
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
2024-11-24 13:43:56.706 |INFO | funbuild.core.core : core: 77 | funbuild | - fundrive build
2024-11-24 13:43:56.709 |INFO | funbuild.core.core : core: 65 | funbuild | - fundrive pull
2024-11-24 13:44:09.740 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-24 13:45:13.541 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-24 13:46:25.644 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-24 13:46:52.557 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-24 15:17:21.882 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-24 21:25:04.117 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-24 21:25:08.783 |INFO | funbuild.core.core : core: 77 | funbuild | - fundrive build
2024-11-24 21:25:08.783 |INFO | funbuild.core.core : core: 65 | funbuild | - fundrive pull
2024-11-24 21:25:20.750 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
2024-11-26 14:29:51.312 |INFO | funbuild.core.core : core: 69 | funbuild | - fundrive push
3 changes: 3 additions & 0 deletions src/fundrive/drives/oss/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .drive import OSSDrive,public_oss_url

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

__all__=['ZenodoDrive']
134 changes: 134 additions & 0 deletions src/fundrive/drives/zenodo/drive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import json
import os
from typing import List

import requests
from fundrive.core import BaseDrive, DriveFile
from funget import simple_download
from funsecret import read_secret
from funutil import getLogger

logger = getLogger("fundrive")


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"

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})
if r.status_code != 200:
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')
return True

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}'
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']))
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:
for file in self.get_file_list(record_id, *args, **kwargs):
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 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"})

if r.status_code != 201:
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:
filename = os.path.basename(local_path)
# old API
if record_id is not None:
r = requests.post(
url=f"{self.base_url}/deposit/depositions/{record_id}/files",
params={"access_token": self.access_token},
data={"name": filename},
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']}")
return False
return True

# new API
if bucket_url is None:
bucket_url = self.mkdir()["links"]["bucket"]

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},
data=fp,
)
print(json.dumps(r.json(), indent=2))
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):
data = {
'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},
data=json.dumps(data),
headers={"Content-Type": "application/json"},
)
if r.status_code != 200:
logger.error("update meta error")

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}
)
if r.status_code != 202:
logger.error("publish error")
return False
return True
Empty file.

0 comments on commit 3a51f75

Please sign in to comment.