Skip to content

Commit e9fa75b

Browse files
authored
Merge pull request #8 from NetLogo/Julia's-Branch
Julia's branch
2 parents 0b0092d + 3d62b70 commit e9fa75b

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ pnpm-debug.log*
1616

1717
# environment variables
1818
.env.production
19+
t.env
1920

2021
# macOS-specific files
2122
.DS_Store
23+
24+
# specific files
25+
references.shtml

src/utils/NetlogoWEB.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)