示例#1
0
def _check_and_parse(tracker, source, target, message):
    if not _is_announcement(source, target, tracker):
        logger.debug("Message is no announcement")
        return

    message = _sanitize_message(message)

    variables = announce_parser.parse(tracker, message)
    if variables is None:
        return None

    return create_announcement(tracker, variables)
    def test_no_torrent_name(self):
        th = TrackerHelper()
        elements1 = [
            HelperXml(x) for x in [
                ["string", "value", "test_string"],
                ["var", "name", "var1"],
                ["varenc", "name", "var2"],
            ]
        ]

        elements2 = [
            HelperXml(x) for x in [
                ["var", "name", "tc1"],
                ["string", "value", " "],
                ["varenc", "name", "tc2"],
            ]
        ]

        elements3 = [HelperXml(x) for x in [["string", "value", "a_category"]]]

        th.config["tc1"] = "config_text1%"
        th.config["tc2"] = "config_text2%"

        th.config.insert_var("first_var", elements1)
        th.config.insert_var("torrentUrl", elements2)
        th.config.insert_var("category", elements3)
        variables = {"var1": "testvar1&", "var2": "testvar2&"}
        var = announcement.create_announcement(th, variables)
        self.assertEqual(var, None, "No match should return None")
        self.assertEqual(
            variables["first_var"],
            "test_stringtestvar1&testvar2%26",
            "Variable not correct",
        )
        self.assertEqual(
            variables["torrentUrl"],
            "config_text1% config_text2%25",
            "Variable not correct",
        )

        self.assertEqual(
            variables["category"],
            "a_category",
            "Variable not correct",
        )
    def test_extract_missing_non_capture_group(self):
        th = TrackerHelper()
        variables = {
            "srcvar": " a name  -   ",
        }

        th.config.insert_extract("srcvar", "^(.*) - (?:(.*))$",
                                 ["torrentName", "g1"], False)
        announce = announcement.create_announcement(th, variables)
        self.assertEqual(announce, None, "No match should return None")
        self.assertEqual(
            variables["torrentName"],
            "a name",
        )
        self.assertTrue(
            "g1" not in variables,
            "Group should be missing",
        )
    def test_extract_missing_non_optional(self):
        th = TrackerHelper()
        variables = {
            "nomatch": "something else",
        }

        th.config.insert_extract("nomatch", "^(.*) - (.*)$",
                                 ["torrentName", "g1"], False)
        announce = announcement.create_announcement(th, variables)
        self.assertEqual(announce, None, "No match should return None")
        self.assertTrue(
            "torrentName" not in variables,
            "Group should be missing",
        )
        self.assertTrue(
            "g1" not in variables,
            "Group should be missing",
        )
    def test_no_torrent_ssl_url(self):
        th = TrackerHelper()
        elements1 = [
            HelperXml(x) for x in [
                ["string", "value", "test_string"],
                ["var", "name", "var1"],
                ["varenc", "name", "var2"],
            ]
        ]

        th.config["torrent_https"] = True
        th.config.insert_var("torrentName", elements1)

        variables = {"var1": "testvar1&", "var2": "testvar2&"}
        var = announcement.create_announcement(th, variables)
        self.assertEqual(var, None, "No match should return None")
        self.assertEqual(
            variables["torrentName"],
            "test_stringtestvar1&testvar2%26",
            "Variable not correct",
        )
    def test_extract_not_valid(self):
        th = TrackerHelper()
        variables = {
            "mysrc": "group1  -  group2",
            "anothersrc": " group3  :  group4"
        }

        th.config.insert_extract("mysrc", "^(.*) - (.*)$", ["g1", "g2"], False)
        th.config.insert_extract("anothersrc", "^(.*) : (.*)$", ["g3", "g4"],
                                 False)
        th.config.insert_extract("missing", ">(.*)$", ["g5"], True)
        announce = announcement.create_announcement(th, variables)
        self.assertEqual(announce, None, "No match should return None")
        self.assertEqual(
            variables["g1"],
            "group1",
            "Variable not correct",
        )
        self.assertEqual(
            variables["g2"],
            "group2",
            "Variable not correct",
        )
        self.assertEqual(
            variables["g3"],
            "group3",
            "Variable not correct",
        )
        self.assertEqual(
            variables["g4"],
            "group4",
            "Variable not correct",
        )
        self.assertTrue(
            "g5" not in variables,
            "Group should be missing",
        )
    def test_var_valid(self):
        th = TrackerHelper()
        elements1 = [
            HelperXml(x) for x in [
                ["string", "value", "test_string"],
                ["var", "name", "var1"],
                ["varenc", "name", "tc1"],
            ]
        ]

        elements2 = [
            HelperXml(x) for x in [
                ["var", "name", "var2"],
                ["string", "value", " "],
                ["varenc", "name", "tc2"],
            ]
        ]

        th.config["tc1"] = "config_text1%"
        th.config["tc2"] = "config_text2%"

        th.config.insert_var("torrentName", elements1)
        th.config.insert_var("torrentUrl", elements2)
        variables = {"var1": "testvar1&", "var2": "testvar2&"}
        announce = announcement.create_announcement(th, variables)
        self.assertNotEqual(announce, None, "Should return match")
        self.assertEqual(
            announce.title,
            "test_stringtestvar1&config_text1%25",
            "Title does not match",
        )
        self.assertEqual(announce.torrent_url, "testvar2& config_text2%25",
                         "URL does not match")
        self.assertEqual(announce.category, None, "Category should be None")
        self.assertEqual(announce.indexer, "trackername", "Wrong indexer")
        self.assertTrue(
            get_time_passed(announce.date) < 0.005, "Date is wrong")
    def test_extract_valid(self):
        th = TrackerHelper()
        variables = {
            "mysrc": "a title  -  group1",
            "anothersrc": " an url  :  group2",
            "present": "> a_category",
        }

        th.config.insert_extract("mysrc", "^(.*) - (.*)$",
                                 ["torrentName", "g1"], False)
        th.config.insert_extract("anothersrc", "^(.*) : (.*)$",
                                 ["torrentUrl", "g2"], False)
        th.config.insert_extract("present", ">(.*)$", ["category"], True)
        announce = announcement.create_announcement(th, variables)
        self.assertNotEqual(announce, None, "Should return match")
        self.assertEqual(
            announce.title,
            "a title",
            "Title does not match",
        )
        self.assertEqual(announce.torrent_url, "an url", "URL does not match")
        self.assertEqual(announce.category, "a_category",
                         "Category should be None")
        self.assertEqual(announce.indexer, "trackername", "Wrong indexer")
        self.assertTrue(
            get_time_passed(announce.date) < 0.005, "Date is wrong")
        self.assertEqual(
            variables["g1"],
            "group1",
            "Variable not correct",
        )
        self.assertEqual(
            variables["g2"],
            "group2",
            "Variable not correct",
        )