btdigg.py 2.5 KB

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