Skip to content

Commit

Permalink
[+]分享文件查看 #46
Browse files Browse the repository at this point in the history
[+]分享文件转存 #52
  • Loading branch information
wxy1343 committed Oct 3, 2021
1 parent 287b113 commit 97a0588
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 29 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ python main.py -h
<td>-a, --album</td>
<td>是否访问相册</td>
</tr>
<tr>
<td>-s, --share-id</td>
<td>指定分享id</td>
</tr>
<tr>
<td>-sp, --share-pwd</td>
<td>指定分享密码</td>
</tr>
</tbody>
</table>
</details>
Expand Down
2 changes: 1 addition & 1 deletion aliyunpan/about.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.7.15'
__version__ = '2.8.0'
80 changes: 67 additions & 13 deletions aliyunpan/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from datetime import datetime
from pathlib import Path
from threading import RLock
from typing import List

import requests
import simplejson

# from aliyunpan.api import ua
from aliyunpan.api.req import *
from aliyunpan.api.type import UserInfo, AlibumInfo
from aliyunpan.api.type import UserInfo, AlibumInfo, Share
from aliyunpan.api.utils import *
from aliyunpan.common import *
from aliyunpan.exceptions import InvalidRefreshToken, AliyunpanException, AliyunpanCode, LoginFailed, \
Expand All @@ -22,11 +23,12 @@

class AliyunPan(object):

def __init__(self, refresh_token: str = None, album: bool = False):
def __init__(self, refresh_token: str = None, album: bool = False, share: Share = Share()):
self._req = Req(self)
self._user_info = None
self._alibum_info = None
self._album = album
self._share = share
self._access_token = None
self._drive_id = None
self._username = None
Expand All @@ -46,6 +48,7 @@ def __init__(self, refresh_token: str = None, album: bool = False):
drive_id = property(lambda self: (self._lock.acquire(), next(self._drive_id_gen_), self._lock.release())[1],
lambda self, value: setattr(self, '_drive_id', value))
album = property(lambda self: self._album, lambda self, value: setattr(self, '_album', value))
share = property(lambda self: self._share)

def login(self, username: str = None, password: str = None, ua: str = None):
"""
Expand Down Expand Up @@ -104,10 +107,21 @@ def get_file_list(self, parent_file_id: str = 'root', next_marker: str = None, r
:param next_marker:
:return:
"""
url = 'https://api.aliyundrive.com/v2/file/list'
json = {"drive_id": self.drive_id, "parent_file_id": parent_file_id, 'fields': '*', 'marker': next_marker}
json = {"parent_file_id": parent_file_id}
if next_marker:
json['marker'] = next_marker
headers = {}
kwargs = {}
if self._share.share_id:
url = 'https://api.aliyundrive.com/adrive/v3/file/list'
json.update({'share_id': self._share.share_id, 'share_pwd': self._share.share_pwd})
headers = {'x-share-token': self.get_share_token()}
kwargs = {'access_token': None}
else:
url = 'https://api.aliyundrive.com/v2/file/list'
json.update({"drive_id": self.drive_id, 'fields': '*'})
logger.info(f'Get the list of parent_file_id {parent_file_id}.')
r = self._req.post(url, json=json)
r = self._req.post(url, json=json, headers=headers, **kwargs)
try:
logger.debug(r.json())
except simplejson.errors.JSONDecodeError:
Expand Down Expand Up @@ -140,21 +154,43 @@ def delete_file(self, file_id: str):
return r.json()['responses'][0]['id']
return False

def move_file(self, file_id: str, parent_file_id: str):
def batch(self, file_id_list: list, parent_file_id: str, force: bool = False) -> requests.models.Response:
"""
移动文件
"""
file_json_list = []
auto_rename = False if force else True
headers = {}
for file_id in file_id_list:
body = {'file_id': file_id, 'to_parent_file_id': parent_file_id, 'auto_rename': auto_rename}
file_json = {'body': body,
'headers': {'Content-Type': 'application/json'}, 'id': 0, 'method': 'POST',
'url': '/file/move'}
file_json_list.append(file_json)
if self._share.share_id:
url = 'https://api.aliyundrive.com/adrive/v2/batch'
for i, file_json in enumerate(file_json_list):
file_json_list[i]['body']['to_drive_id'] = self.drive_id
file_json_list[i]['body']['share_id'] = self._share.share_id
file_json_list[i]['url'] = '/file/copy'
headers['x-share-token'] = self.get_share_token()
else:
url = 'https://api.aliyundrive.com/v2/batch'
for i, file_json in enumerate(file_json_list):
file_json_list[i]['id'] = file_json['body']['file_id']
file_json_list[i]['body']['drive_id'] = self.drive_id
json = {'requests': file_json_list, 'resource': "file"}
return self._req.post(url, json=json, headers=headers)

