import http.server
import socketserver
import urllib.parse
import hashlib

class AtomServer(http.server.BaseHTTPRequestHandler):
    def do_GET(self):
        # Parse the URL to get the base path for item IDs
        parsed_url = urllib.parse.urlparse(self.path)
        base_path = parsed_url.path

        # Generate consistent ID based on URL
        url_hash = hashlib.md5(base_path.encode()).hexdigest()
        feed_id = f"tag:example.com,{url_hash}"

        # Create feed content
        feed_content = f'''<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>{feed_id}</id>
  <title>Feed for {base_path}</title>
  <updated>2023-01-01T00:00:00Z</updated>
  <author>Feed Generator</author>
'''

        # Add 500 items
        for i in range(500):
            # Generate consistent item ID based on URL and item index
            item_id = f"{feed_id}:item-{i}"
            feed_content += f'''  <entry>
    <id>{item_id}</id>
    <title>Item {i} in {base_path}</title>
    <updated>2023-01-01T00:00:00Z</updated>
    <summary>Summary for item {i}</summary>
    <link href="{self.path}?item={i}" rel="alternate" />
  </entry>
'''

        feed_content += '</feed>'

        # Send response
        self.send_response(200)
        self.send_header("Content-type", "application/atom+xml")
        self.end_headers()
        self.wfile.write(feed_content.encode())

if __name__ == "__main__":
    PORT = 8000
    with socketserver.TCPServer(("", PORT), AtomServer) as httpd:
        print(f"Server running at http://localhost:{PORT}/")
        httpd.serve_forever()
