# Search by link

**URL:** https://meta.discourse.org/t/search-by-link/339422
**Category:** Development
**Tags:** search
**Created:** [November 30, 2024, 1:43am UTC](https://meta.discourse.org/t/search-by-link/339422 "2024-11-30T01:43:27Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![David\_Ghost](https://avatars.discourse-cdn.com/v4/letter/d/c37758/32.png) [@David\_Ghost](https://meta.discourse.org/u/David_Ghost)
#### Post date: [November 30, 2024, 1:43am UTC](https://meta.discourse.org/t/search-by-link/339422/1 "2024-11-30T01:43:27Z")

</div>

Hi,

I’m writing a python script that needs to search in my forum and check if a external link exists in the content of my forum threads.

The problem is: i checked manually, it finds some results of posts containing the link and others not. Even though the link is in the post.

Would this be something expected?

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [November 30, 2024, 2:06am UTC](https://meta.discourse.org/t/search-by-link/339422/2 "2024-11-30T02:06:58Z")

</div>

What are you searching? The raw or cooked?

---

<div class="post-metadata">

### Author: ![David\_Ghost](https://avatars.discourse-cdn.com/v4/letter/d/c37758/32.png) [@David\_Ghost](https://meta.discourse.org/u/David_Ghost)
#### Post date: [November 30, 2024, 2:16am UTC](https://meta.discourse.org/t/search-by-link/339422/3 "2024-11-30T02:16:42Z")

</div>

The raw. Ex:

> [@Search by link](https://meta.discourse.org/t/search-by-link/339422):
>
> Hi, I’m writing a python script that needs to search in my forum and check if a external link exists in the content of my forum threads. The problem is: i checked manually, it finds some results of posts containing the link and others not. Even though the link is in the post. Would this be something expected?

And it’s oneboxed in the op thread. Sometimes it’s works with the same domain but different ending, sometimes not.

---

<div class="post-metadata">

### Author: ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)
#### Post date: [November 30, 2024, 2:25am UTC](https://meta.discourse.org/t/search-by-link/339422/4 "2024-11-30T02:25:38Z")

</div>

Without knowing exactly how you’re searching it’s hard to guess what’s going on.

If I were given this task to implement I would probably make a [Data Explorer](https://meta.discourse.org/t/32566?silent=true) query that searched for any posts with the link in raw or cooked.

---

<div class="post-metadata">

### Author: ![David\_Ghost](https://avatars.discourse-cdn.com/v4/letter/d/c37758/32.png) [@David\_Ghost](https://meta.discourse.org/u/David_Ghost)
#### Post date: [November 30, 2024, 2:47am UTC](https://meta.discourse.org/t/search-by-link/339422/5 "2024-11-30T02:47:05Z")

</div>

for now it’s only a normal search, in site side, like this:

 ![A screenshot of a mobile search result page showing a search bar, a list of results, and some text explaining the results. (Captioned by AI)](https://global.discourse-cdn.com/meta/original/4X/f/c/d/fcd11f1fe20c9cc54ce8bd39685cd20749bccea8.jpeg)

In my forum, I just created a topic with 1 link, and I still can’t find the topic by searching for the link.

I haven’t tested it here, since I’m not sure if it’s allowed to post links or not.

---

<div class="post-metadata">

### Author: ![David\_Ghost](https://avatars.discourse-cdn.com/v4/letter/d/c37758/32.png) [@David\_Ghost](https://meta.discourse.org/u/David_Ghost)
#### Post date: [December 7, 2024, 4:16pm UTC](https://meta.discourse.org/t/search-by-link/339422/6 "2024-12-07T16:16:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![thoka](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thoka/32/115652_2.png) [@thoka](https://meta.discourse.org/u/thoka)
#### Post date: [December 7, 2024, 8:08pm UTC](https://meta.discourse.org/t/search-by-link/339422/7 "2024-12-07T20:08:43Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![David\_Ghost](https://avatars.discourse-cdn.com/v4/letter/d/c37758/32.png) [@David\_Ghost](https://meta.discourse.org/u/David_Ghost)
#### Post date: [December 7, 2024, 8:16pm UTC](https://meta.discourse.org/t/search-by-link/339422/8 "2024-12-07T20:16:53Z")

</div>

Thanks for your reply

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.

```python
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
```
