Init 1.0 codes
This commit is contained in:
141
leonapp-cli/README.md
Normal file
141
leonapp-cli/README.md
Normal file
@@ -0,0 +1,141 @@
|
||||
# LeonApp CLI
|
||||
|
||||
一个用于访问App Store API的命令行工具,支持查看应用列表、应用详情、标签、开发者信息、公告和统计数据等功能。
|
||||
|
||||
## 功能特性
|
||||
|
||||
- **应用管理**:查看所有应用列表、获取应用详细信息
|
||||
- **标签管理**:查看所有标签、获取标签下的应用列表
|
||||
- **开发者管理**:查看开发者的应用列表、获取开发者详细信息
|
||||
- **公告管理**:查看所有公告信息
|
||||
- **统计信息**:获取应用商店的统计数据
|
||||
- **交互式模式**:提供友好的交互式命令行界面
|
||||
- **命令行模式**:支持直接通过命令行参数执行特定操作
|
||||
- **彩色输出**:使用彩色文本提升用户体验
|
||||
- **分页支持**:支持列表数据的分页查看
|
||||
|
||||
## 环境要求
|
||||
|
||||
- Python 3.6 或更高版本
|
||||
- 安装必要的依赖包(见安装指南)
|
||||
|
||||
## 安装指南
|
||||
|
||||
1. 确保你已安装Python 3.6或更高版本
|
||||
|
||||
2. 克隆或下载此项目到本地
|
||||
|
||||
3. 安装依赖包:
|
||||
|
||||
```bash
|
||||
cd leonapp-cli
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
4. 在Windows上,你可以直接运行Python脚本:
|
||||
```bash
|
||||
python leonapp_cli.py
|
||||
```
|
||||
|
||||
在Linux/Mac上,你可能需要设置执行权限:
|
||||
```bash
|
||||
chmod +x leonapp_cli.py
|
||||
./leonapp_cli.py
|
||||
```
|
||||
|
||||
## 使用方法
|
||||
|
||||
### 1. 交互式模式
|
||||
|
||||
最简单的使用方式是直接运行脚本进入交互式模式:
|
||||
|
||||
```bash
|
||||
python leonapp_cli.py
|
||||
```
|
||||
|
||||
在交互式模式下,你可以输入命令来执行各种操作。输入 `help` 可以查看所有可用命令。
|
||||
|
||||
### 2. 命令行模式
|
||||
|
||||
你也可以直接通过命令行参数执行特定操作:
|
||||
|
||||
#### 查看应用列表
|
||||
```bash
|
||||
python leonapp_cli.py list-apps --page 1 --limit 20
|
||||
```
|
||||
|
||||
#### 查看应用详情
|
||||
```bash
|
||||
python leonapp_cli.py app-info 1
|
||||
```
|
||||
|
||||
#### 查看所有标签
|
||||
```bash
|
||||
python leonapp_cli.py list-tags
|
||||
```
|
||||
|
||||
#### 查看标签下的应用
|
||||
```bash
|
||||
python leonapp_cli.py tag-apps 1 --page 1 --limit 20
|
||||
```
|
||||
|
||||
#### 查看开发者的应用
|
||||
```bash
|
||||
python leonapp_cli.py developer-apps 1 --page 1 --limit 20
|
||||
```
|
||||
|
||||
#### 查看开发者信息
|
||||
```bash
|
||||
python leonapp_cli.py developer-info 1
|
||||
```
|
||||
|
||||
#### 查看所有公告
|
||||
```bash
|
||||
python leonapp_cli.py list-announcements --page 1 --limit 20
|
||||
```
|
||||
|
||||
#### 查看统计信息
|
||||
```bash
|
||||
python leonapp_cli.py stats
|
||||
```
|
||||
|
||||
## 交互式命令列表
|
||||
|
||||
在交互式模式下,你可以使用以下命令:
|
||||
|
||||
- `exit` 或 `quit` - 退出程序
|
||||
- `help` - 显示帮助信息
|
||||
- `list apps` - 列出所有应用
|
||||
- `app [id]` - 查看应用详情
|
||||
- `list tags` - 列出所有标签
|
||||
- `tag apps [id]` - 查看标签下的应用
|
||||
- `developer apps [id]` - 查看开发者的应用
|
||||
- `developer info [id]` - 查看开发者信息
|
||||
- `list announcements` - 列出所有公告
|
||||
- `stats` - 查看统计信息
|
||||
|
||||
## 配置
|
||||
|
||||
目前,API基础URL是硬编码在脚本中的:`http://localhost/APP Store/api.php`。如果你的API地址不同,你需要修改脚本中的`self.api_base_url`变量。
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 确保API服务器正在运行,并且可以从你的计算机访问
|
||||
2. 如果遇到连接问题,请检查网络连接和防火墙设置
|
||||
3. 脚本使用了`requests`和`colorama`库,确保它们已正确安装
|
||||
4. 部分功能可能需要特定的API权限
|
||||
|
||||
## 许可证
|
||||
|
||||
[MIT License](LICENSE)
|
||||
|
||||
## 作者
|
||||
|
||||
LeonApp CLI Team
|
||||
|
||||
## 更新日志
|
||||
|
||||
### v1.0.0
|
||||
- 初始版本,支持所有基本功能
|
||||
- 提供交互式和命令行两种使用模式
|
||||
- 支持彩色输出和分页功能
|
||||
122
leonapp-cli/api_example.py
Normal file
122
leonapp-cli/api_example.py
Normal file
@@ -0,0 +1,122 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
LeonApp API示例 - 展示如何以编程方式调用API
|
||||
"""
|
||||
|
||||
import json
|
||||
import requests
|
||||
|
||||
class LeonAppAPI:
|
||||
def __init__(self, api_base_url="http://localhost/APP Store/api.php"):
|
||||
"""初始化API客户端"""
|
||||
self.api_base_url = api_base_url
|
||||
|
||||
def make_request(self, endpoint_type, params=None):
|
||||
"""基础API请求函数"""
|
||||
if params is None:
|
||||
params = {}
|
||||
|
||||
# 添加API类型参数
|
||||
params['t'] = endpoint_type
|
||||
|
||||
try:
|
||||
response = requests.get(self.api_base_url, params=params, timeout=30)
|
||||
response.raise_for_status() # 抛出HTTP错误
|
||||
|
||||
data = response.json()
|
||||
|
||||
if data.get('status') == 'error':
|
||||
print(f"API错误: {data.get('message', '未知错误')}")
|
||||
return None
|
||||
|
||||
return data.get('data')
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"请求异常: {str(e)}")
|
||||
return None
|
||||
except json.JSONDecodeError:
|
||||
print("无法解析响应")
|
||||
return None
|
||||
|
||||
def get_all_apps(self, page=1, limit=20):
|
||||
"""获取所有应用列表"""
|
||||
return self.make_request('getallapps', {'page': page, 'limit': limit})
|
||||
|
||||
def get_app_info(self, app_id):
|
||||
"""获取应用详情"""
|
||||
return self.make_request('getappinfo', {'id': app_id})
|
||||
|
||||
def get_all_tags(self):
|
||||
"""获取所有标签"""
|
||||
return self.make_request('getalltags')
|
||||
|
||||
def get_tag_apps(self, tag_id, page=1, limit=20):
|
||||
"""获取标签下的应用"""
|
||||
return self.make_request('gettagapp', {'id': tag_id, 'page': page, 'limit': limit})
|
||||
|
||||
def get_developer_apps(self, developer_id, page=1, limit=20):
|
||||
"""获取开发者的应用"""
|
||||
return self.make_request('getdeveloperapp', {'id': developer_id, 'page': page, 'limit': limit})
|
||||
|
||||
def get_developer_info(self, developer_id):
|
||||
"""获取开发者信息"""
|
||||
return self.make_request('getdeveloperinfo', {'id': developer_id})
|
||||
|
||||
def get_all_announcements(self, page=1, limit=20):
|
||||
"""获取所有公告"""
|
||||
return self.make_request('getacc', {'page': page, 'limit': limit})
|
||||
|
||||
def get_count_info(self):
|
||||
"""获取统计信息"""
|
||||
return self.make_request('getcount')
|
||||
|
||||
# 示例用法
|
||||
if __name__ == "__main__":
|
||||
# 创建API客户端实例
|
||||
api = LeonAppAPI()
|
||||
|
||||
print("LeonApp API示例\n")
|
||||
|
||||
# 1. 获取统计信息示例
|
||||
print("=== 获取统计信息 ===")
|
||||
count_info = api.get_count_info()
|
||||
if count_info:
|
||||
print(f"应用总数: {count_info['total_apps']}")
|
||||
print(f"开发者总数: {count_info['total_developers']}")
|
||||
print(f"标签总数: {count_info['total_tags']}")
|
||||
print(f"公告总数: {count_info['total_announcements']}")
|
||||
print(f"总下载量: {count_info['total_downloads']}")
|
||||
print()
|
||||
|
||||
# 2. 获取前5个应用示例
|
||||
print("=== 获取前5个应用 ===")
|
||||
apps = api.get_all_apps(page=1, limit=5)
|
||||
if apps and 'apps' in apps:
|
||||
for app in apps['apps']:
|
||||
print(f"ID: {app['id']}, 名称: {app['name']}")
|
||||
print()
|
||||
|
||||
# 3. 获取第一个标签的应用示例
|
||||
print("=== 获取第一个标签的应用 ===")
|
||||
tags = api.get_all_tags()
|
||||
if tags and len(tags) > 0:
|
||||
first_tag = tags[0]
|
||||
print(f"标签: {first_tag['name']} (ID: {first_tag['id']})")
|
||||
|
||||
tag_apps = api.get_tag_apps(first_tag['id'], limit=3)
|
||||
if tag_apps and 'apps' in tag_apps:
|
||||
for app in tag_apps['apps']:
|
||||
print(f"- {app['name']}")
|
||||
else:
|
||||
print("该标签下暂无应用")
|
||||
else:
|
||||
print("暂无标签数据")
|
||||
print()
|
||||
|
||||
# 4. 使用提示
|
||||
print("=== 使用提示 ===")
|
||||
print("1. 你可以在自己的脚本中导入此类并使用相应的方法")
|
||||
print("2. 根据实际需要处理API返回的数据")
|
||||
print("3. 记得添加错误处理和异常捕获")
|
||||
print("4. 如果API地址不同,初始化时可以指定:api = LeonAppAPI('http://your-api-url/api.php')")
|
||||
441
leonapp-cli/leonapp_cli.py
Normal file
441
leonapp-cli/leonapp_cli.py
Normal file
@@ -0,0 +1,441 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
LeonApp CLI - 一个用于访问App Store API的命令行工具
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
import json
|
||||
import platform
|
||||
from datetime import datetime
|
||||
import requests
|
||||
from colorama import init, Fore, Style
|
||||
|
||||
# 初始化colorama
|
||||
def init_colorama():
|
||||
"""初始化colorama,确保在Windows和其他平台上都能正确显示彩色文本"""
|
||||
init(autoreset=True)
|
||||
|
||||
class LeonAppCLI:
|
||||
def __init__(self):
|
||||
# API基础URL
|
||||
self.api_base_url = "http://leonmmcoset.jjxmm.win:8010/api.php"
|
||||
# 终端宽度,用于格式化输出
|
||||
self.terminal_width = self.get_terminal_width()
|
||||
# 初始化colorama
|
||||
init_colorama()
|
||||
|
||||
def get_terminal_width(self):
|
||||
"""获取终端宽度"""
|
||||
try:
|
||||
return os.get_terminal_size().columns
|
||||
except OSError:
|
||||
# 如果无法获取终端宽度,返回默认值
|
||||
return 80
|
||||
|
||||
def print_separator(self):
|
||||
"""打印分隔线"""
|
||||
print(Fore.YELLOW + "=" * self.terminal_width)
|
||||
|
||||
def print_header(self, text):
|
||||
"""打印带样式的标题"""
|
||||
self.print_separator()
|
||||
print(Fore.GREEN + Style.BRIGHT + text.center(self.terminal_width))
|
||||
self.print_separator()
|
||||
|
||||
def print_error(self, message):
|
||||
"""打印错误消息"""
|
||||
print(Fore.RED + f"错误: {message}")
|
||||
|
||||
def print_success(self, message):
|
||||
"""打印成功消息"""
|
||||
print(Fore.GREEN + f"成功: {message}")
|
||||
|
||||
def print_info(self, message):
|
||||
"""打印信息消息"""
|
||||
print(Fore.BLUE + message)
|
||||
|
||||
def make_api_request(self, endpoint_type, params=None):
|
||||
"""
|
||||
发送API请求
|
||||
:param endpoint_type: API端点类型
|
||||
:param params: 请求参数
|
||||
:return: 响应数据(字典)
|
||||
"""
|
||||
if params is None:
|
||||
params = {}
|
||||
|
||||
# 添加API类型参数
|
||||
params['t'] = endpoint_type
|
||||
|
||||
try:
|
||||
self.print_info(f"正在请求API: {self.api_base_url}?t={endpoint_type}")
|
||||
response = requests.get(self.api_base_url, params=params, timeout=30)
|
||||
|
||||
# 检查响应状态
|
||||
if response.status_code != 200:
|
||||
self.print_error(f"API请求失败,状态码: {response.status_code}")
|
||||
return None
|
||||
|
||||
# 解析JSON响应
|
||||
data = response.json()
|
||||
|
||||
# 检查响应状态
|
||||
if data.get('status') == 'error':
|
||||
self.print_error(data.get('message', '未知错误'))
|
||||
return None
|
||||
|
||||
return data.get('data')
|
||||
except requests.exceptions.RequestException as e:
|
||||
self.print_error(f"网络请求异常: {str(e)}")
|
||||
return None
|
||||
except json.JSONDecodeError:
|
||||
self.print_error("无法解析API响应")
|
||||
return None
|
||||
|
||||
def list_all_apps(self, page=1, limit=20):
|
||||
"""获取所有APP列表"""
|
||||
data = self.make_api_request('getallapps', {'page': page, 'limit': limit})
|
||||
if data:
|
||||
self.print_header(f"应用列表 (第 {page} 页,共 {data['pagination']['totalPages']} 页)")
|
||||
|
||||
if not data['apps']:
|
||||
self.print_info("暂无应用数据")
|
||||
return
|
||||
|
||||
# 打印应用列表
|
||||
for app in data['apps']:
|
||||
print(Fore.CYAN + f"ID: {app['id']}")
|
||||
print(Fore.YELLOW + f"名称: {app['name']}")
|
||||
print(Fore.WHITE + f"描述: {app['description'][:100]}{'...' if len(app['description']) > 100 else ''}")
|
||||
print(Fore.WHITE + f"评分: {app.get('avg_rating', '暂无')}")
|
||||
print(Fore.WHITE + f"下载量: {app.get('total_downloads', 0)}")
|
||||
print("-")
|
||||
|
||||
# 打印分页信息
|
||||
self.print_info(f"总数: {data['pagination']['total']} 个应用")
|
||||
|
||||
def get_app_info(self, app_id):
|
||||
"""获取APP详细信息"""
|
||||
data = self.make_api_request('getappinfo', {'id': app_id})
|
||||
if data:
|
||||
self.print_header(f"应用详情 - {data['name']}")
|
||||
|
||||
# 打印应用基本信息
|
||||
print(Fore.CYAN + f"ID: {data['id']}")
|
||||
print(Fore.YELLOW + f"名称: {data['name']}")
|
||||
print(Fore.WHITE + f"版本: {data['version']}")
|
||||
print(Fore.WHITE + f"年龄分级: {data['age_rating']}")
|
||||
print(Fore.WHITE + f"描述: {data['description']}")
|
||||
print(Fore.WHITE + f"评分: {data.get('avg_rating', '暂无')}")
|
||||
print(Fore.WHITE + f"下载量: {data.get('total_downloads', 0)}")
|
||||
|
||||
# 打印标签信息
|
||||
if 'tags' in data and data['tags']:
|
||||
tags = ', '.join([tag['name'] for tag in data['tags']])
|
||||
print(Fore.WHITE + f"标签: {tags}")
|
||||
|
||||
# 打印版本信息
|
||||
if 'versions' in data and data['versions']:
|
||||
print(Fore.MAGENTA + "\n版本历史:")
|
||||
for version in data['versions'][:3]: # 只显示前3个版本
|
||||
print(Fore.WHITE + f"- 版本 {version['version']} ({version['download_count']} 下载)")
|
||||
|
||||
# 打印图片数量
|
||||
if 'images' in data and data['images']:
|
||||
print(Fore.MAGENTA + f"\n图片数量: {len(data['images'])}")
|
||||
|
||||
def list_all_tags(self):
|
||||
"""获取所有标签"""
|
||||
data = self.make_api_request('getalltags')
|
||||
if data:
|
||||
self.print_header("所有标签")
|
||||
|
||||
# 按字母顺序排序标签
|
||||
data.sort(key=lambda x: x['name'])
|
||||
|
||||
# 打印标签列表
|
||||
for i, tag in enumerate(data, 1):
|
||||
print(Fore.CYAN + f"{i}. {tag['name']} (ID: {tag['id']})")
|
||||
|
||||
self.print_info(f"总共有 {len(data)} 个标签")
|
||||
|
||||
def list_tag_apps(self, tag_id, page=1, limit=20):
|
||||
"""获取某标签下的APP列表"""
|
||||
data = self.make_api_request('gettagapp', {'id': tag_id, 'page': page, 'limit': limit})
|
||||
if data:
|
||||
self.print_header(f"标签 '{data['tag']['name']}' 下的应用列表")
|
||||
|
||||
if not data['apps']:
|
||||
self.print_info("该标签下暂无应用")
|
||||
return
|
||||
|
||||
# 打印应用列表
|
||||
for app in data['apps']:
|
||||
print(Fore.CYAN + f"ID: {app['id']}")
|
||||
print(Fore.YELLOW + f"名称: {app['name']}")
|
||||
print(Fore.WHITE + f"描述: {app['description'][:100]}{'...' if len(app['description']) > 100 else ''}")
|
||||
print(Fore.WHITE + f"评分: {app.get('avg_rating', '暂无')}")
|
||||
print("-")
|
||||
|
||||
# 打印分页信息
|
||||
self.print_info(f"总数: {data['pagination']['total']} 个应用")
|
||||
|
||||
def list_developer_apps(self, developer_id, page=1, limit=20):
|
||||
"""获取某开发者的APP列表"""
|
||||
data = self.make_api_request('getdeveloperapp', {'id': developer_id, 'page': page, 'limit': limit})
|
||||
if data:
|
||||
self.print_header(f"开发者ID {developer_id} 的应用列表")
|
||||
|
||||
if not data['apps']:
|
||||
self.print_info("该开发者暂无应用")
|
||||
return
|
||||
|
||||
# 打印应用列表
|
||||
for app in data['apps']:
|
||||
print(Fore.CYAN + f"ID: {app['id']}")
|
||||
print(Fore.YELLOW + f"名称: {app['name']}")
|
||||
print(Fore.WHITE + f"描述: {app['description'][:100]}{'...' if len(app['description']) > 100 else ''}")
|
||||
print(Fore.WHITE + f"评分: {app.get('avg_rating', '暂无')}")
|
||||
print(Fore.WHITE + f"下载量: {app.get('total_downloads', 0)}")
|
||||
print("-")
|
||||
|
||||
# 打印分页信息
|
||||
self.print_info(f"总数: {data['pagination']['total']} 个应用")
|
||||
|
||||
def get_developer_info(self, developer_id):
|
||||
"""获取开发者信息"""
|
||||
data = self.make_api_request('getdeveloperinfo', {'id': developer_id})
|
||||
if data:
|
||||
self.print_header(f"开发者信息")
|
||||
|
||||
print(Fore.CYAN + f"ID: {data['id']}")
|
||||
print(Fore.YELLOW + f"用户名: {data['username']}")
|
||||
print(Fore.WHITE + f"邮箱: {data['email']}")
|
||||
print(Fore.WHITE + f"注册时间: {data['created_at']}")
|
||||
print(Fore.WHITE + f"是否验证: {'是' if data['is_verified'] else '否'}")
|
||||
if data['is_verified'] and data['verified_at']:
|
||||
print(Fore.WHITE + f"验证时间: {data['verified_at']}")
|
||||
print(Fore.GREEN + f"应用数量: {data['app_count']}")
|
||||
|
||||
def list_all_announcements(self, page=1, limit=20):
|
||||
"""获取所有公告"""
|
||||
data = self.make_api_request('getacc', {'page': page, 'limit': limit})
|
||||
if data:
|
||||
self.print_header(f"公告列表 (第 {page} 页,共 {data['pagination']['totalPages']} 页)")
|
||||
|
||||
if not data['announcements']:
|
||||
self.print_info("暂无公告")
|
||||
return
|
||||
|
||||
# 打印公告列表
|
||||
for announcement in data['announcements']:
|
||||
print(Fore.CYAN + f"ID: {announcement['id']}")
|
||||
print(Fore.YELLOW + f"标题: {announcement['title']}")
|
||||
print(Fore.WHITE + f"内容: {announcement['content'][:100]}{'...' if len(announcement['content']) > 100 else ''}")
|
||||
print(Fore.WHITE + f"发布时间: {announcement['created_at']}")
|
||||
print(Fore.WHITE + f"管理员ID: {announcement['admin_id']}")
|
||||
print("-")
|
||||
|
||||
# 打印分页信息
|
||||
self.print_info(f"总数: {data['pagination']['total']} 条公告")
|
||||
|
||||
def get_count_info(self):
|
||||
"""获取计数信息"""
|
||||
data = self.make_api_request('getcount')
|
||||
if data:
|
||||
self.print_header("应用商店统计信息")
|
||||
|
||||
print(Fore.GREEN + f"应用总数: {data['total_apps']}")
|
||||
print(Fore.GREEN + f"开发者总数: {data['total_developers']}")
|
||||
print(Fore.GREEN + f"标签总数: {data['total_tags']}")
|
||||
print(Fore.GREEN + f"公告总数: {data['total_announcements']}")
|
||||
print(Fore.GREEN + f"总下载量: {data['total_downloads']}")
|
||||
|
||||
def interactive_mode(self):
|
||||
"""交互式模式"""
|
||||
self.print_header("LeonApp CLI 交互式模式")
|
||||
self.print_info("输入命令或 'help' 获取帮助,'exit' 退出")
|
||||
|
||||
while True:
|
||||
try:
|
||||
command = input(Fore.YELLOW + "\n> " + Fore.WHITE).strip().lower()
|
||||
|
||||
if not command:
|
||||
continue
|
||||
|
||||
if command == 'exit' or command == 'quit':
|
||||
self.print_success("感谢使用,再见!")
|
||||
break
|
||||
|
||||
elif command == 'help':
|
||||
self.show_help()
|
||||
|
||||
elif command == 'list apps':
|
||||
try:
|
||||
page = int(input("页码 (默认1): ") or "1")
|
||||
limit = int(input("每页数量 (默认20): ") or "20")
|
||||
self.list_all_apps(page, limit)
|
||||
except ValueError:
|
||||
self.print_error("请输入有效的数字")
|
||||
|
||||
elif command.startswith('app '):
|
||||
try:
|
||||
app_id = command.split()[1]
|
||||
self.get_app_info(app_id)
|
||||
except IndexError:
|
||||
self.print_error("请提供应用ID")
|
||||
|
||||
elif command == 'list tags':
|
||||
self.list_all_tags()
|
||||
|
||||
elif command.startswith('tag apps '):
|
||||
try:
|
||||
tag_id = command.split()[2]
|
||||
page = int(input("页码 (默认1): ") or "1")
|
||||
limit = int(input("每页数量 (默认20): ") or "20")
|
||||
self.list_tag_apps(tag_id, page, limit)
|
||||
except (IndexError, ValueError):
|
||||
self.print_error("请提供有效的标签ID")
|
||||
|
||||
elif command.startswith('developer apps '):
|
||||
try:
|
||||
developer_id = command.split()[2]
|
||||
page = int(input("页码 (默认1): ") or "1")
|
||||
limit = int(input("每页数量 (默认20): ") or "20")
|
||||
self.list_developer_apps(developer_id, page, limit)
|
||||
except (IndexError, ValueError):
|
||||
self.print_error("请提供有效的开发者ID")
|
||||
|
||||
elif command.startswith('developer info '):
|
||||
try:
|
||||
developer_id = command.split()[2]
|
||||
self.get_developer_info(developer_id)
|
||||
except IndexError:
|
||||
self.print_error("请提供开发者ID")
|
||||
|
||||
elif command == 'list announcements':
|
||||
try:
|
||||
page = int(input("页码 (默认1): ") or "1")
|
||||
limit = int(input("每页数量 (默认20): ") or "20")
|
||||
self.list_all_announcements(page, limit)
|
||||
except ValueError:
|
||||
self.print_error("请输入有效的数字")
|
||||
|
||||
elif command == 'stats':
|
||||
self.get_count_info()
|
||||
|
||||
else:
|
||||
self.print_error("未知命令,请输入 'help' 获取帮助")
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print()
|
||||
self.print_info("按 'exit' 退出")
|
||||
except Exception as e:
|
||||
self.print_error(f"发生错误: {str(e)}")
|
||||
|
||||
def show_help(self):
|
||||
"""显示帮助信息"""
|
||||
self.print_header("命令帮助")
|
||||
print(Fore.CYAN + "exit/quit" + Fore.WHITE + " - 退出程序")
|
||||
print(Fore.CYAN + "help" + Fore.WHITE + " - 显示此帮助信息")
|
||||
print(Fore.CYAN + "list apps" + Fore.WHITE + " - 列出所有应用")
|
||||
print(Fore.CYAN + "app [id]" + Fore.WHITE + " - 查看应用详情")
|
||||
print(Fore.CYAN + "list tags" + Fore.WHITE + " - 列出所有标签")
|
||||
print(Fore.CYAN + "tag apps [id]" + Fore.WHITE + " - 查看标签下的应用")
|
||||
print(Fore.CYAN + "developer apps [id]" + Fore.WHITE + " - 查看开发者的应用")
|
||||
print(Fore.CYAN + "developer info [id]" + Fore.WHITE + " - 查看开发者信息")
|
||||
print(Fore.CYAN + "list announcements" + Fore.WHITE + " - 列出所有公告")
|
||||
print(Fore.CYAN + "stats" + Fore.WHITE + " - 查看统计信息")
|
||||
|
||||
def parse_arguments(self):
|
||||
"""解析命令行参数"""
|
||||
parser = argparse.ArgumentParser(description='LeonApp CLI - 一个用于访问App Store API的命令行工具')
|
||||
|
||||
# 子命令
|
||||
subparsers = parser.add_subparsers(dest='command', help='可用命令')
|
||||
|
||||
# list-apps 命令
|
||||
list_apps_parser = subparsers.add_parser('list-apps', help='列出所有应用')
|
||||
list_apps_parser.add_argument('--page', type=int, default=1, help='页码 (默认: 1)')
|
||||
list_apps_parser.add_argument('--limit', type=int, default=20, help='每页数量 (默认: 20)')
|
||||
|
||||
# app-info 命令
|
||||
app_info_parser = subparsers.add_parser('app-info', help='查看应用详情')
|
||||
app_info_parser.add_argument('id', help='应用ID')
|
||||
|
||||
# list-tags 命令
|
||||
subparsers.add_parser('list-tags', help='列出所有标签')
|
||||
|
||||
# tag-apps 命令
|
||||
tag_apps_parser = subparsers.add_parser('tag-apps', help='查看标签下的应用')
|
||||
tag_apps_parser.add_argument('id', help='标签ID')
|
||||
tag_apps_parser.add_argument('--page', type=int, default=1, help='页码 (默认: 1)')
|
||||
tag_apps_parser.add_argument('--limit', type=int, default=20, help='每页数量 (默认: 20)')
|
||||
|
||||
# developer-apps 命令
|
||||
developer_apps_parser = subparsers.add_parser('developer-apps', help='查看开发者的应用')
|
||||
developer_apps_parser.add_argument('id', help='开发者ID')
|
||||
developer_apps_parser.add_argument('--page', type=int, default=1, help='页码 (默认: 1)')
|
||||
developer_apps_parser.add_argument('--limit', type=int, default=20, help='每页数量 (默认: 20)')
|
||||
|
||||
# developer-info 命令
|
||||
developer_info_parser = subparsers.add_parser('developer-info', help='查看开发者信息')
|
||||
developer_info_parser.add_argument('id', help='开发者ID')
|
||||
|
||||
# list-announcements 命令
|
||||
list_announcements_parser = subparsers.add_parser('list-announcements', help='列出所有公告')
|
||||
list_announcements_parser.add_argument('--page', type=int, default=1, help='页码 (默认: 1)')
|
||||
list_announcements_parser.add_argument('--limit', type=int, default=20, help='每页数量 (默认: 20)')
|
||||
|
||||
# stats 命令
|
||||
subparsers.add_parser('stats', help='查看统计信息')
|
||||
|
||||
# interactive 命令
|
||||
subparsers.add_parser('interactive', help='进入交互式模式')
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
def run(self):
|
||||
"""运行CLI"""
|
||||
# 解析命令行参数
|
||||
args = self.parse_arguments()
|
||||
|
||||
# 根据命令执行相应操作
|
||||
if args.command == 'list-apps':
|
||||
self.list_all_apps(args.page, args.limit)
|
||||
elif args.command == 'app-info':
|
||||
self.get_app_info(args.id)
|
||||
elif args.command == 'list-tags':
|
||||
self.list_all_tags()
|
||||
elif args.command == 'tag-apps':
|
||||
self.list_tag_apps(args.id, args.page, args.limit)
|
||||
elif args.command == 'developer-apps':
|
||||
self.list_developer_apps(args.id, args.page, args.limit)
|
||||
elif args.command == 'developer-info':
|
||||
self.get_developer_info(args.id)
|
||||
elif args.command == 'list-announcements':
|
||||
self.list_all_announcements(args.page, args.limit)
|
||||
elif args.command == 'stats':
|
||||
self.get_count_info()
|
||||
elif args.command == 'interactive':
|
||||
self.interactive_mode()
|
||||
else:
|
||||
# 如果没有提供命令或者命令未知,显示帮助信息并进入交互式模式
|
||||
self.show_help()
|
||||
self.interactive_mode()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 创建CLI实例并运行
|
||||
cli = LeonAppCLI()
|
||||
try:
|
||||
cli.run()
|
||||
except KeyboardInterrupt:
|
||||
print()
|
||||
cli.print_success("程序已中断,再见!")
|
||||
except Exception as e:
|
||||
cli.print_error(f"程序发生错误: {str(e)}")
|
||||
sys.exit(1)
|
||||
2
leonapp-cli/requirements.txt
Normal file
2
leonapp-cli/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
requests>=2.28.0
|
||||
colorama>=0.4.6
|
||||
Reference in New Issue
Block a user