|
| 1 | +import os |
| 2 | +import yaml |
| 3 | +import requests |
| 4 | +from datetime import datetime |
| 5 | +from tabulate import tabulate |
| 6 | + |
| 7 | +def check_url(base_url, path): |
| 8 | + url = f"{base_url}{path}" |
| 9 | + try: |
| 10 | + resp = requests.get(url, timeout=10, allow_redirects=True) |
| 11 | + return resp.status_code < 400, resp.status_code |
| 12 | + except requests.RequestException: |
| 13 | + return False, 0 |
| 14 | + |
| 15 | +def main(): |
| 16 | + with open('resources/links-on-ui.yaml', 'r') as f: |
| 17 | + config = yaml.safe_load(f) |
| 18 | + |
| 19 | + base_url = os.getenv('DOCS_BASE_URL', 'https://docs.daocloud.io') |
| 20 | + results = [] |
| 21 | + |
| 22 | + for url in config.get('compatibility_links', []): |
| 23 | + cn_valid, cn_code = check_url(base_url, url) |
| 24 | + en_valid, en_code = check_url(f"{base_url}/en", url) |
| 25 | + |
| 26 | + cn_url = f"{base_url}{url}" |
| 27 | + en_url = f"{base_url}/en{url}" |
| 28 | + |
| 29 | + cn_status = '✅' if cn_valid else f'❌ ({cn_code})' |
| 30 | + en_status = '✅' if en_valid else f'❌ ({en_code})' |
| 31 | + |
| 32 | + # If both are wrong, use cn_url; if only English, use en_url |
| 33 | + if not cn_valid and not en_valid: |
| 34 | + formatted_url = f"[{url}]({cn_url})" |
| 35 | + elif not en_valid: |
| 36 | + formatted_url = f"[{url}]({en_url})" |
| 37 | + else: |
| 38 | + formatted_url = f"[{url}]({cn_url})" |
| 39 | + |
| 40 | + print(formatted_url, cn_status, en_status) |
| 41 | + results.append([formatted_url, cn_status, en_status]) |
| 42 | + |
| 43 | + current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') |
| 44 | + report = [ |
| 45 | + f"# Compatibility Links Check Report\n", |
| 46 | + f"Check Time: {current_time}\n", |
| 47 | + "## Results\n\n", |
| 48 | + tabulate( |
| 49 | + results, |
| 50 | + headers=['URL', 'Chinese', 'English'], |
| 51 | + tablefmt='github' |
| 52 | + ) |
| 53 | + ] |
| 54 | + |
| 55 | + print('\n'.join(report)) |
| 56 | + |
| 57 | +if __name__ == '__main__': |
| 58 | + main() |
0 commit comments