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 2016 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of xdcc-dl. 

5 

6xdcc-dl 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 

11xdcc-dl 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 xdcc-dl. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20from enum import Enum 

21from typing import List, Set, Callable, Optional 

22from xdcc_dl.entities.XDCCPack import XDCCPack 

23from xdcc_dl.pack_search.procedures.nibl import find_nibl_packs 

24from xdcc_dl.pack_search.procedures.ixirc import find_ixirc_packs 

25from xdcc_dl.pack_search.procedures.subsplease import find_subsplease_packs 

26from xdcc_dl.pack_search.procedures.xdcc_eu import find_xdcc_eu_packs 

27 

28 

29class SearchEngine: 

30 """ 

31 An XDCC Pack Search Engine 

32 """ 

33 

34 def __init__(self, name: str, procedure: Callable): 

35 """ 

36 Initializes the Search Engine 

37 :param name: The name of the search engine 

38 :param procedure: A function that performs the XDCC pack search 

39 """ 

40 self.name = name 

41 self._procedure = procedure 

42 

43 def search(self, term: str) -> List[XDCCPack]: 

44 """ 

45 Searches for packs that match the provided term 

46 :param term: The term to search for 

47 :return: A list of XDCC Packs 

48 """ 

49 return self._procedure(term) 

50 

51 

52class SearchEngineType(Enum): 

53 """ 

54 The different implemented search engines 

55 """ 

56 

57 SUBSPLEASE = SearchEngine("SubsPlease", find_subsplease_packs) 

58 NIBL = SearchEngine("Nibl", find_nibl_packs) 

59 IXIRC = SearchEngine("iXirc", find_ixirc_packs) 

60 XDCC_EU = SearchEngine("xdcc-eu", find_xdcc_eu_packs) 

61 

62 @classmethod 

63 def choices(cls, lower: bool = True) -> Set[str]: 

64 """ 

65 Provides a set of strings that represent the possible search engine 

66 choices. 

67 :param lower: Provides lower-case names of the search engine types 

68 :return: The set of choices 

69 """ 

70 choices = [] 

71 for choice in cls: 

72 name = choice.value.name.lower() if lower else choice.value.name 

73 choices.append(name) 

74 return set(choices) 

75 

76 @classmethod 

77 def resolve(cls, name: str) -> Optional[SearchEngine]: 

78 """ 

79 Resolves a string identifier of a search engine and provides 

80 the correct search engine 

81 :param name: The name of the search engine 

82 :return: The search engine object or None if no match was found 

83 """ 

84 for choice in cls: 

85 if choice.value.name.lower() == name.lower(): 

86 return choice.value 

87 return None