2025-08-21 17:57:58 +02:00
|
|
|
"""Searchcode (IT)"""
|
2024-03-06 08:18:24 +01:00
|
|
|
|
2025-08-21 17:57:58 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import typing as t
|
2014-12-20 07:07:32 +01:00
|
|
|
|
2020-08-06 17:42:46 +02:00
|
|
|
from urllib.parse import urlencode
|
2014-12-22 16:26:45 +01:00
|
|
|
|
2025-08-21 17:57:58 +02:00
|
|
|
from searx.result_types import EngineResults
|
|
|
|
|
from searx.extended_types import SXNG_Response
|
|
|
|
|
|
2021-01-13 11:31:25 +01:00
|
|
|
# about
|
|
|
|
|
about = {
|
2025-08-21 17:57:58 +02:00
|
|
|
"website": "https://searchcode.com/",
|
2021-01-13 11:31:25 +01:00
|
|
|
"wikidata_id": None,
|
2025-08-21 17:57:58 +02:00
|
|
|
"official_api_documentation": "https://searchcode.com/api/",
|
2021-01-13 11:31:25 +01:00
|
|
|
"use_official_api": True,
|
|
|
|
|
"require_api_key": False,
|
2025-08-21 17:57:58 +02:00
|
|
|
"results": "JSON",
|
2021-01-13 11:31:25 +01:00
|
|
|
}
|
2014-12-20 07:07:32 +01:00
|
|
|
|
|
|
|
|
# engine dependent config
|
2025-08-21 17:57:58 +02:00
|
|
|
categories = ["it"]
|
|
|
|
|
search_api = "https://searchcode.com/api/codesearch_I/?"
|
2014-12-20 23:33:03 +01:00
|
|
|
|
2024-03-06 08:18:24 +01:00
|
|
|
# paging is broken in searchcode.com's API .. not sure it will ever been fixed
|
|
|
|
|
# paging = True
|
2014-12-20 07:07:32 +01:00
|
|
|
|
|
|
|
|
|
2025-08-21 17:57:58 +02:00
|
|
|
def request(query: str, params: dict[str, t.Any]) -> None:
|
|
|
|
|
args = {
|
|
|
|
|
"q": query,
|
|
|
|
|
# paging is broken in searchcode.com's API
|
|
|
|
|
# "p": params["pageno"] - 1,
|
|
|
|
|
# "per_page": 10,
|
|
|
|
|
}
|
2014-12-20 07:07:32 +01:00
|
|
|
|
2025-08-21 17:57:58 +02:00
|
|
|
params["url"] = search_api + urlencode(args)
|
|
|
|
|
logger.debug("query_url --> %s", params["url"])
|
2014-12-20 07:07:32 +01:00
|
|
|
|
2014-12-22 16:26:45 +01:00
|
|
|
|
2025-08-21 17:57:58 +02:00
|
|
|
def response(resp: SXNG_Response) -> EngineResults:
|
|
|
|
|
res = EngineResults()
|
2014-12-20 07:07:32 +01:00
|
|
|
|
|
|
|
|
# parse results
|
2025-08-21 17:57:58 +02:00
|
|
|
for result in resp.json().get("results", []):
|
2024-03-06 08:18:24 +01:00
|
|
|
lines = {}
|
2025-08-21 17:57:58 +02:00
|
|
|
for line, code in result["lines"].items():
|
2014-12-20 07:07:32 +01:00
|
|
|
lines[int(line)] = code
|
|
|
|
|
|
2025-08-21 17:57:58 +02:00
|
|
|
res.add(
|
|
|
|
|
res.types.Code(
|
|
|
|
|
url=result["url"],
|
|
|
|
|
title=f'{result["name"]} - {result["filename"]}',
|
|
|
|
|
repository=result["repo"],
|
|
|
|
|
filename=result["filename"],
|
|
|
|
|
codelines=sorted(lines.items()),
|
|
|
|
|
strip_whitespace=True,
|
|
|
|
|
)
|
2021-12-27 09:26:22 +01:00
|
|
|
)
|
2014-12-20 07:07:32 +01:00
|
|
|
|
2025-08-21 17:57:58 +02:00
|
|
|
return res
|