def move_file(self, file_id: List[str], parent_file_id: str):
"""
移动文件
:param file_id:
:param parent_file_id:
:return:
"""
url = 'https://api.aliyundrive.com/v2/batch'
json = {"requests": [{"body": {"drive_id": self.drive_id, "file_id": file_id,
"to_parent_file_id": parent_file_id},
"headers": {"Content-Type": "application/json"},
"id": file_id, "method": "POST", "url": "/file/move"}],
"resource": "file"}
logger.info(f'Move files {file_id} to {parent_file_id}')
r = self._req.post(url, json=json)
r = self.batch([file_id], parent_file_id)
if r.status_code == 200:
if 'message' in r.json()['responses'][0]['body']:
print(r.json()['responses'][0]['body']['message'])
Expand Down Expand Up @@ -724,6 +760,24 @@ def share_link(self, file_id_list: list, expiration=''):
return r.json()['share_url']

def get_share_by_anonymous(self, share_id: str):
"""
获取分享文件列表
"""
url = f'https://api.aliyundrive.com/adrive/v3/share_link/get_share_by_anonymous'
r = self._req.post(url, json={'share_id': share_id})
return r.json()['file_infos']

def get_share_token(self, share: Share = None):
"""
获取share_token
"""
if not share:
share = self._share
if share.share_token:
return share.share_token
url = 'https://api.aliyundrive.com/v2/share_link/get_share_token'
json = {'share_id': share.share_id, 'share_pwd': share.share_pwd}
r = self._req.post(url, json=json)
share_token = r.json().get('share_token', '')
share.share_token = share_token
return share_token
16 changes: 9 additions & 7 deletions aliyunpan/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,24 @@ def get_file_info(info):
for info in info_list:
if info['type'] == 'file':
file_info = FileInfo(name=info['name'], id=info['file_id'], pid=info['parent_file_id'], type=True,
ctime=time.strptime(info['created_at'], '%Y-%m-%dT%H:%M:%S.%fZ'),
ctime=time.strptime(info['created_at'],
'%Y-%m-%dT%H:%M:%S.%fZ') if 'created_at' in info else time.localtime(),
update_time=time.strptime(info['updated_at'], '%Y-%m-%dT%H:%M:%S.%fZ'),
hidden=info['hidden'], category=info['category'],
content_type=info['content_type'],
size=info['size'], content_hash_name=info['content_hash_name'],
content_hash=info['content_hash'],
hidden=info.get('hidden'), category=info['category'],
content_type=info.get('content_type'),
size=info['size'], content_hash_name=info.get('content_hash_name'),
content_hash=info.get('content_hash'),
download_url=info['download_url'] if 'download_url' in info else '',
video_media_metadata=info[
'video_media_metadata'] if 'video_media_metadata' in info else None,
video_preview_metadata=info[
'video_preview_metadata'] if 'video_preview_metadata' in info else None)
else:
file_info = FileInfo(name=info['name'], id=info['file_id'], pid=info['parent_file_id'], type=False,
ctime=time.strptime(info['created_at'], '%Y-%m-%dT%H:%M:%S.%fZ'),
ctime=time.strptime(info['created_at'],
'%Y-%m-%dT%H:%M:%S.%fZ') if 'created_at' in info else time.time(),
update_time=time.strptime(info['updated_at'], '%Y-%m-%dT%H:%M:%S.%fZ'),
hidden=info['hidden'])
hidden=info.get('hidden'))
file_info_list.append(file_info)
return file_info_list

Expand Down
13 changes: 12 additions & 1 deletion aliyunpan/api/type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import namedtuple

__all__ = ['FileInfo', 'UserInfo', 'ShareInfo', 'AlibumInfo']
__all__ = ['FileInfo', 'UserInfo', 'ShareInfo', 'AlibumInfo', 'Share']

