|
| 1 | + |
| 2 | + |
| 3 | +from directus_sdk_py import DirectusClient |
| 4 | +from bs4 import BeautifulSoup |
| 5 | +import os |
| 6 | +import time |
| 7 | +import requests |
| 8 | +from tenacity import retry, stop_after_attempt, wait_exponential |
| 9 | + |
| 10 | +from dotenv import load_dotenv |
| 11 | + |
| 12 | +load_dotenv() |
| 13 | + |
| 14 | +DIRECTUS_TOKEN = os.getenv("DIRECTUS_TOKEN") |
| 15 | + |
| 16 | +# Directus setup (Replace with the correct API URL) |
| 17 | +DIRECTUS_URL = "https://backend.netlogo.org" # Base API URL, not Admin panel URL |
| 18 | + |
| 19 | +# Initialize Directus client |
| 20 | +client = DirectusClient(DIRECTUS_URL, DIRECTUS_TOKEN) |
| 21 | + |
| 22 | +def extract_references(file_path): |
| 23 | + with open(file_path, encoding="utf8") as file: |
| 24 | + soup = BeautifulSoup(file, 'html.parser') |
| 25 | + |
| 26 | + references = [] |
| 27 | + |
| 28 | + for year_header in soup.find_all("h2"): |
| 29 | + year_link = year_header.find("a") |
| 30 | + if year_link and year_link.get("name"): |
| 31 | + year = year_link.get("name").strip() |
| 32 | + |
| 33 | + if year.isdigit(): |
| 34 | + year = int(year) |
| 35 | + else: |
| 36 | + continue |
| 37 | + |
| 38 | + reference_list = year_header.find_next("ul").find_all("li", class_="ccl") |
| 39 | + |
| 40 | + for ref in reference_list: |
| 41 | + references.append({"year": year, "reference": ref.get_text()}) |
| 42 | + |
| 43 | + return references |
| 44 | +#this i added due to a HTTPSConnectionPool(host='backend.netlogo.org', port=443): Read timed out. (read timeout=None) error |
| 45 | +# I Defined a retry decorator to handle transient failures |
| 46 | +# - stop_after_attempt(3): Attempt the function up to 3 times before giving up. |
| 47 | +# - wait_exponential: Wait time increases exponentially between retries. |
| 48 | +# - multiplier=1: Base multiplier for the exponential backoff. |
| 49 | +# - min=4: Minimum wait time in seconds. |
| 50 | +# - max=10: Maximum wait time in seconds. |
| 51 | +@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10)) |
| 52 | +def post_reference_to_directus(ref): |
| 53 | + try: |
| 54 | + # Attempt to post a reference to Directus |
| 55 | + response = client.create_item("References", ref) |
| 56 | + print(f"Posted: {ref} - Response: {response}") |
| 57 | + # Catch specific timeout exceptions to handle network timeouts |
| 58 | + except requests.exceptions.Timeout: |
| 59 | + print(f"Timeout posting {ref}. Retrying...") |
| 60 | + raise |
| 61 | + except Exception as e: |
| 62 | + print(f"Error posting {ref}: {e}") |
| 63 | + raise |
| 64 | + |
| 65 | +def post_references_to_directus(references): |
| 66 | + for ref in references: |
| 67 | + try: |
| 68 | + post_reference_to_directus(ref) |
| 69 | + except Exception as e: |
| 70 | + print(f"Failed to post {ref} after retries: {e}") |
| 71 | + |
| 72 | +# Run the function and post to Directus |
| 73 | +file_path = "src/utils/references.shtml" |
| 74 | +references = extract_references(file_path) |
| 75 | +post_references_to_directus(references) |
| 76 | + |
0 commit comments