btdigg.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. BTDigg (Videos, Music, Files)
  4. """
  5. from urllib.parse import quote, urljoin
  6. from lxml import html
  7. from searx.utils import extract_text, get_torrent_size
  8. # about
  9. about = {
  10. "website": 'https://btdig.com',
  11. "wikidata_id": 'Q4836698',
  12. "official_api_documentation": {'url': 'https://btdig.com/contacts', 'comment': 'on demand'},
  13. "use_official_api": False,
  14. "require_api_key": False,
  15. "results": 'HTML',
  16. }
  17. # engine dependent config
  18. categories = ['files']
  19. paging = True
  20. # search-url
  21. url = 'https://btdig.com'
  22. search_url = url + '/search?q={search_term}&p={pageno}'
  23. # do search-request
  24. def request(query, params):
  25. params['url'] = search_url.format(search_term=quote(query), pageno=params['pageno'] - 1)
  26. return params
  27. # get response from search-request
  28. def response(resp):
  29. results = []
  30. dom = html.fromstring(resp.text)
  31. search_res = dom.xpath('//div[@class="one_result"]')
  32. # return empty array if nothing is found
  33. if not search_res:
  34. return []
  35. # parse results
  36. for result in search_res:
  37. link = result.xpath('.//div[@class="torrent_name"]//a')[0]
  38. href = urljoin(url, link.attrib.get('href'))
  39. title = extract_text(link)
  40. excerpt = result.xpath('.//div[@class="torrent_excerpt"]')[0]
  41. content = html.tostring(excerpt, encoding='unicode', method='text', with_tail=False)
  42. # it is better to emit <br/> instead of |, but html tags are verboten
  43. content = content.strip().replace('\n', ' | ')
  44. content = ' '.join(content.split())
  45. filesize = result.xpath('.//span[@class="torrent_size"]/text()')[0].split()[0]
  46. filesize_multiplier = result.xpath('.//span[@class="torrent_size"]/text()')[0].split()[1]
  47. files = (result.xpath('.//span[@class="torrent_files"]/text()') or ['1'])[0]
  48. # convert filesize to byte if possible
  49. filesize = get_torrent_size(filesize, filesize_multiplier)
  50. # convert files to int if possible
  51. try:
  52. files = int(files)
  53. except: # pylint: disable=bare-except
  54. files = None
  55. magnetlink = result.xpath('.//div[@class="torrent_magnet"]//a')[0].attrib['href']
  56. # append result
  57. results.append(
  58. {
  59. 'url': href,
  60. 'title': title,
  61. 'content': content,
  62. 'filesize': filesize,
  63. 'files': files,
  64. 'magnetlink': magnetlink,
  65. 'template': 'torrent.html',
  66. }
  67. )
  68. # return results sorted by seeder
  69. return results