Advertisement
CatmanIX

nyaatorrents.py

Aug 14th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. #VERSION: 3.33
  2.  
  3. from novaprinter import prettyPrinter
  4. import sgmllib
  5. from helpers import retrieve_url, download_file
  6.  
  7. class nyaatorrents(object):
  8.   url = 'http://www.nyaa.se'
  9.   name = 'Nyaatorrents'
  10.   supported_categories = {'all': '0_0', 'anime': '1_0', 'books': '2_0', 'music': '3_0', 'pictures': '4_0', 'software': '6_0', 'games': '6_24'}
  11.  
  12.     def __init__(self):
  13.         self.results = []
  14.         self.parser = self.SimpleSGMLParser(self.results, self.url)
  15.  
  16.     def download_torrent(self, info):
  17.         print download_file(info)
  18.  
  19.     class SimpleSGMLParser(sgmllib.SGMLParser):
  20.         def __init__(self, results, url):
  21.             sgmllib.SGMLParser.__init__(self)
  22.             self.td_counter = None
  23.             self.current_item = None
  24.             self.results = results
  25.             self.url = url
  26.  
  27.         def start_a(self, attr):
  28.             params = dict(attr)
  29.             if 'page=view' in params['href']:
  30.               self.current_item = {}
  31.               self.td_counter = 0
  32.               self.current_item['desc_link'] = params['href'].strip()
  33.             elif 'page=download' in params['href']:
  34.               self.current_item['link'] = params['href'].strip()
  35.  
  36.     def handle_data(self, data):
  37.       if self.td_counter == 0:
  38.         if not self.current_item.has_key('name'):
  39.           self.current_item['name'] = ''
  40.         self.current_item['name'] += data.strip()
  41.       elif self.td_counter == 2:
  42.         if not self.current_item.has_key('size'):
  43.           self.current_item['size'] = ''
  44.         self.current_item['size'] += data.strip()
  45.       elif self.td_counter == 3:
  46.         if not self.current_item.has_key('seeds'):
  47.           self.current_item['seeds'] = ''
  48.         self.current_item['seeds'] += data.strip()
  49.       elif self.td_counter == 4:
  50.         if not self.current_item.has_key('leech'):
  51.           self.current_item['leech'] = ''
  52.         self.current_item['leech'] += data.strip()
  53.  
  54.         def start_td(self, attr):
  55.             if isinstance(self.td_counter, int):
  56.                 self.td_counter += 1
  57.                 if self.td_counter > 4:
  58.                     self.td_counter = None
  59.                     if self.current_item:
  60.                       self.current_item['engine_url'] = self.url
  61.                       if not self.current_item['seeds'].isdigit():
  62.                         self.current_item['seeds'] = 0
  63.                       if not self.current_item['leech'].isdigit():
  64.                         self.current_item['leech'] = 0
  65.                       prettyPrinter(self.current_item)
  66.                       self.results.append('a')
  67.  
  68.     def search(self, what, cat='all'):
  69.     i = 1
  70.     while True and i < 11:
  71.       results = []
  72.       parser = self.SimpleSGMLParser(results, self.url)
  73.       dat = retrieve_url(self.url + '/?page=search&term=%s&offset=%d&cats=%s' % (what, i, self.supported_categories[cat]))
  74.       parser.feed(dat)
  75.       parser.close()
  76.       if len(results) <= 0:
  77.         break
  78.       i += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement