btdigg.py 2.6 KB

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