|  | @@ -0,0 +1,83 @@
 | 
	
		
			
			|  | 1 | +## Kickass Torrent (Videos, Music, Files)
 | 
	
		
			
			|  | 2 | +# 
 | 
	
		
			
			|  | 3 | +# @website     https://kickass.so
 | 
	
		
			
			|  | 4 | +# @provide-api no (nothing found)
 | 
	
		
			
			|  | 5 | +# 
 | 
	
		
			
			|  | 6 | +# @using-api   no
 | 
	
		
			
			|  | 7 | +# @results     HTML (using search portal)
 | 
	
		
			
			|  | 8 | +# @stable      yes (HTML can change)
 | 
	
		
			
			|  | 9 | +# @parse       url, title, content, seed, leech, magnetlink
 | 
	
		
			
			|  | 10 | +
 | 
	
		
			
			|  | 11 | +from urlparse import urljoin
 | 
	
		
			
			|  | 12 | +from cgi import escape
 | 
	
		
			
			|  | 13 | +from urllib import quote
 | 
	
		
			
			|  | 14 | +from lxml import html
 | 
	
		
			
			|  | 15 | +from operator import itemgetter
 | 
	
		
			
			|  | 16 | +
 | 
	
		
			
			|  | 17 | +# engine dependent config
 | 
	
		
			
			|  | 18 | +categories = ['videos', 'music', 'files']
 | 
	
		
			
			|  | 19 | +paging = True
 | 
	
		
			
			|  | 20 | +
 | 
	
		
			
			|  | 21 | +# search-url
 | 
	
		
			
			|  | 22 | +url = 'https://kickass.so/'
 | 
	
		
			
			|  | 23 | +search_url = url + 'search/{search_term}/{pageno}/'
 | 
	
		
			
			|  | 24 | +
 | 
	
		
			
			|  | 25 | +# specific xpath variables
 | 
	
		
			
			|  | 26 | +magnet_xpath = './/a[@title="Torrent magnet link"]'
 | 
	
		
			
			|  | 27 | +#content_xpath = './/font[@class="detDesc"]//text()'
 | 
	
		
			
			|  | 28 | +
 | 
	
		
			
			|  | 29 | +
 | 
	
		
			
			|  | 30 | +# do search-request
 | 
	
		
			
			|  | 31 | +def request(query, params):
 | 
	
		
			
			|  | 32 | +    params['url'] = search_url.format(search_term=quote(query),
 | 
	
		
			
			|  | 33 | +                                      pageno=params['pageno'])
 | 
	
		
			
			|  | 34 | +
 | 
	
		
			
			|  | 35 | +    return params
 | 
	
		
			
			|  | 36 | +
 | 
	
		
			
			|  | 37 | +
 | 
	
		
			
			|  | 38 | +# get response from search-request
 | 
	
		
			
			|  | 39 | +def response(resp):
 | 
	
		
			
			|  | 40 | +    results = []
 | 
	
		
			
			|  | 41 | +
 | 
	
		
			
			|  | 42 | +    dom = html.fromstring(resp.text)
 | 
	
		
			
			|  | 43 | +
 | 
	
		
			
			|  | 44 | +    search_res = dom.xpath('//table[@class="data"]//tr')
 | 
	
		
			
			|  | 45 | +
 | 
	
		
			
			|  | 46 | +    # return empty array if nothing is found
 | 
	
		
			
			|  | 47 | +    if not search_res:
 | 
	
		
			
			|  | 48 | +        return []
 | 
	
		
			
			|  | 49 | +
 | 
	
		
			
			|  | 50 | +    # parse results
 | 
	
		
			
			|  | 51 | +    for result in search_res[1:]:
 | 
	
		
			
			|  | 52 | +        link = result.xpath('.//a[@class="cellMainLink"]')[0]
 | 
	
		
			
			|  | 53 | +        href = urljoin(url, link.attrib['href'])
 | 
	
		
			
			|  | 54 | +        title = ' '.join(link.xpath('.//text()'))
 | 
	
		
			
			|  | 55 | +        #content = escape(' '.join(result.xpath(content_xpath)))
 | 
	
		
			
			|  | 56 | +        seed = result.xpath('.//td[contains(@class, "green")]/text()')[0]
 | 
	
		
			
			|  | 57 | +        leech = result.xpath('.//td[contains(@class, "red")]/text()')[0]
 | 
	
		
			
			|  | 58 | +
 | 
	
		
			
			|  | 59 | +        # convert seed to int if possible
 | 
	
		
			
			|  | 60 | +        if seed.isdigit():
 | 
	
		
			
			|  | 61 | +            seed = int(seed)
 | 
	
		
			
			|  | 62 | +        else:
 | 
	
		
			
			|  | 63 | +            seed = 0
 | 
	
		
			
			|  | 64 | +
 | 
	
		
			
			|  | 65 | +        # convert leech to int if possible
 | 
	
		
			
			|  | 66 | +        if leech.isdigit():
 | 
	
		
			
			|  | 67 | +            leech = int(leech)
 | 
	
		
			
			|  | 68 | +        else:
 | 
	
		
			
			|  | 69 | +            leech = 0
 | 
	
		
			
			|  | 70 | +
 | 
	
		
			
			|  | 71 | +        magnetlink = result.xpath(magnet_xpath)[0].attrib['href']
 | 
	
		
			
			|  | 72 | +
 | 
	
		
			
			|  | 73 | +        # append result
 | 
	
		
			
			|  | 74 | +        results.append({'url': href,
 | 
	
		
			
			|  | 75 | +                        'title': title,
 | 
	
		
			
			|  | 76 | +                        'content': '',
 | 
	
		
			
			|  | 77 | +                        'seed': seed,
 | 
	
		
			
			|  | 78 | +                        'leech': leech,
 | 
	
		
			
			|  | 79 | +                        'magnetlink': magnetlink,
 | 
	
		
			
			|  | 80 | +                        'template': 'torrent.html'})
 | 
	
		
			
			|  | 81 | +
 | 
	
		
			
			|  | 82 | +    # return results sorted by seeder
 | 
	
		
			
			|  | 83 | +    return sorted(results, key=itemgetter('seed'), reverse=True)
 |