Posted By

pantuts on 09/20/14


Tagged

search python download torrent asskickpy


Versions (?)

asskick.py


 / Published in: Python
 

URL: http://pantuts.com/2014/09/20/asskick-py-python-script-search-download-torrents-kickass/

Python script to search and download torrents from KickAss torrents.

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author: pantuts
  4. # URL: http://pantuts.com
  5. # Agreement: You can use, modify, or redistribute this tool under the terms of GNU General Public License (GPLv3).
  6. # This tool is for educational purposes only. Any damage you make will not affect the author.
  7. # Dependencies:
  8. # requests: https://pypi.python.org/pypi/requests
  9. # beautifulSoup4: https://pypi.python.org/pypi/beautifulsoup4/4.3.2
  10. # tabulate: https://pypi.python.org/pypi/tabulate
  11.  
  12. from bs4 import BeautifulSoup
  13. import os
  14. import re
  15. import requests
  16. import subprocess
  17. import sys
  18. import tabulate
  19.  
  20.  
  21. class OutColors:
  22. DEFAULT = '\033[0m'
  23. BW = '\033[1m'
  24. LG = '\033[0m\033[32m'
  25. LR = '\033[0m\033[31m'
  26. SEEDER = '\033[1m\033[32m'
  27. LEECHER = '\033[1m\033[31m'
  28.  
  29.  
  30. def helper():
  31. print(OutColors.DEFAULT + "\nSearch torrents from Kickass.to ;)")
  32.  
  33.  
  34. def select_torrent():
  35. torrent = input('>> ')
  36. return torrent
  37.  
  38.  
  39. def download_torrent(url):
  40. fname = os.getcwd() + '/' + url.split('title=')[-1] + '.torrent'
  41. # http://stackoverflow.com/a/14114741/1302018
  42. try:
  43. r = requests.get(url, stream=True)
  44. with open(fname, 'wb') as f:
  45. for chunk in r.iter_content(chunk_size=1024):
  46. if chunk:
  47. f.write(chunk)
  48. f.flush()
  49. except requests.exceptions.RequestException as e:
  50. print('\n' + OutColors.LR + str(e))
  51. sys.exit(1)
  52.  
  53. return fname
  54.  
  55.  
  56. def aksearch():
  57. helper()
  58. tmp_url = 'http://kickass.to/usearch/'
  59.  
  60. query = input('Type query: ')
  61. url = tmp_url + query + '/'
  62.  
  63. try:
  64. cont = requests.get(url)
  65. except requests.exceptions.RequestException as e:
  66. raise SystemExit('\n' + OutColors.LR + str(e))
  67.  
  68. # check if no torrents found
  69. if not re.findall(r'Download torrent file', str(cont.content)):
  70. print('Torrents found: 0')
  71. aksearch()
  72. else:
  73. soup = BeautifulSoup(cont.content)
  74.  
  75. # to use by age, seeders, and leechers
  76. # sample:
  77. # 700.46 MB
  78. # 5
  79. # 2 years
  80. # 1852
  81. # 130
  82. al = [s.get_text() for s in soup.find_all('td', {'class':'center'})]
  83.  
  84. href = [a.get('href') for a in soup.find_all('a', {'title':'Download torrent file'})]
  85. size = [t.get_text() for t in soup.find_all('td', {'class':'nobr'}) ]
  86. title = [ti.get_text() for ti in soup.find_all('a', {'class':'cellMainLink'})]
  87. age = al[2::5]
  88. seeders = al[3::5]
  89. leechers = al[4::5]
  90.  
  91. # for table printing
  92. table = [[OutColors.BW + str(i+1) + OutColors.DEFAULT if (i+1) % 2 == 0 else i+1,
  93. OutColors.BW + title[i] + OutColors.DEFAULT if (i+1) % 2 == 0 else title[i],
  94. OutColors.BW + size[i] + OutColors.DEFAULT if (i+1) % 2 == 0 else size[i],
  95. OutColors.BW + age[i] + OutColors.DEFAULT if (i+1) % 2 == 0 else age[i],
  96. OutColors.SEEDER + seeders[i] + OutColors.DEFAULT if (i+1) % 2 == 0 else OutColors.LG + seeders[i] + OutColors.DEFAULT,
  97. OutColors.LEECHER + leechers[i] + OutColors.DEFAULT if (i+1) % 2 == 0 else OutColors.LR + leechers[i] + OutColors.DEFAULT] for i in range(len(href))]
  98. print()
  99. print(tabulate.tabulate(table, headers=['No', 'Title', 'Size', 'Age', 'Seeders', 'Leechers']))
  100.  
  101. # torrent selection
  102. if len(href) == 1:
  103. torrent = 1
  104. else:
  105. print('\nSelect torrent: [ 1 - ' + str(len(href)) + ' ] or [ M ] to go back to main menu or [ Q ] to quit')
  106. torrent = select_torrent()
  107. if torrent == 'Q' or torrent == 'q':
  108. sys.exit(0)
  109. elif torrent == 'M' or torrent == 'm':
  110. aksearch()
  111. else:
  112. if int(torrent) <= 0 or int(torrent) > len(href):
  113. print('Use eyeglasses...')
  114. else:
  115. print('Download >> ' + href[int(torrent)-1].split('title=')[-1] + '.torrent')
  116. fname = download_torrent(href[int(torrent)-1])
  117. subprocess.Popen(['xdg-open', fname], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  118. aksearch()
  119.  
  120.  
  121. if __name__ == '__main__':
  122. try:
  123. aksearch()
  124. except KeyboardInterrupt:
  125. print('\nHuha!')

Report this snippet  

You need to login to post a comment.