Skip to content

Commit

Permalink
feat: refactor to automatically obtain folder id
Browse files Browse the repository at this point in the history
  • Loading branch information
DDSRem committed Dec 6, 2024
1 parent 3c4378a commit 8611553
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 79 deletions.
60 changes: 39 additions & 21 deletions all_in_one.sh
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ function get_aliyunpan_folder_id() {
-v "${1}:/data" \
-e LANG=C.UTF-8 \
ddsderek/xiaoya-glue:python \
bash /get_folder_id/get_folder_id.sh
/get_folder_id/get_folder_id.py --data_path='/data' --drive_mode=r
;;
*)
WARN "目前阿里云盘 folder id 自动获取只支持amd64和arm64架构,你的架构是:$cpu_arch"
Expand All @@ -995,6 +995,43 @@ function get_aliyunpan_folder_id() {

}

function settings_aliyunpan_folder_id() {

if [ ! -f "${1}/temp_transfer_folder_id.txt" ]; then
while true; do
INFO "是否自动获取 阿里云盘转存目录 folder id [Y/n](默认 Y)"
read -erp "Token:" auto_get_folder_id
[[ -z "${auto_get_folder_id}" ]] && auto_get_folder_id="y"
if [[ ${auto_get_folder_id} == [YyNn] ]]; then
break
else
ERROR "非法输入,请输入 [Y/n]"
fi
done
if [[ ${auto_get_folder_id} == [Yy] ]]; then
get_aliyunpan_folder_id "${1}"
fi

folderidfilesize=$(cat "${1}"/temp_transfer_folder_id.txt)
folderidstringsize=${#folderidfilesize}
if [ "$folderidstringsize" -le 39 ]; then
while true; do
INFO "输入你的阿里云盘转存目录 folder id"
read -erp "FOLDERID:" folderid
folder_id_len=${#folderid}
if [ "$folder_id_len" -ne 40 ]; then
ERROR "长度不对,阿里云盘 folder id 是40位长"
ERROR "请参考指南配置文件: https://xiaoyaliu.notion.site/xiaoya-docker-69404af849504fa5bcf9f2dd5ecaa75f"
else
echo "$folderid" > "${1}"/temp_transfer_folder_id.txt
break
fi
done
fi
fi

}

function get_config_dir() {

local xiaoya_config_dir DEFAULT_CONFIG_DIR
Expand Down Expand Up @@ -1219,26 +1256,7 @@ function install_xiaoya_alist() {
settings_aliyunpan_opentoken "${CONFIG_DIR}"
fi

folderidfilesize=$(cat "${CONFIG_DIR}"/temp_transfer_folder_id.txt)
folderidstringsize=${#folderidfilesize}
if [ "$folderidstringsize" -le 39 ]; then
while true; do
INFO "输入你的阿里云盘转存目录 folder id(留空自动获取)"
read -erp "FOLDERID:" folderid
if [ -z "${folderid}" ]; then
get_aliyunpan_folder_id "${CONFIG_DIR}"
folderid=$(cat "${CONFIG_DIR}"/temp_transfer_folder_id.txt)
fi
folder_id_len=${#folderid}
if [ "$folder_id_len" -ne 40 ]; then
ERROR "长度不对,阿里云盘 folder id 是40位长"
ERROR "请参考指南配置文件: https://xiaoyaliu.notion.site/xiaoya-docker-69404af849504fa5bcf9f2dd5ecaa75f"
else
echo "$folderid" > "${CONFIG_DIR}"/temp_transfer_folder_id.txt
break
fi
done
fi
settings_aliyunpan_folder_id "${CONFIG_DIR}"

settings_pikpak_account "${CONFIG_DIR}"

Expand Down
5 changes: 5 additions & 0 deletions glue_python/get_folder_id/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Get Folder ID

## Thanks

- [aligo](https://github.com/foyoux/aligo)
115 changes: 96 additions & 19 deletions glue_python/get_folder_id/get_folder_id.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,98 @@
#!/usr/local/bin/python3

import os
import json
import logging
import requests
import argparse
from aligo import Aligo

with open('/data/mytoken.txt', 'r') as file:
refresh_token = file.readline().strip()
ali = Aligo(refresh_token=refresh_token)

# 这里默认使用资源盘
v2_user = ali.v2_user_get()
resource_drive_id = v2_user.resource_drive_id
ali.default_drive_id = resource_drive_id

file_list = ali.get_file_list()
folder_id = ''
for folder in file_list:
if folder.name == '小雅转存文件夹':
folder_id = folder.file_id
break
if not folder_id:
folder_id = ali.create_folder(name='小雅转存文件夹').file_id

print(folder_id)

def write_to_file(file_path, data):
with open(file_path, 'w') as file:
file.write(data)


def get_refresh_token(path):
with open(f'{path}/mytoken.txt', 'r') as file:
return file.readline().strip()


def update_refresh_token(path):
file_path = os.path.join(os.path.expanduser("~"), ".aligo", "aligo.json")
try:
with open(file_path, 'r') as file:
data = json.load(file)
aligo_refresh_token = data.get("refresh_token")
if not aligo_refresh_token:
logging.error("读取 Refresh Token 失败")
return False
except Exception as e:
logging.error(f"读取 Refresh Token 发生错误:{e}")
return False
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36",
"Content-Type": "application/json",
"Referer": "https://www.aliyundrive.com/"
}
data = {
"refresh_token": aligo_refresh_token,
"grant_type": "refresh_token"
}
try:
response = requests.post("https://auth.aliyundrive.com/v2/account/token", headers=headers, json=data)
response.raise_for_status()
refresh_token = response.json().get("refresh_token")
if refresh_token:
with open(f"{path}/mytoken.txt", "w") as file:
file.write(refresh_token)
logging.info("刷新 Refresh Token 成功")
return True
else:
logging.error("刷新 Refresh Token 失败")
return False
except requests.RequestException:
logging.error("网络问题,无法刷新 Refresh Token")
return False


def get_folder_id(client, drive_mode):
if drive_mode == 'r':
v2_user = client.v2_user_get()
resource_drive_id = v2_user.resource_drive_id
client.default_drive_id = resource_drive_id
file_list = client.get_file_list()
folder_id = ''
for folder in file_list:
if folder.name == '小雅转存文件夹':
folder_id = folder.file_id
break
if not folder_id:
try:
folder_id = client.create_folder(name='小雅转存文件夹').file_id
except Exception as e:
logging.error(f"创建 小雅转存文件夹 失败:{e}")
return None
return folder_id


if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser(description='Get Folder ID')
parser.add_argument('--drive_mode', type=str, required=True, help='选择操作盘类型,资源盘:r,备份盘:b')
parser.add_argument('--data_path', type=str, required=True, help='小雅配置文件路径')
args = parser.parse_args()
refresh_token = get_refresh_token(args.data_path)
try:
ali = Aligo(refresh_token=refresh_token)
except Exception as e:
logging.error(f"登入阿里云盘失败:{e}")
folder_id = get_folder_id(ali, args.drive_mode)
if folder_id is not None:
logging.info(f'阿里云盘转存目录 folder id: {folder_id}')
write_to_file(f'{args.data_path}/temp_transfer_folder_id.txt', folder_id)
write_to_file(f'{args.data_path}/folder_type.txt', args.drive_mode)
else:
logging.error('自动获取 阿里云盘转存目录 folder id 失败,请手动获取!')
if not update_refresh_token(args.data_path):
logging.error('请手动更新 mytoken.txt 文件中的 Refresh Token')
38 changes: 0 additions & 38 deletions glue_python/get_folder_id/get_folder_id.sh

This file was deleted.

4 changes: 3 additions & 1 deletion glue_python/get_folder_id/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
aligo
aligo
argparse
requests

0 comments on commit 8611553

Please sign in to comment.