_file_info = (
'name', 'id', 'pid', 'type', 'ctime', 'update_time', 'hidden', 'category', 'content_type', 'size',
Expand All @@ -15,3 +15,14 @@
ShareInfo.__new__.__defaults__ = ('',) * 6
AlibumInfo = namedtuple('AlibumInfo', ['drive_name', 'drive_id'])
AlibumInfo.__new__.__defaults__ = ('',) * 2


class Share:
share_id = ''
share_pwd = ''
share_token = ''

def __init__(self, share_id='', share_pwd='', share_token=''):
self.share_id = share_id
self.share_pwd = share_pwd
self.share_token = share_token
6 changes: 4 additions & 2 deletions aliyunpan/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from aliyunpan.api.core import AliyunPan
from aliyunpan.api.models import *
from aliyunpan.api.req import *
from aliyunpan.api.type import Share
from aliyunpan.api.utils import *
from aliyunpan.cli.config import Config
from aliyunpan.cli.tui import AliyunpanTUI
Expand All @@ -23,7 +24,7 @@ class Commander:
def __init__(self, init=True, *args, **kwargs):
self._disk = AliyunPan()
self._path_list = PathList(self._disk)
self._req = Req()
self._req = Req(self._disk)
self._config = Config()
self._task_config = Config(ROOT_DIR / Path('tasks.yaml'))
self._share_link = 'aliyunpan://'
Expand All @@ -46,11 +47,12 @@ def __del__(self):
pass

def init(self, config_file=None, refresh_token=None, username=None, password=None, depth=3, timeout=None,
drive_id=None, album=False):
drive_id=None, album=False, share_id='', share_pwd=''):
self._path_list.depth = depth
self._req.timeout = timeout
self._disk.drive_id = drive_id
self._disk.album = album
self._disk._share = Share(share_id, share_pwd)
config_file_list = list(
filter(lambda x: get_real_path(x).is_file(), map(lambda x: get_real_path(x), self._config_set)))
if config_file:
Expand Down
11 changes: 9 additions & 2 deletions aliyunpan/cli/tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,11 @@ def update_file_list(self, name=None, file_id=None):
# 保存当前目录信息
self._parent_file_info = None
if file_id != 'root':
self._parent_file_info = self.parent.parentApp._cli._path_list._tree.get_node(file_id).data
file_node = self.parent.parentApp._cli._path_list._tree.get_node(file_id)
if file_node:
self._parent_file_info = file_node.data
else:
self._file_list = self.parent.parentApp._cli._path_list.get_fid_list('root', update=False)
else:
self._file_list = self.parent.parentApp._cli._path_list.get_fid_list(self._parent_file_info.id,
update=False)
Expand All @@ -447,7 +451,10 @@ def update_path(self):
pid = self._parent_file_info.pid if self._parent_file_info else None
while True:
if pid:
file_info = self.parent.parentApp._cli._path_list._tree.get_node(pid).data
file_node = self.parent.parentApp._cli._path_list._tree.get_node(pid)
if not file_node:
file_node = self.parent.parentApp._cli._path_list._tree.get_node('root')
file_info = file_node.data
if file_info:
path = file_info.name / path
pid = file_info.pid
Expand Down
8 changes: 5 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
@click.option('-D', '--debug', is_flag=True, help='Debug mode.')
@click.option('-T', '--timeout', type=click.FLOAT, help='Api request timeout.')
@click.option('-id', '--drive-id', type=click.STRING, help='Specify DRIVE_ID.')
@click.option('-a', '--album', is_flag=True, help='Specify album')
def cli(config_file, refresh_token, username, password, depth, debug, timeout, drive_id, album):
@click.option('-a', '--album', is_flag=True, help='Specify album.')
@click.option('-s', '--share-id', type=click.STRING, help='Specify share_id.')
@click.option('-sp', '--share-pwd', type=click.STRING, help='Specify share_pwd.')
def cli(config_file, refresh_token, username, password, depth, debug, timeout, drive_id, album, share_id, share_pwd):
logger.info(f'Version:{__version__}')
if debug:
logger.setLevel('DEBUG')
commander.init(config_file=None if refresh_token or username else config_file,
refresh_token=None if username else refresh_token, username=username, password=password, depth=depth,
timeout=timeout, drive_id=drive_id, album=album)
timeout=timeout, drive_id=drive_id, album=album, share_id=share_id, share_pwd=share_pwd)


@cli.command(aliases=['l', 'list', 'dir'], help='List files.')
Expand Down

0 comments on commit 97a0588

Please sign in to comment.