solidtorrents.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """SolidTorrents
  3. """
  4. from datetime import datetime
  5. from urllib.parse import urlencode
  6. import random
  7. from lxml import html
  8. from searx.utils import (
  9. extract_text,
  10. eval_xpath,
  11. eval_xpath_getindex,
  12. eval_xpath_list,
  13. )
  14. about = {
  15. "website": 'https://www.solidtorrents.to/',
  16. "wikidata_id": None,
  17. "official_api_documentation": None,
  18. "use_official_api": False,
  19. "require_api_key": False,
  20. "results": 'HTML',
  21. }
  22. categories = ['files']
  23. paging = True
  24. # base_url can be overwritten by a list of URLs in the settings.yml
  25. base_url = 'https://solidtorrents.to'
  26. def request(query, params):
  27. if isinstance(base_url, list):
  28. params['base_url'] = random.choice(base_url)
  29. else:
  30. params['base_url'] = base_url
  31. search_url = params['base_url'] + '/search?{query}'
  32. query = urlencode({'q': query, 'page': params['pageno']})
  33. params['url'] = search_url.format(query=query)
  34. return params
  35. def response(resp):
  36. results = []
  37. dom = html.fromstring(resp.text)
  38. for result in eval_xpath(dom, '//li[contains(@class, "search-result")]'):
  39. torrentfile = eval_xpath_getindex(result, './/a[contains(@class, "dl-torrent")]/@href', 0, None)
  40. magnet = eval_xpath_getindex(result, './/a[contains(@class, "dl-magnet")]/@href', 0, None)
  41. if torrentfile is None or magnet is None:
  42. continue # ignore anime results that which aren't actually torrents
  43. title = eval_xpath_getindex(result, './/h5[contains(@class, "title")]', 0, None)
  44. url = eval_xpath_getindex(result, './/h5[contains(@class, "title")]/a/@href', 0, None)
  45. categ = eval_xpath(result, './/a[contains(@class, "category")]')
  46. stats = eval_xpath_list(result, './/div[contains(@class, "stats")]/div', min_len=5)
  47. params = {
  48. 'seed': extract_text(stats[3]),
  49. 'leech': extract_text(stats[2]),
  50. 'title': extract_text(title),
  51. 'url': resp.search_params['base_url'] + url,
  52. 'filesize': extract_text(stats[1]),
  53. 'magnetlink': magnet,
  54. 'torrentfile': torrentfile,
  55. 'metadata': extract_text(categ),
  56. 'template': "torrent.html",
  57. }
  58. try:
  59. params['publishedDate'] = datetime.strptime(extract_text(stats[4]), '%b %d, %Y')
  60. except ValueError:
  61. pass
  62. results.append(params)
  63. return results