btdigg.py 2.5 KB

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