Skip to content

Commit

Permalink
Merge pull request #24 from PeterDing/fix/disentangle
Browse files Browse the repository at this point in the history
Improving the download and upload functionality
  • Loading branch information
PeterDing authored Apr 2, 2024
2 parents ee8ef43 + 30ad8f5 commit 2ba550f
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 267 deletions.
47 changes: 45 additions & 2 deletions alipcs_py/alipcs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,21 @@ def download_link(self, file_id: str) -> Optional[PcsDownloadUrl]:
info = self._alipcs.download_link(file_id)
return PcsDownloadUrl.from_(info)

def update_download_url(self, pcs_file: PcsFile) -> PcsFile:
"""Update the download url of the `pcs_file` if it is expired
Return a new `PcsFile` with the updated download url.
"""

assert pcs_file.is_file, f"{pcs_file} is not a file"

pcs_file = deepcopy(pcs_file)
if pcs_file.download_url_expires():
pcs_url = self.download_link(pcs_file.file_id)
if pcs_url:
pcs_file.download_url = pcs_url.url
return pcs_file

def file_stream(
self,
file_id: str,
Expand Down Expand Up @@ -982,6 +997,21 @@ def download_link(self, file_id: str) -> Optional[PcsDownloadUrl]:
info = self._aliopenpcs.download_link(file_id)
return PcsDownloadUrl.from_(info)

def update_download_url(self, pcs_file: PcsFile) -> PcsFile:
"""Update the download url of the `pcs_file` if it is expired
Return a new `PcsFile` with the updated download url.
"""

assert pcs_file.is_file, f"{pcs_file} is not a file"

pcs_file = deepcopy(pcs_file)
if pcs_file.download_url_expires():
pcs_url = self.download_link(pcs_file.file_id)
if pcs_url:
pcs_file.download_url = pcs_url.url
return pcs_file

def file_stream(
self,
file_id: str,
Expand Down Expand Up @@ -1053,11 +1083,24 @@ def __init__(
def download_link(self, file_id: str) -> Optional[PcsDownloadUrl]:
"""Get the download link of the `file_id`"""

if self._aliopenpcsapi:
if self._aliopenpcsapi is not None:
return self._aliopenpcsapi.download_link(file_id)
else:
return super().download_link(file_id)

def update_download_url(self, pcs_file: PcsFile) -> PcsFile:
"""Update the download url of the `pcs_file` if it is expired
Return a new `PcsFile` with the updated download url.
"""

assert pcs_file.is_file, f"{pcs_file} is not a file"

if self._aliopenpcsapi is not None:
return self._aliopenpcsapi.update_download_url(pcs_file)
else:
return super().update_download_url(pcs_file)

def file_stream(
self,
file_id: str,
Expand All @@ -1067,7 +1110,7 @@ def file_stream(
) -> Optional[RangeRequestIO]:
"""File stream as a normal io"""

if self._aliopenpcsapi:
if self._aliopenpcsapi is not None:
return self._aliopenpcsapi.file_stream(
file_id, max_chunk_size=max_chunk_size, callback=callback, encrypt_password=encrypt_password
)
Expand Down
2 changes: 1 addition & 1 deletion alipcs_py/alipcs/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def refresh(*args, **kwargs):

share_auth = self.__class__.SHARE_AUTHS.get(share_id)
if share_auth:
share_auth.expire_time = 0.0
share_auth.expire_time = 0
continue

elif code == "ParamFlowException":
Expand Down
10 changes: 9 additions & 1 deletion alipcs_py/alipcs/inner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
import re
import urllib.parse
import warnings

from alipcs_py.common.date import iso_8601_to_timestamp, now_timestamp

Expand Down Expand Up @@ -178,6 +179,11 @@ def download_url_expires(self) -> bool:
def update_download_url(self, api: "AliPCSApi"):
"""Update the download url if it expires"""

warnings.warn(
"This method is deprecated and will be removed in a future version, use `update_download_url` in `AliPCSApi` instead",
DeprecationWarning,
)

if self.is_file:
if self.download_url_expires():
pcs_url = api.download_link(self.file_id)
Expand Down Expand Up @@ -394,7 +400,7 @@ class SharedAuth:
share_id: str
share_password: str
share_token: str
expire_time: float
expire_time: int
expires_in: int
info: Any

Expand Down Expand Up @@ -578,6 +584,7 @@ def from_(info) -> "PcsRateLimit":
@dataclass
class PcsDownloadUrl:
url: Optional[str] = None
download_url: Optional[str] = None # url and download_url seem the same, the download rate is same
internal_url: Optional[str] = None
cdn_url: Optional[str] = None
size: Optional[int] = None
Expand All @@ -594,6 +601,7 @@ def from_(info) -> "PcsDownloadUrl":

return PcsDownloadUrl(
url=info.get("url"),
download_url=info.get("download_url"),
internal_url=info.get("internal_url"),
cdn_url=info.get("cdn_url"),
size=info.get("size"),
Expand Down
22 changes: 14 additions & 8 deletions alipcs_py/alipcs/pcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
ALIYUNDRIVE_COM_API = "https://api.aliyundrive.com"
ALIYUNDRIVE_OPENAPI_DOMAIN = "https://openapi.aliyundrive.com"

# TODO: Update UA
PCS_UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
PCS_HEADERS = {"Origin": ALIYUNDRIVE_COM[:-1], "Referer": ALIYUNDRIVE_COM + "/", "User-Agent": PCS_UA}
PCS_HEADERS = {"Origin": ALIYUNDRIVE_COM, "Referer": ALIYUNDRIVE_COM + "/", "User-Agent": PCS_UA}

CheckNameMode = Literal[
"overwrite", # 直接覆盖,以后多版本有用
Expand Down Expand Up @@ -769,14 +770,15 @@ def get_share_token(self, share_id: str, share_password: str = ""):
"""Get share token"""

shared_auth = self.__class__.SHARE_AUTHS.get(share_id)
if shared_auth and not shared_auth.is_expired():
if shared_auth is not None:
share_password = share_password or shared_auth.share_password
return shared_auth.info

if not shared_auth.is_expired():
return shared_auth.info

url = PcsNode.ShareToken.url()
data = dict(share_id=share_id, share_pwd=share_password)
resp = self._request(Method.Post, url, json=data)

info = resp.json()
if info.get("share_token"):
# Store share password for refreshing share token
Expand Down Expand Up @@ -883,6 +885,10 @@ def user_info(self):
@assert_ok
@handle_error
def download_link(self, file_id: str):
info = self.meta(file_id)["responses"][0]["body"]
if info.get("url") or info.get("download_url"):
return info

url = PcsNode.DownloadUrl.url()
data = dict(drive_id=self.default_drive_id, file_id=file_id)
headers = dict(PCS_HEADERS)
Expand All @@ -898,7 +904,7 @@ def file_stream(
encrypt_password: bytes = b"",
) -> Optional[RangeRequestIO]:
info = self.download_link(file_id)
url = info["url"]
url = info.get("url") or info.get("download_url")

headers = {
"User-Agent": PCS_UA,
Expand Down Expand Up @@ -1207,8 +1213,8 @@ def meta(self, *file_ids: str, share_id: str = None):

responses = []
for file_id in file_ids:
data = dict(file_id=file_id, drive_id=self.default_drive_id)
url = PcsNode.Meta.url()
data = dict(file_id=file_id, fields="*", drive_id=self.default_drive_id)
url = OpenPcsNode.Meta.url()
resp = self._request(Method.Post, url, json=data)
info = resp.json()
responses.append(dict(body=info))
Expand Down Expand Up @@ -1276,7 +1282,7 @@ def list(

assert limit <= 200, "`limit` should be less than 200"

url = PcsNode.FileList.url()
url = OpenPcsNode.FileList.url()
orderby = "name"
if name:
orderby = "name"
Expand Down
Loading

0 comments on commit 2ba550f

Please sign in to comment.