-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
79 lines (64 loc) · 2.94 KB
/
main.py
File metadata and controls
79 lines (64 loc) · 2.94 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
import os
import re
import logging
import requests
import sys # Añadido para manejo de salida limpia
from datetime import datetime, timedelta
from notion_client import Client
# Configuración de Logging más limpia
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
logger = logging.getLogger(__name__)
# NOTA: En GitHub Actions no necesitas load_dotenv(),
# las variables se leen directamente de os.getenv
NOTION_API_KEY = os.getenv("NOTION_API_KEY")
TICKTICK_ACCESS_TOKEN = os.getenv("TICKTICK_ACCESS_TOKEN")
LIBRARY_DB_ID = os.getenv("LIBRARY_DB_ID") # Mejor leerlo de entorno también
# Validación de seguridad: Si faltan llaves, el script se detiene con gracia
if not all([NOTION_API_KEY, TICKTICK_ACCESS_TOKEN, LIBRARY_DB_ID]):
logger.error("Faltan variables de entorno obligatorias. Revisa los Secrets de GitHub.")
sys.exit(1)
notion = Client(auth=NOTION_API_KEY)
TICKTICK_API_URL = "https://api.ticktick.com/open/v1/task"
TICKTICK_HEADERS = {"Authorization": f"Bearer {TICKTICK_ACCESS_TOKEN}", "Content-Type": "application/json"}
def extract_tags(description):
words = re.findall(r'\b\w{5,}\b', description.lower())
return [{"name": w.capitalize()} for w in words[:3]]
def parse_markdown_links(filepath):
if not os.path.exists(filepath):
logger.warning(f"Archivo {filepath} no encontrado.")
return []
parsed_data = []
current_cat, current_sub = "General", "General"
try:
with open(filepath, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line: continue
if line.startswith("# "): current_cat = line[2:].strip()
elif line.startswith("## "): current_sub = line[3:].strip()
elif line.startswith("- ["):
match = re.search(r'- \[(.*?)\]\((.*?)\):\s*(.*)', line)
if match:
parsed_data.append({
"title": match.group(1), "url": match.group(2),
"description": match.group(3), "category": current_cat,
"subcategory": current_sub
})
except Exception as e:
logger.error(f"Error leyendo el archivo: {e}")
return parsed_data
# ... (Funciones create_ticktick_task y add_to_notion se mantienen igual)
if __name__ == "__main__":
FILENAME = "input_links.md"
links = parse_markdown_links(FILENAME)
if not links:
logger.info("No hay enlaces nuevos para procesar. Finalizando...")
sys.exit(0) # Salida exitosa pero sin hacer nada
logger.info(f"Procesando {len(links)} enlaces...")
for i, l in enumerate(links):
try:
tt_id = create_ticktick_task(l, i)
add_to_notion(l, tt_id)
logger.info(f"✅ [{i+1}/{len(links)}] {l['title']}")
except Exception as e:
logger.error(f"❌ Error procesando {l['title']}: {e}")