260 lines
7.7 KiB
Python
260 lines
7.7 KiB
Python
"""
|
|
CookieCloud客户端使用示例
|
|
演示如何使用cookiecloud_client模块
|
|
"""
|
|
import sys
|
|
import os
|
|
|
|
# 添加父目录到路径
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
# 示例1: 基本使用
|
|
def example_basic_usage():
|
|
"""基本使用示例"""
|
|
from cookiecloud_client import CookieCloudClient, CookieConfig
|
|
|
|
print("=" * 60)
|
|
print("示例1: 基本使用")
|
|
print("=" * 60)
|
|
|
|
# 创建配置
|
|
config = CookieConfig(
|
|
server="https://movie-pilot.org/cookiecloud",
|
|
username="hu6n2vcUmzpu7mqUN2rVCg",
|
|
password="jtDM5dV9AyqXkZdQVeA9f6"
|
|
)
|
|
|
|
# 创建客户端
|
|
client = CookieCloudClient(config)
|
|
|
|
# 测试连接
|
|
success, message = client.test_connection()
|
|
print(f"连接测试: {message}")
|
|
|
|
if success:
|
|
# 下载所有cookies
|
|
result = client.download()
|
|
|
|
if result.success:
|
|
print(f"\n✓ 下载成功!")
|
|
print(f" 总域名数: {result.total_domains}")
|
|
print(f" 总Cookie数: {result.total_cookies}")
|
|
print(f" 下载耗时: {result.download_time:.2f}秒")
|
|
|
|
# 显示前5个域名
|
|
domains = result.get_domains()[:5]
|
|
print(f"\n前5个域名:")
|
|
for idx, domain in enumerate(domains, 1):
|
|
cookie_str = result.get_cookie_string(domain)
|
|
preview = cookie_str[:50] + "..." if len(cookie_str) > 50 else cookie_str
|
|
print(f" {idx}. {domain}: {preview}")
|
|
else:
|
|
print(f"\n✗ 下载失败: {result.error_message}")
|
|
|
|
|
|
# 示例2: 下载指定域名的Cookie
|
|
def example_download_specific_domain():
|
|
"""下载指定域名示例"""
|
|
from cookiecloud_client import CookieCloudClient, CookieConfig
|
|
|
|
print("\n" + "=" * 60)
|
|
print("示例2: 下载指定域名的Cookie")
|
|
print("=" * 60)
|
|
|
|
config = CookieConfig(
|
|
server="https://movie-pilot.org/cookiecloud",
|
|
username="hu6n2vcUmzpu7mqUN2rVCg",
|
|
password="jtDM5dV9AyqXkZdQVeA9f6"
|
|
)
|
|
|
|
client = CookieCloudClient(config)
|
|
|
|
# 下载单个域名
|
|
domain = "baidu.com"
|
|
cookie_str = client.download_for_domain(domain)
|
|
|
|
if cookie_str:
|
|
print(f"✓ {domain}的Cookie:")
|
|
print(f" {cookie_str[:100]}...")
|
|
else:
|
|
print(f"✗ 未找到{domain}的Cookie")
|
|
|
|
|
|
# 示例3: 批量下载多个域名
|
|
def example_download_multiple_domains():
|
|
"""批量下载示例"""
|
|
from cookiecloud_client import CookieCloudClient, CookieConfig
|
|
|
|
print("\n" + "=" * 60)
|
|
print("示例3: 批量下载多个域名")
|
|
print("=" * 60)
|
|
|
|
config = CookieConfig(
|
|
server="https://movie-pilot.org/cookiecloud",
|
|
username="hu6n2vcUmzpu7mqUN2rVCg",
|
|
password="jtDM5dV9AyqXkZdQVeA9f6"
|
|
)
|
|
|
|
client = CookieCloudClient(config)
|
|
|
|
# 批量下载
|
|
domains = ["baidu.com", "github.com", "google.com", "bing.com"]
|
|
cookies = client.download_for_domains(domains)
|
|
|
|
print("批量下载结果:")
|
|
for domain, cookie_str in cookies.items():
|
|
if cookie_str:
|
|
preview = cookie_str[:50] + "..."
|
|
print(f" ✓ {domain}: {preview}")
|
|
else:
|
|
print(f" ✗ {domain}: 未找到Cookie")
|
|
|
|
|
|
# 示例4: 异常处理
|
|
def example_error_handling():
|
|
"""异常处理示例"""
|
|
from cookiecloud_client import (
|
|
CookieCloudClient,
|
|
CookieConfig,
|
|
CookieCloudError,
|
|
ConnectionError,
|
|
AuthenticationError,
|
|
NetworkError
|
|
)
|
|
|
|
print("\n" + "=" * 60)
|
|
print("示例4: 异常处理")
|
|
print("=" * 60)
|
|
|
|
try:
|
|
# 使用错误的凭据
|
|
config = CookieConfig(
|
|
server="https://movie-pilot.org/cookiecloud",
|
|
username="wrong_user",
|
|
password="wrong_pass"
|
|
)
|
|
|
|
client = CookieCloudClient(config)
|
|
result = client.download()
|
|
|
|
except AuthenticationError as e:
|
|
print(f"✗ 认证失败: {e.message}")
|
|
print(f" 用户名: {e.details.get('username')}")
|
|
|
|
except ConnectionError as e:
|
|
print(f"✗ 连接失败: {e.message}")
|
|
print(f" 服务器: {e.details.get('server')}")
|
|
print(f" 状态码: {e.details.get('status_code')}")
|
|
|
|
except NetworkError as e:
|
|
print(f"✗ 网络错误: {e.message}")
|
|
print(f" 原始错误: {e.details.get('original_error')}")
|
|
|
|
except CookieCloudError as e:
|
|
print(f"✗ CookieCloud错误: {e.message}")
|
|
|
|
|
|
# 示例5: 自定义配置
|
|
def example_custom_config():
|
|
"""自定义配置示例"""
|
|
from cookiecloud_client import CookieCloudClient, CookieConfig
|
|
|
|
print("\n" + "=" * 60)
|
|
print("示例5: 自定义配置")
|
|
print("=" * 60)
|
|
|
|
# 自定义配置
|
|
config = CookieConfig(
|
|
server="https://movie-pilot.org/cookiecloud",
|
|
username="hu6n2vcUmzpu7mqUN2rVCg",
|
|
password="jtDM5dV9AyqXkZdQVeA9f6",
|
|
timeout=60, # 60秒超时
|
|
verify_ssl=False, # 不验证SSL证书
|
|
ignore_cookies=[ # 忽略的cookie
|
|
"CookieAutoDeleteBrowsingDataCleanup",
|
|
"CookieAutoDeleteCleaningDiscarded",
|
|
"_ga", # 忽略Google Analytics
|
|
]
|
|
)
|
|
|
|
client = CookieCloudClient(config)
|
|
|
|
print(f"配置信息:")
|
|
print(f" 服务器: {config.server}")
|
|
print(f" 用户名: {config.username}")
|
|
print(f" 超时时间: {config.timeout}秒")
|
|
print(f" 验证SSL: {config.verify_ssl}")
|
|
print(f" 忽略Cookie: {len(config.ignore_cookies)}个")
|
|
|
|
result = client.download()
|
|
|
|
if result.success:
|
|
print(f"\n✓ 下载成功")
|
|
print(f" 总域名数: {result.total_domains}")
|
|
print(f" 总Cookie数: {result.total_cookies}")
|
|
|
|
|
|
# 示例6: 使用Cookie数据对象
|
|
def example_cookie_data_objects():
|
|
"""使用Cookie数据对象示例"""
|
|
from cookiecloud_client import CookieCloudClient, CookieConfig
|
|
|
|
print("\n" + "=" * 60)
|
|
print("示例6: 使用Cookie数据对象")
|
|
print("=" * 60)
|
|
|
|
config = CookieConfig(
|
|
server="https://movie-pilot.org/cookiecloud",
|
|
username="hu6n2vcUmzpu7mqUN2rVCg",
|
|
password="jtDM5dV9AyqXkZdQVeA9f6"
|
|
)
|
|
|
|
client = CookieCloudClient(config)
|
|
result = client.download()
|
|
|
|
if result.success:
|
|
# 获取第一个域名的详细信息
|
|
first_domain = result.get_domains()[0]
|
|
collection = result.cookies.get(first_domain)
|
|
|
|
if collection:
|
|
print(f"域名: {collection.domain}")
|
|
print(f"Cookie数量: {len(collection.cookies)}")
|
|
print(f"\nCookie详情:")
|
|
|
|
for idx, cookie in enumerate(collection.cookies[:3], 1):
|
|
print(f" {idx}. {cookie.name}")
|
|
print(f" 值: {cookie.value[:30]}...")
|
|
print(f" 路径: {cookie.path}")
|
|
print(f" 安全: {cookie.secure}")
|
|
print(f" HttpOnly: {cookie.http_only}")
|
|
print()
|
|
|
|
|
|
# 主函数
|
|
def main():
|
|
"""运行所有示例"""
|
|
print("\n")
|
|
print("╔" + "=" * 58 + "╗")
|
|
print("║" + " " * 15 + "CookieCloud客户端使用示例" + " " * 17 + "║")
|
|
print("╚" + "=" * 58 + "╝")
|
|
|
|
try:
|
|
example_basic_usage()
|
|
example_download_specific_domain()
|
|
example_download_multiple_domains()
|
|
example_error_handling()
|
|
example_custom_config()
|
|
example_cookie_data_objects()
|
|
|
|
print("\n" + "=" * 60)
|
|
print("所有示例执行完成!")
|
|
print("=" * 60)
|
|
|
|
except Exception as e:
|
|
print(f"\n示例执行出错: {str(e)}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|