Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| OpenAlex API 简单测试 - 修复版本 | |
| """ | |
| import requests | |
| import json | |
| def test_openalex_basic(): | |
| """测试OpenAlex API基本功能""" | |
| url = "https://api.openalex.org/works" | |
| # 使用正确的User-Agent格式 | |
| headers = { | |
| 'User-Agent': 'AcademicReviewerSystem/1.0 (mailto:[email protected])' | |
| } | |
| # 最简单的查询 | |
| params = { | |
| "search": "machine learning", | |
| "per-page": 3 | |
| } | |
| print("OpenAlex API 基本测试") | |
| print("=" * 50) | |
| print(f"URL: {url}") | |
| print(f"参数: {params}") | |
| print("-" * 50) | |
| try: | |
| response = requests.get(url, params=params, headers=headers, timeout=30) | |
| print(f"状态码: {response.status_code}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| items = data.get("results", []) | |
| total_results = data.get("meta", {}).get("count", 0) | |
| print(f"成功! 总命中数: {total_results}") | |
| print(f"返回结果数: {len(items)}") | |
| print() | |
| for i, item in enumerate(items, 1): | |
| title = item.get('title', 'N/A') | |
| cited_by_count = item.get('cited_by_count', 0) | |
| pub_year = item.get('publication_year', 'N/A') | |
| print(f"{i}. {title}") | |
| print(f" 被引用次数: {cited_by_count}") | |
| print(f" 发表年份: {pub_year}") | |
| print(f" OpenAlex ID: {item.get('id', 'N/A')}") | |
| print() | |
| return True | |
| else: | |
| print(f"失败: {response.text}") | |
| return False | |
| except Exception as e: | |
| print(f"异常: {str(e)}") | |
| return False | |
| def test_openalex_with_sort(): | |
| """测试OpenAlex API排序功能""" | |
| url = "https://api.openalex.org/works" | |
| headers = { | |
| 'User-Agent': 'AcademicReviewerSystem/1.0 (mailto:[email protected])' | |
| } | |
| # 测试按引用量排序 | |
| params = { | |
| "search": "machine learning", | |
| "per-page": 5, | |
| "sort": "cited_by_count:desc" | |
| } | |
| print(f"\n=== 测试按引用量排序 ===") | |
| print(f"参数: {params}") | |
| print("-" * 50) | |
| try: | |
| response = requests.get(url, params=params, headers=headers, timeout=30) | |
| print(f"状态码: {response.status_code}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| items = data.get("results", []) | |
| print(f"成功! 返回 {len(items)} 个结果") | |
| for i, item in enumerate(items, 1): | |
| title = item.get('title', 'N/A') | |
| if len(title) > 60: | |
| title = title[:60] + "..." | |
| cited_by_count = item.get('cited_by_count', 0) | |
| pub_year = item.get('publication_year', 'N/A') | |
| print(f"{i}. {title}") | |
| print(f" 被引用次数: {cited_by_count}") | |
| print(f" 发表年份: {pub_year}") | |
| print() | |
| # 显示引用量统计 | |
| citations = [item.get('cited_by_count', 0) for item in items] | |
| print(f"引用量统计: {citations}") | |
| if citations: | |
| print(f"平均引用量: {sum(citations) / len(citations):.2f}") | |
| print(f"最大引用量: {max(citations)}") | |
| return True | |
| else: | |
| print(f"失败: {response.text}") | |
| return False | |
| except Exception as e: | |
| print(f"异常: {str(e)}") | |
| return False | |
| def test_openalex_work_details(): | |
| """测试OpenAlex工作详情查找""" | |
| # 使用一个已知的OpenAlex ID | |
| work_id = "W2755950973" # 一个示例ID | |
| url = f"https://api.openalex.org/works/{work_id}" | |
| headers = { | |
| 'User-Agent': 'AcademicReviewerSystem/1.0 (mailto:[email protected])' | |
| } | |
| print(f"\n=== 测试工作详情查找 ===") | |
| print(f"Work ID: {work_id}") | |
| print("-" * 50) | |
| try: | |
| response = requests.get(url, headers=headers, timeout=30) | |
| print(f"状态码: {response.status_code}") | |
| if response.status_code == 200: | |
| data = response.json() | |
| title = data.get('title', 'N/A') | |
| cited_by_count = data.get('cited_by_count', 0) | |
| citation_count = data.get('citation_count', 0) | |
| print(f"标题: {title}") | |
| print(f"被引用次数: {cited_by_count}") | |
| print(f"引用次数: {citation_count}") | |
| print(f"OpenAlex ID: {data.get('id', 'N/A')}") | |
| return True | |
| else: | |
| print(f"失败: {response.text}") | |
| return False | |
| except Exception as e: | |
| print(f"异常: {str(e)}") | |
| return False | |
| if __name__ == "__main__": | |
| print("OpenAlex API 修复测试") | |
| print("=" * 60) | |
| # 基本测试 | |
| test_openalex_basic() | |
| # 排序测试 | |
| test_openalex_with_sort() | |
| # 工作详情测试 | |
| test_openalex_work_details() | |
| print("\n" + "=" * 60) | |
| print("测试完成") | |