-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
152 lines (138 loc) · 4.79 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from ed import ed
from update import update_replit
from package import load_pkg, make_pkg, PackageError, PackageNotFound, FormatError, NameTaken, get_pkg_zip
import random
import os
import json
import io
from flask import Flask, request, Response, abort, redirect, send_file
import requests
import shutil
class MyResponse(Response):
default_mimetype = 'text/plain'
app = Flask('app')
app.response_class = MyResponse
def requires_auth(token):
if token != os.environ.get("token"):
abort(403)
def url_exists(url):
"""Boolean return - check to see if the site exists.
This function takes a url as input and then it requests the site
head - not the full html and then it checks the response to see if
it's less than 400. If it is less than 400 it will return TRUE
else it will return False.
"""
try:
site_ping = requests.head(url)
if site_ping.status_code < 400:
return True
else:
return False
except Exception:
return False
def get(name):
return request.form.get(name, request.args.get(name))
@app.route('/')
def home():
return 'This is an api for EuropaPkgMan. It is not finished. Also, for making projects you need to use the interface that doesnt exist right now. Anyways, docs at /docs'
@app.route('/pkg/get', methods=["GET", "POST"])
def getpkg():
name = get("name")
if name == None:
return f"the arg 'name' is required and was not provided", 400
try:
return json.dumps(load_pkg(ed.encode(name)))
except PackageNotFound:
return f"Package '{name}' was not found", 404
except Exception as e:
return f"something went wrong, this is the python error\n{type(e).__name__}: {e}\nrolling back changes", 400
@app.route('/pkg/get/zip', methods=["GET", "POST"])
def getpkgzip():
name = get("name")
if name == None:
return f"the arg 'name' is required and was not provided", 400
zippath, mimetype, download_name, as_attachment = get_pkg_zip(name)
return send_file(zippath,mimetype = mimetype,download_name=download_name,as_attachment = as_attachment)
@app.route('/pkg/get/readme', methods=["GET", "POST"])
def getpkgreadme():
name = get("name")
if name == None:
return f"the arg 'name' is required and was not provided", 400
root = os.path.join("pkgs", ed.encode(name))
content = None
with open(os.path.join(root, "README.md")) as f:
content = f.read()
return content
@app.route('/pkg/make', methods=["GET", "POST"])
def makepkg():
requires_auth(get("token"))
name = get("name")
username = get("username")
data = json.loads(get("data"))
if name == None:
return f"the arg 'name' is required and was not provided", 400
elif username == None:
return f"the arg 'username' is required and was not provided", 400
elif data == None:
return f"the arg 'data' is required and was not provided", 400
try:
make_pkg(username, name, data)
except NameTaken:
return "name taken", 400
except FormatError:
return "format is not met", 400
except Exception as e:
try:
shutil.rmtree(os.path.join("pkgs", ed.encode(name)))
except Exception:
pass
update_replit()
return f"something went wrong, this is the python error\n{type(e).__name__}: {e}\nrolling back changes", 500
return "success"
@app.route('/pkg/edit/readme', methods=["GET", "POST"])
def editpkgreadme():
requires_auth(get("token"))
name = get("name")
username = get("username")
content = get("content")
if name == None:
return f"the arg 'name' is required and was not provided", 400
elif username == None:
return f"the arg 'username' is required and was not provided", 400
elif content == None:
return f"the arg 'content' is required and was not provided", 400
root = os.path.join("pkgs", ed.encode(name))
info = json.load(open(os.path.join(root, "info.json")))
if username == info["owner"]:
with open(os.path.join(root, "README.md"), "w") as f:
f.write(content)
return "success"
else:
return "user is not the owner of this project", 403
@app.route('/pkg/list', methods=["GET", "POST"])
def getpkglist():
length = get("length")
if length == None:
return f"the arg 'length' is required and was not provided", 400
elif not length.isdigit():
return f"the arg 'length' needs to be a vaild number", 400
length = int(length)
l = []
n = 0
for f in os.scandir('pkgs'):
if n==length:
return json.dumps(l)
if f.is_dir():
l.append(f.name)
n+=1
if n==length:
return json.dumps(l)
return 'that is more than the amount of listings', 400
# docs
@app.route('/docs')
def docs():
return redirect("https://europa-docs.europalang.repl.co/Package%20Manager")
@app.errorhandler(403)
def forbidden(e):
return 'you do not have the required auth for this action', 403
app.run(host='0.0.0.0', port=8080)