Coverage for xdcc_dl/pack_search/procedures/subsplease.py : 10%

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>
4This file is part of xdcc-dl.
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.
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.
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"""
20# imports
21import cfscrape
22from typing import List, Dict
23from xdcc_dl.entities.XDCCPack import XDCCPack
24from xdcc_dl.entities.IrcServer import IrcServer
27def find_subsplease_packs(search_phrase: str) -> List[XDCCPack]:
28 """
29 Method that conducts the xdcc pack search for subsplease.org
31 :return: the search results as a list of XDCCPack objects
32 """
33 if not search_phrase:
34 return []
36 search_query = search_phrase.replace(" ", "%20")
37 search_query = search_query.replace("!", "%21")
39 url = "https://subsplease.org/xdcc/search.php?t=" + search_query
40 scraper = cfscrape.create_scraper()
41 results = scraper.get(url).text.split(";")
43 packs = []
44 for result in results:
46 try:
47 result = parse_result(result)
48 botname = result["b"]
49 filename = result["f"]
50 filesize = int(result["s"])
51 packnumber = int(result["n"])
52 pack = XDCCPack(IrcServer("irc.rizon.net"), botname, packnumber)
53 pack.set_filename(filename)
54 pack.set_size(filesize * 1000 * 1000)
55 packs.append(pack)
57 except IndexError: # In case the line is not parseable
58 pass
60 return packs
63def parse_result(result: str) -> Dict[str, str]:
64 """
65 Turns the weird subsplease response syntax into a useable dictionary
66 :param result: The result to parse
67 :return: The result as a dictionary
68 """
70 # Result should look like this:
71 # {b: "Bot", n:filesize, s:packnumber, f:"filename"}
73 data = {}
74 result = result.split("=", 1)[1].strip()
75 result = result[1:-1] # Trim away curly braces
77 current_key = None
78 for position, segment in enumerate(result.split("\"")):
80 if segment == "":
81 continue
83 if position % 2 == 0:
84 # Segment is not a string, must be evaluated further
85 for part in segment.split(","):
87 if part == "":
88 continue
90 key, content = part.split(":", 1)
91 current_key = key.strip()
92 if content != "":
93 data[current_key.strip()] = content.strip()
95 else:
96 # Segment is a string
97 data[str(current_key)] = segment
99 return data