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 

20import logging 

21import cfscrape 

22from typing import List, Dict 

23from xdcc_dl.entities.XDCCPack import XDCCPack 

24from xdcc_dl.entities.IrcServer import IrcServer 

25 

26 

27def find_subsplease_packs(search_phrase: str) -> List[XDCCPack]: 

28 """ 

29 Method that conducts the xdcc pack search for subsplease.org 

30 

31 :return: the search results as a list of XDCCPack objects 

32 """ 

33 if not search_phrase: 

34 return [] 

35 

36 search_query = search_phrase.replace(" ", "%20") 

37 search_query = search_query.replace("!", "%21") 

38 

39 url = "https://subsplease.org/xdcc/search.php?t=" + search_query 

40 scraper = cfscrape.create_scraper() 

41 response = scraper.get(url) 

42 

43 if response.status_code >= 300: 

44 logging.warning("Failed to load data from subsplease. " 

45 "Most likely has something to do with CloudFlare's " 

46 "DDoS protection") 

47 return [] 

48 

49 results = response.text.split(";") 

50 

51 packs = [] 

52 for result in results: 

53 

54 try: 

55 result = parse_result(result) 

56 botname = result["b"] 

57 filename = result["f"] 

58 filesize = int(result["s"]) 

59 packnumber = int(result["n"]) 

60 pack = XDCCPack(IrcServer("irc.rizon.net"), botname, packnumber) 

61 pack.set_filename(filename) 

62 pack.set_size(filesize * 1000 * 1000) 

63 packs.append(pack) 

64 

65 except IndexError: # In case the line is not parseable 

66 pass 

67 

68 return packs 

69 

70 

71def parse_result(result: str) -> Dict[str, str]: 

72 """ 

73 Turns the weird subsplease response syntax into a useable dictionary 

74 :param result: The result to parse 

75 :return: The result as a dictionary 

76 """ 

77 

78 # Result should look like this: 

79 # {b: "Bot", n:filesize, s:packnumber, f:"filename"} 

80 

81 data = {} 

82 result = result.split("=", 1)[1].strip() 

83 result = result[1:-1] # Trim away curly braces 

84 

85 current_key = None 

86 for position, segment in enumerate(result.split("\"")): 

87 

88 if segment == "": 

89 continue 

90 

91 if position % 2 == 0: 

92 # Segment is not a string, must be evaluated further 

93 for part in segment.split(","): 

94 

95 if part == "": 

96 continue 

97 

98 key, content = part.split(":", 1) 

99 current_key = key.strip() 

100 if content != "": 

101 data[current_key.strip()] = content.strip() 

102 

103 else: 

104 # Segment is a string 

105 data[str(current_key)] = segment 

106 

107 return data