2021-02-07 00:21:58 +00:00
|
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
import igmirror
|
2021-02-07 01:56:08 +00:00
|
|
|
import sys
|
2021-02-08 20:16:06 +00:00
|
|
|
import os
|
2021-02-07 00:21:58 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
class MyServer(BaseHTTPRequestHandler):
|
|
|
|
def do_GET(self):
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", "application/json")
|
|
|
|
self.end_headers()
|
2021-02-07 13:55:02 +00:00
|
|
|
# the account is the first part of the URL
|
|
|
|
# and the action for the account is the last part
|
|
|
|
# example: /shakira/add
|
2021-02-07 00:21:58 +00:00
|
|
|
path = self.path.strip('/')
|
|
|
|
parts = path.split('/')
|
2021-02-08 20:16:06 +00:00
|
|
|
if len(parts) == 1:
|
|
|
|
if parts[0] == 'list':
|
|
|
|
accounts = os.listdir('./db/accounts')
|
|
|
|
for acc in sorted(set(accounts)):
|
|
|
|
self.wfile.write(bytes(acc+'\n', "utf-8"))
|
|
|
|
elif len(parts) == 2:
|
2021-02-07 13:55:02 +00:00
|
|
|
# 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()
|
2021-02-07 13:55:02 +00:00
|
|
|
else:
|
|
|
|
# make sure account name contains only safe characters
|
|
|
|
# i think IG usernames can only have this characters:
|
2021-02-07 23:03:04 +00:00
|
|
|
accname = re.sub(r'[^a-zA-Z0-9_\.]+', '', parts[0])
|
2021-02-07 13:55:02 +00:00
|
|
|
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)
|
2021-02-08 20:22:00 +00:00
|
|
|
elif parts[1].lower() == 'login':
|
|
|
|
igmirror.pixelfed_login(accname, True)
|
2021-02-08 14:49:42 +00:00
|
|
|
elif parts[1].lower() == 'logout':
|
|
|
|
igmirror.logout_account(accname)
|
2021-02-08 20:07:49 +00:00
|
|
|
elif parts[1].lower() == 'nuke':
|
|
|
|
igmirror.delete_statuses(accname)
|
2021-02-07 13:55:02 +00:00
|
|
|
self.wfile.write(bytes('{"status": "ok"}', "utf-8"))
|
|
|
|
else:
|
|
|
|
self.wfile.write(bytes('{"status": "parameters are not correct"}', "utf-8"))
|
2021-02-07 00:21:58 +00:00
|
|
|
|
2021-02-07 01:56:08 +00:00
|
|
|
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))
|
2021-02-07 00:21:58 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
webServer.serve_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
|
|
|
webServer.server_close()
|
|
|
|
print("Server stopped.")
|