#!/usr/bin/python3
# CustomMedia but all requests are just sent to a static server instead of trying to resolve the origin server
import urllib.request

class MyServer:
    def __init__(self, environ, start_response):
        self.environ = environ
        self.start_response = start_response

    def __iter__(self):
        path_info = hs = self.environ['PATH_INFO'].split('/')

        if path_info[2] == 'client':
            mediatype = path_info[5]
            hs = path_info[6]
            mediaid = path_info[7]
        else:
            mediatype = path_info[4]
            hs = path_info[5]
            mediaid = path_info[6]

        hsp = 'https://matrix.catgirl.cloud/_matrix/media/v3/' + mediatype + '/' + hs + '/' + mediaid + '?' + self.environ['QUERY_STRING']
        self.start_response('301 Moved Permanently', [('Location', hsp)])
        return iter([])

if __name__ == "__main__":
    from gunicorn.app.base import BaseApplication

    class GunicornServer(BaseApplication):
        def __init__(self, app, options=None):
            self.options = options or {}
            self.application = app
            super().__init__()

        def load_config(self):
            for key, value in self.options.items():
                self.cfg.set(key, value)

        def load(self):
            return self.application

    options = {
        'bind': 'localhost:9999',
        'workers': 16,  # Adjust the number of workers based on your system's resources - ChatGPT
    }

    server = GunicornServer(MyServer, options)
    server.run()