from html.parser import HTMLParser


class MyCollectionsAccessParser(HTMLParser):
    def __init__(self, *args, **kwargs):
        self.hrefs = []
        self.href = None
        super().__init__()

    def return_data(self):
        return self.hrefs

    def handle_starttag(self, tag, attrs):
        if tag == "a":
            for i, attr in enumerate(attrs):
                prev_attr = None
                if 0 <= i - 1 < len(attrs):
                    prev_attr = attrs[i - 1]
                if (
                    attr[0] == "class"
                    and attr[1] == "btn btn-sm access-apply-button"
                    and prev_attr
                    and prev_attr[0] == "href"
                ):
                    self.href = prev_attr[1]

    def handle_endtag(self, tag):
        if tag == "a" and self.href is not None:
            self.hrefs.append(self.href)
            self.href = None


class MyAppsWithdrawParser(HTMLParser):
    def __init__(self, *args, **kwargs):
        self.forms = []
        self.form = {}
        super().__init__()

    def return_data(self):
        return self.forms

    def handle_starttag(self, tag, attrs):
        if tag == "form":
            self.form = {
                "action": None,
                "csrfmiddlewaretoken": None,
                "submit": None,
            }
            for attr in attrs:
                if attr[0] == "action":
                    self.form["action"] = attr[1]

        if (
            tag == "input"
            and self.form
            and "action" in self.form
            and self.form["action"] is not None
        ):
            for i, attr in enumerate(attrs):
                next_attr = None
                if 0 <= i + 1 < len(attrs):
                    next_attr = attrs[i + 1]
                if (
                    attr[0] == "name"
                    and attr[1] == "csrfmiddlewaretoken"
                    and next_attr
                    and next_attr[0] == "value"
                ):
                    self.form["csrfmiddlewaretoken"] = next_attr[1]
                if (
                    attr[0] == "type"
                    and attr[1] == "submit"
                    and next_attr
                    and next_attr[0] == "value"
                    and next_attr[1] == "Withdraw"
                ):
                    self.form["submit"] = next_attr[1]

    def handle_endtag(self, tag):
        if tag == "form":
            self.forms.append(self.form)
            self.form = {}

@@ -1204,428 +1204,169 @@
     
-self.form = %7B%0A                %22action%22: None,%0A                %22csrfmiddlewaretoken%22: None,%0A                %22submit%22: None,%0A            %7D%0A            for attr in attrs:%0A                if attr%5B0%5D == %22action%22:%0A                    self.form%5B%22action%22%5D = attr%5B1%5D%0A%0A        if (%0A            tag == %22input%22%0A            and self.form%0A            and %22action%22 in self.form%0A            and self.form%5B%22action%22%5D is not None%0A        )
+for attr in attrs:%0A                if attr%5B0%5D == %22action%22:%0A                    self.form%5B%22action%22%5D = attr%5B1%5D%0A%0A        if tag == %22input%22 and %22action%22 in self.form
 :%0A  
@@ -2178,32 +2178,254 @@
  tag == %22form%22:%0A
+            if all(%0A                key in self.form%0A                for key in (%0A                    %22action%22,%0A                    %22csrfmiddlewaretoken%22,%0A                    %22submit%22,%0A                )%0A            ):%0A    
             self

improve validation for MyAppsWithdrawParser