Search by link discussion revolves around David_Ghost attempting to create a Python script to search for external links within his forum threads. He encounters inconsistent results, with some posts containing the link being found and others not. supermathie suggests searching both raw and cooked content, while David_Ghost confirms he is searching the raw content.
David_Ghost shares a screenshot of his search results and notes that he cannot find a topic by searching for a link, even when the link is the only content in the topic. He later discovers that he cannot search for links exceeding 101 characters David_Ghost.
thoka mentions that Discourse tracks links inside posts, but there is no API to access this data. David_Ghost shares his automation script, which works well for links under 100 characters but returns non-existent results for longer links.
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