I’m still testing it. It seems that I can’t search for an entire long link (max 101 characters). So I need to trim it down a bit if it’s longer. Let me know if I’m allowed to post a sample link and if it’s expected behavior
Discourse keeps track of any link inside posts.
For your use case, I would use this data.
As far as I know, there is no API to access these links.
Implementing one via plugin should not be hard.
I’m using this automation, It works very well, as long as the link doesn’t exceed 100 characters. If it does, it returns as non-existent, even though there is a topic with that link.
async def search_discourse_topic(session, link):
headers = {"Api-Key": USER_API_KEY, "Api-Username": USER_ID}
cleaned_link = clean_url(link) # Limpa o URL fornecido para garantir consistência
try:
log(f"Searching for topic with link: {cleaned_link}") # Log quando inicia a pesquisa
async with session.get(f"{DISCOURSE_API_URL}/search.json", headers=headers, params={"q": cleaned_link}) as response:
search_results = await response.json()
topics = search_results.get("topics", [])
if not topics:
log(f"No topics found for link: {cleaned_link}") # Log se não encontrar resultados
for topic in topics:
if cleaned_link in topic.get("blurb", ""): # Checa se o link aparece na descrição do tópico
log(f"Found existing topic with link: {cleaned_link}") # Log se um tópico correspondente for encontrado
return topic["id"]
except Exception as e:
log(f"Error searching for topic with link: {e}")
return None