SRoy

1337x.st_search.py

Aug 15th, 2021
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.73 KB | None | 0 0
  1. # VERSION: 2.00
  2. # AUTHORS: sa3dany (modified by me)
  3.  
  4. # LICENSING INFORMATION
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22.  
  23. import re
  24. from html.parser import HTMLParser
  25.  
  26. from helpers import download_file, retrieve_url
  27. from novaprinter import prettyPrinter
  28.  
  29.  
  30. class one337x(object):
  31.     url = 'https://1337x.st'
  32.     name = '1337x'
  33.     supported_categories = {
  34.         'all': None,
  35.         'anime': 'Anime',
  36.         'software': 'Applications',
  37.         'games': 'Games',
  38.         'movies': 'Movies',
  39.         'music': 'Music',
  40.         'tv': 'TV',
  41.     }
  42.  
  43.     class MyHtmlParser(HTMLParser):
  44.         def error(self, message):
  45.             pass
  46.  
  47.         A, TD, TR, HREF, TABLE = ('a', 'td', 'tr', 'href', 'tbody')
  48.  
  49.         def __init__(self, url):
  50.             HTMLParser.__init__(self)
  51.             self.url = url
  52.             self.row = {}
  53.             self.column = None
  54.             self.insideRow = False
  55.             self.foundTable = False
  56.             self.foundResults = False
  57.             self.parser_class = {
  58.                 # key: className
  59.                 'name': 'name',
  60.                 'seeds': 'seeds',
  61.                 'leech': 'leeches',
  62.                 'size': 'size'
  63.             }
  64.  
  65.         def handle_starttag(self, tag, attrs):
  66.             params = dict(attrs)
  67.  
  68.             if 'search-page' in params.get('class', ''):
  69.                 self.foundResults = True
  70.                 return
  71.  
  72.             if self.foundResults and tag == self.TABLE:
  73.                 self.foundTable = True
  74.                 return
  75.  
  76.             if self.foundTable and tag == self.TR:
  77.                 self.insideRow = True
  78.                 return
  79.  
  80.             if self.insideRow and tag == self.TD:
  81.                 classList = params.get('class', None)
  82.                 for columnName, classValue in self.parser_class.items():
  83.                     if classValue in classList:
  84.                         self.column = columnName
  85.                         self.row[self.column] = -1
  86.                 return
  87.  
  88.             if self.insideRow and tag == self.A:
  89.                 if self.column != 'name' or self.HREF not in params:
  90.                     return
  91.                 link = params[self.HREF]
  92.                 if link.startswith('/torrent/'):
  93.                     link = f'{self.url}{link}'
  94.                     self.row['link'] = link
  95.                     self.row['engine_url'] = self.url
  96.                     self.row['desc_link'] = link
  97.  
  98.         def handle_data(self, data):
  99.             if self.insideRow and self.column:
  100.                 self.row[self.column] = data
  101.                 self.column = None
  102.  
  103.         def handle_endtag(self, tag):
  104.             if tag == 'table':
  105.                 self.foundTable = False
  106.  
  107.             if self.insideRow and tag == self.TR:
  108.                 self.insideRow = False
  109.                 self.column = None
  110.                 array_length = len(self.row)
  111.                 if array_length < 1:
  112.                     return
  113.                 prettyPrinter(self.row)
  114.                 self.row = {}
  115.  
  116.     def download_torrent(self, info):
  117.         info_page = retrieve_url(info)
  118.         magnet_match = re.search(r'href\s*\=\s*"(magnet[^"]+)"', info_page)
  119.         if magnet_match and magnet_match.groups():
  120.             print(magnet_match.groups()[0] + ' ' + info)
  121.         else:
  122.             raise Exception('Error, please fill a bug report!')
  123.  
  124.     def search(self, what, cat='all'):
  125.         category = self.supported_categories[cat]
  126.  
  127.         if category:
  128.             page_url = f'{self.url}/category-search/{what}/{category}/1/'
  129.         else:
  130.             page_url = f'{self.url}/search/{what}/1/'
  131.  
  132.         parser = self.MyHtmlParser(self.url)
  133.         html = retrieve_url(page_url)
  134.         parser.feed(html)
  135.         parser.close()
Add Comment
Please, Sign In to add comment