ig-pixelfed-mirror/server.py

47 lines
1.6 KiB
Python
Raw Normal View History

from http.server import BaseHTTPRequestHandler, HTTPServer
import igmirror
import sys
import re
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
# the account is the first part of the URL
# and the action for the account is the last part
# example: /shakira/add
path = self.path.strip('/')
parts = path.split('/')
if len(parts) == 2:
# a wilcard select all accounts
if parts[0] == '*':
if parts[1].lower() == 'update':
2021-02-07 19:13:47 +00:00
igmirror.update_allaccounts_async()
else:
# make sure account name contains only safe characters
# i think IG usernames can only have this characters:
accname = re.sub(r'[^a-zA-Z0-9_\.]+', '', parts[0])
if parts[1].lower() == 'add':
igmirror.add_igaccount(accname)
elif parts[1].lower() == 'update':
2021-02-07 19:13:47 +00:00
igmirror.update_igaccount_async(accname)
self.wfile.write(bytes('{"status": "ok"}', "utf-8"))
else:
self.wfile.write(bytes('{"status": "parameters are not correct"}', "utf-8"))
if __name__ == "__main__":
port = 8080
if len(sys.argv) > 1:
port = int(sys.argv[1])
webServer = HTTPServer(('0.0.0.0', port), MyServer)
print("Server started http://%s:%s" % ('0.0.0.0', port))
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print("Server stopped.")