freesound.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Freesound (Sound)
  4. """
  5. from json import loads
  6. from urllib.parse import urlencode
  7. from datetime import datetime
  8. disabled = True
  9. api_key = ""
  10. # about
  11. about = {
  12. "website": "https://freesound.org",
  13. "wikidata_id": "Q835703",
  14. "official_api_documentation": "https://freesound.org/docs/api",
  15. "use_official_api": True,
  16. "require_api_key": True,
  17. "results": "JSON",
  18. }
  19. # engine dependent config
  20. paging = True
  21. # search url
  22. url = "https://freesound.org/apiv2/"
  23. search_url = (
  24. url + "search/text/?query={query}&page={page}&fields=name,url,download,created,description,type&token={api_key}"
  25. )
  26. # search request
  27. def request(query, params):
  28. params["url"] = search_url.format(
  29. query=urlencode({"q": query}),
  30. page=params["pageno"],
  31. api_key=api_key,
  32. )
  33. return params
  34. # get response from search request
  35. def response(resp):
  36. results = []
  37. search_res = loads(resp.text)
  38. # parse results
  39. for result in search_res.get("results", []):
  40. title = result["name"]
  41. content = result["description"][:128]
  42. publishedDate = datetime.fromisoformat(result["created"])
  43. uri = result["download"]
  44. # append result
  45. results.append(
  46. {
  47. "url": result["url"],
  48. "title": title,
  49. "publishedDate": publishedDate,
  50. "audio_src": uri,
  51. "content": content,
  52. }
  53. )
  54. return results