#!/usr/bin/env python3
import urllib.request
import json
import re
from html.parser import HTMLParser

class TextExtractor(HTMLParser):
    def __init__(self):
        super().__init__()
        self.text = []
        self.in_script = False
        self.in_style = False
    
    def handle_starttag(self, tag, attrs):
        if tag == 'script':
            self.in_script = True
        elif tag == 'style':
            self.in_style = True
    
    def handle_endtag(self, tag):
        if tag == 'script':
            self.in_script = False
        elif tag == 'style':
            self.in_style = False
    
    def handle_data(self, data):
        if not self.in_script and not self.in_style:
            text = data.strip()
            if text:
                self.text.append(text)
    
    def get_text(self):
        return '\n'.join(self.text)

def fetch_page(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    req = urllib.request.Request(url, headers=headers)
    try:
        with urllib.request.urlopen(req, timeout=15) as response:
            return response.read().decode('utf-8')
    except Exception as e:
        return f"Error fetching {url}: {str(e)}"

# Fetch Beamer pricing
print("=== BEAMER PRICING ===")
beamer_html = fetch_page("https://www.getbeamer.com/pricing")
if "Error" not in beamer_html[:100]:
    parser = TextExtractor()
    parser.feed(beamer_html)
    print(parser.get_text()[:15000])
else:
    print(beamer_html)

print("\n\n=== CANNY PRICING ===")
# Fetch Canny pricing
canny_html = fetch_page("https://canny.io/pricing")
if "Error" not in canny_html[:100]:
    parser2 = TextExtractor()
    parser2.feed(canny_html)
    print(parser2.get_text()[:15000])
else:
    print(canny_html)
