tokyotoshokan.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. """
  2. Tokyo Toshokan (A BitTorrent Library for Japanese Media)
  3. @website https://www.tokyotosho.info/
  4. @provide-api no
  5. @using-api no
  6. @results HTML
  7. @stable no (HTML can change)
  8. @parse url, title, publishedDate, seed, leech,
  9. filesize, magnetlink, content
  10. """
  11. import re
  12. from lxml import html
  13. from searx.engines.xpath import extract_text
  14. from datetime import datetime
  15. from searx.url_utils import urlencode
  16. from searx.utils import get_torrent_size
  17. # engine dependent config
  18. categories = ['files', 'videos', 'music']
  19. paging = True
  20. # search-url
  21. base_url = 'https://www.tokyotosho.info/'
  22. search_url = base_url + 'search.php?{query}'
  23. # convert a variable to integer or return 0 if it's not a number
  24. def int_or_zero(num):
  25. if isinstance(num, list):
  26. if len(num) < 1:
  27. return 0
  28. num = num[0]
  29. if num.isdigit():
  30. return int(num)
  31. return 0
  32. # do search-request
  33. def request(query, params):
  34. query = urlencode({'page': params['pageno'], 'terms': query})
  35. params['url'] = search_url.format(query=query)
  36. return params
  37. # get response from search-request
  38. def response(resp):
  39. results = []
  40. dom = html.fromstring(resp.text)
  41. rows = dom.xpath('//table[@class="listing"]//tr[contains(@class, "category_0")]')
  42. # check if there are no results or page layout was changed so we cannot parse it
  43. # currently there are two rows for each result, so total count must be even
  44. if len(rows) == 0 or len(rows) % 2 != 0:
  45. return []
  46. # regular expression for parsing torrent size strings
  47. size_re = re.compile(r'Size:\s*([\d.]+)(TB|GB|MB|B)', re.IGNORECASE)
  48. # processing the results, two rows at a time
  49. for i in range(0, len(rows), 2):
  50. # parse the first row
  51. name_row = rows[i]
  52. links = name_row.xpath('./td[@class="desc-top"]/a')
  53. params = {
  54. 'template': 'torrent.html',
  55. 'url': links[-1].attrib.get('href'),
  56. 'title': extract_text(links[-1])
  57. }
  58. # I have not yet seen any torrents without magnet links, but
  59. # it's better to be prepared to stumble upon one some day
  60. if len(links) == 2:
  61. magnet = links[0].attrib.get('href')
  62. if magnet.startswith('magnet'):
  63. # okay, we have a valid magnet link, let's add it to the result
  64. params['magnetlink'] = magnet
  65. # no more info in the first row, start parsing the second one
  66. info_row = rows[i + 1]
  67. desc = extract_text(info_row.xpath('./td[@class="desc-bot"]')[0])
  68. for item in desc.split('|'):
  69. item = item.strip()
  70. if item.startswith('Size:'):
  71. try:
  72. # ('1.228', 'GB')
  73. groups = size_re.match(item).groups()
  74. params['filesize'] = get_torrent_size(groups[0], groups[1])
  75. except:
  76. pass
  77. elif item.startswith('Date:'):
  78. try:
  79. # Date: 2016-02-21 21:44 UTC
  80. date = datetime.strptime(item, 'Date: %Y-%m-%d %H:%M UTC')
  81. params['publishedDate'] = date
  82. except:
  83. pass
  84. elif item.startswith('Comment:'):
  85. params['content'] = item
  86. stats = info_row.xpath('./td[@class="stats"]/span')
  87. # has the layout not changed yet?
  88. if len(stats) == 3:
  89. params['seed'] = int_or_zero(extract_text(stats[0]))
  90. params['leech'] = int_or_zero(extract_text(stats[1]))
  91. results.append(params)
  92. return results