btdigg.py 2.6 KB

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