Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""LICENSE 

2Copyright 2021 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of torrent-download. 

5 

6torrent-download is free software: you can redistribute it and/or modify 

7it under the terms of the GNU General Public License as published by 

8the Free Software Foundation, either version 3 of the License, or 

9(at your option) any later version. 

10 

11torrent-download is distributed in the hope that it will be useful, 

12but WITHOUT ANY WARRANTY; without even the implied warranty of 

13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14GNU General Public License for more details. 

15 

16You should have received a copy of the GNU General Public License 

17along with torrent-download. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import os 

21import requests 

22from typing import List 

23from bs4 import BeautifulSoup 

24from puffotter.units import byte_string_to_byte_count 

25from torrent_download.search.SearchEngine import SearchEngine 

26from torrent_download.entities.TorrentInfo import TorrentInfo 

27 

28 

29class NyaaSearchEngine(SearchEngine): 

30 """ 

31 Search engine for nyaa.si 

32 """ 

33 

34 @classmethod 

35 def name(cls) -> str: 

36 """ 

37 :return: The name of the search engine 

38 """ 

39 return "nyaa" 

40 

41 def search(self, search_term) -> List[TorrentInfo]: 

42 """ 

43 Performs the actual search 

44 :param search_term: The term to search for 

45 :return: The search results 

46 """ 

47 url = f"https://nyaa.si/?q={search_term.replace(' ', '+')}" \ 

48 f"&s=seeders&o=desc" 

49 page = requests.get(url) 

50 soup = BeautifulSoup(page.text, "html.parser") 

51 rows = soup.select("tr") 

52 

53 if len(rows) == 0: 

54 return [] 

55 

56 rows.pop(0) 

57 

58 results = [] 

59 for row in rows: 

60 columns = row.select("td") 

61 name = columns[1].select("a")[-1].text.strip() 

62 links = columns[2].select("a") 

63 torrent_file = \ 

64 os.path.join("https://nyaa.si", links[0]["href"][1:]) 

65 magnet_link = links[1]["href"] 

66 size_string = columns[3].text.replace("i", "").replace(" ", "") 

67 size = byte_string_to_byte_count(size_string) 

68 results.append(TorrentInfo(name, torrent_file, magnet_link, size)) 

69 

70 return results