-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevopsdaysthemes.py
More file actions
384 lines (285 loc) · 9.81 KB
/
devopsdaysthemes.py
File metadata and controls
384 lines (285 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import requests
from bs4 import BeautifulSoup
import csv
import os
import re
import time
from datetime import datetime
import json
from openai import OpenAI
BASE_URL = "https://devopsdays.org"
LEGACY_BASE = "https://legacy.devopsdays.org"
EVENTS_URL = f"{BASE_URL}/events/"
OUTPUT_CSV = "talks_program.csv"
HEADERS = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)"}
COUNTRY_CACHE = {}
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def sort_csv_by_year(output_csv):
if not os.path.isfile(output_csv):
print("CSV ainda não existe, nada para ordenar.")
return
rows = []
with open(output_csv, "r", encoding="utf-8") as csvfile:
reader = csv.reader(csvfile)
header = next(reader)
for row in reader:
rows.append(row)
rows.sort(key=lambda r: int(r[0]))
with open(output_csv, "w", encoding="utf-8", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(header)
writer.writerows(rows)
print("CSV ordenado por ano com sucesso!")
def build_link(base, link):
if not link:
return ""
url = base + link if link.startswith("/") else link
return url
def fetch(url):
try:
resp = requests.get(url, headers=HEADERS, timeout=10)
if resp.status_code == 200:
return resp.text
return None
except:
return None
def split_author_title(text):
if " - " in text:
return text.split(" - ", 1)
if ", " in text:
return text.split(", ", 1)
if " – " in text:
return text.split(" – ", 1)
return ("", text)
def extract_year(text: str) -> str:
match = re.search(r"\b(20\d{2}|19\d{2})\b", text)
return match.group(1) if match else text.strip()
def extract_city(event_name: str) -> str:
name = re.sub(r"\s+", " ", event_name).strip()
if " - " in name:
name = name.split(" - ")[0].strip()
if ":" in name:
parts = name.split(":")
name = parts[-1].strip()
name = re.sub(r"\(.*?\)", "", name).strip()
name = re.sub(r"\d+", "", name).strip()
name = re.sub(r"\s+", " ", name)
return name
def get_country(city):
city_key = city.lower().strip()
if city_key in COUNTRY_CACHE:
return COUNTRY_CACHE[city_key]
country = "Unknown"
try:
r = requests.get(
"https://geocoding-api.open-meteo.com/v1/search",
params={
"name": city,
"count": 1,
"language": "en",
"format": "json",
},
timeout=10,
)
if r.status_code != 200:
COUNTRY_CACHE[city_key] = "Unknown"
return "Unknown"
data = r.json()
results = data.get("results", [])
if not results:
COUNTRY_CACHE[city_key] = "Unknown"
return "Unknown"
country = results[0].get("country", "Unknown")
except:
country = "Unknown"
COUNTRY_CACHE[city_key] = country
return country
def parse_legacy_complex(html, year, event_name, program_url):
soup = BeautifulSoup(html, "html.parser")
talks = []
for box in soup.find_all("div", class_="span-6"):
titles = [s.get_text(strip=True) for s in box.find_all("strong")]
if not titles:
continue
author = ""
for a in box.find_all("a"):
href = a.get("href", "")
if "/speakers/" in href:
author = a.get_text(strip=True)
if not author:
continue
for title in titles:
talks.append(
{
"year": year,
"event": event_name,
"author": author,
"title": title,
"link": program_url,
}
)
return talks
def parse_legacy_program(url, year, event_name):
html = fetch(url)
if not html:
return []
soup = BeautifulSoup(html, "html.parser")
if soup.find("div", class_="span-6"):
return parse_legacy_complex(html, year, event_name, url)
talks = []
for a in soup.find_all("a"):
text = a.get_text(strip=True)
if not text:
continue
if "://" not in text and ("," in text or " - " in text):
author, title = split_author_title(text)
talks.append(
{
"year": year,
"event": event_name,
"author": author.strip(),
"title": title.strip(),
"link": url,
}
)
return talks
def parse_modern_program(url, year, event_name):
html = fetch(url)
if not html:
return []
soup = BeautifulSoup(html, "html.parser")
talks = []
for div in soup.find_all("div", class_="program-talk"):
a = div.find("a")
if not a:
continue
text = a.get_text(strip=True)
link = a.get("href")
author, title = split_author_title(text)
talks.append(
{
"year": year,
"event": event_name,
"author": author.strip(),
"title": title.strip(),
"link": BASE_URL + link if link.startswith("/") else link,
}
)
return talks
def iter_events():
print("Buscando eventos em devopsdays.org...")
html = fetch(EVENTS_URL)
if not html:
return []
soup = BeautifulSoup(html, "html.parser")
year = None
for tag in soup.find_all(["h4", "a"]):
if tag.name == "h4" and "events-page-months" in tag.get("class", []):
year = extract_year(tag.get_text(strip=True))
if tag.name == "a" and "events-page-event" in tag.get("class", []):
raw_event = tag.text.strip()
city = extract_city(raw_event)
country = get_country(city)
event_name = f"{city} - {country}"
link = tag.get("href")
full_url = BASE_URL + link
yield {"year": year, "event": event_name, "url": full_url}
time.sleep(0.25)
def extract_container_html(html: str) -> str:
try:
soup = BeautifulSoup(html, "html.parser")
container = soup.find("div", class_="container")
if container:
return str(container)
else:
return html
except:
return html
def extract_talks_with_chatgpt(program_url: str, year: str, event_name: str):
print("Extraindo via ChatGPT (fallback)...")
html = fetch(program_url)
if not html:
return []
cleaned_html = extract_container_html(html)
cleaned_html = cleaned_html[:50000]
prompt = f"""
Receba o HTML abaixo, procure qualquer programação de talks/palestras e retorne APENAS um JSON com:
- "author"
- "title"
- "link" (se existir; pode ser null)
NÃO ESCREVA NADA FORA DO JSON.
HTML PARA EXTRAÇÃO:
{cleaned_html}
"""
resp = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{"role": "system", "content": "Você extrai dados estruturados de HTML e devolve somente JSON."},
{"role": "user", "content": prompt},
],
temperature=0,
max_completion_tokens=1500,
)
content = resp.choices[0].message.content.strip()
try:
data = json.loads(content)
return [
{
"year": year,
"event": event_name,
"author": t.get("author") or "",
"title": t.get("title") or "",
"link": build_link(BASE_URL, t.get("link")),
}
for t in data
]
except Exception as e:
print("Erro ao interpretar JSON recebido do ChatGPT:", e)
print("Conteúdo bruto recebido:\n", content)
return []
def main():
file_exists = os.path.isfile(OUTPUT_CSV)
with open(OUTPUT_CSV, "a", encoding="utf-8", newline="") as csvfile:
writer = csv.writer(csvfile)
if not file_exists:
writer.writerow(["ano", "local", "autor", "titulo", "link"])
for ev in iter_events():
year = ev.get("year")
event_name = ev.get("event")
event_url = ev.get("url")
try:
year_int = int(year)
if year_int > datetime.now().year:
print(f"\nPulando evento futuro: {event_name} ({year})")
continue
except ValueError:
print(f"\nAno inválido para o evento: {event_name} ({year})")
continue
print(f"\nEvento: {event_name} ({year})")
program_url = event_url.rstrip("/") + "/program"
print(f"Testando moderno: {program_url}")
talks = parse_modern_program(program_url, year, event_name)
if not talks:
legacy_url = event_url.replace(BASE_URL, LEGACY_BASE) + "/program"
print(f"Fallback: testando legacy → {legacy_url}")
talks = parse_legacy_program(legacy_url, year, event_name)
if not talks:
legacy_url = event_url.replace(BASE_URL, LEGACY_BASE) + "/program"
print("Nenhum talk encontrado — tentando com ChatGPT…")
talks = extract_talks_with_chatgpt(legacy_url, year, event_name)
if not talks:
print("Nenhum talk encontrado para este evento.\n")
continue
for t in talks:
writer.writerow([
t.get("year"),
t.get("event"),
t.get("author"),
t.get("title"),
t.get("link"),
])
print(f"{len(talks)} talks extraídas.\n")
print("\nConcluído! Arquivo gerado:", OUTPUT_CSV)
sort_csv_by_year(OUTPUT_CSV)
if __name__ == "__main__":
main()