Python

Now proudly hosted at pyth.link! Now to finish it O_O

fork
loading
Files
  • main.py
  • static
  • templates
  • links.db
  • requirements.txt
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from flask import Flask, render_template, request, redirect, make_response
import itertools
from urllib.parse import urlparse
import os
from flask_sqlalchemy import SQLAlchemy

project_dir = os.path.dirname(os.path.abspath(__file__))
database_file = "sqlite:///{}".format("links.db")

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = database_file
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

db = SQLAlchemy(app)

class Link(db.Model):
    link = db.Column(db.String(80), unique=True, nullable=False, primary_key=True)
    target = db.Column(db.String(80), nullable=False)
    password = db.Column(db.String(80))

    def __repr__(self):
        return "<Link: {}>".format(self.link)

chars = list("abcdefghijklmnopqrstuvwxyzABCDEGHIJKLMNOPQRSTUVWXYZ1234567890")

def make_url():
    length = 1
    for url in itertools.combinations(chars, length):
        if Link.query.filter_by(link="".join(url)) != None:
            return "".join(url)
    
    length += 1
    for url in itertools.combinations(chars, length):
        if Link.query.filter_by(link="".join(url)) != None:
            return "".join(url)

def is_url(url):
    try:
        result = urlparse(url)
        return all([result.scheme, result.netloc, result.path])
    except:
        return False

@app.shell_context_processor
def make_shell_context():
    return {'db': db, 'Link': Link}

#u = Link(link='test', target='http://google.com', password="hi")
#db.session.add(u)
#db.session.commit()

@app.route('/m/<page>')
def go_to_page(page):
    if request.cookies.get("peek") == "1":
        return redirect("/m/peek/" + page)
    else:
        try:
            target = Link.query.filter_by(link=page).one().target
            return redirect(target, 301)
        except:
            return render_template("error.html") 

@app.route('/m/<page>/<extra>')
def go_to_page_extra(page, extra):
    if request.cookies.get("peek") == "1":
        return redirect("/m/peek/" + page + "/" + extra)
    else:
        try:
            target = urls[page][0]
            if extra:
                if target[-1] != "/":
                    target += "/"
                target += extra
            return redirect(target, 301)
        except:
            return render_template("error.html") 

@app.route('/m/peek/<page>')
def peek_page(page):
    try:
        target = urls[page][0]
        return render_template("peek.html", link=page, target=target)
    except KeyError:
        return render_template("error.html") 

@app.route('/m/peek/<page>/<extra>')
def peek_page_extra(page, extra):
    try:
        target = urls[page][0]
        if extra:
            if target[-1] != "/":
                target += "/"
            target += extra
        return render_template("peek.html", link=page, target=target)
    except:
        return render_template("error.html") 

@app.route('/')
def index():
	return render_template('index.html', peek="On" if request.cookies.get("peek") == "1" else "Off")

@app.route("/murder")
def murder():
    return redirect("https://sites.google.com/wrdsb.ca/murderatthemoviesbreakout", 301)

@app.route('/decoder')
def decode():
    return render_template('decode.html')

@app.route('/toggle-peek')
def toggle_peek():
    resp = make_response(redirect("https://www.pyth.link"))
    if request.cookies.get("peek") in (None, "0"):
        resp.set_cookie("peek", "1")
    elif request.cookies.get("peek") == "1":
        resp.set_cookie("peek", "0")
    return resp

@app.route('/make', defaults={'target': None, 'link': None, 'password': None})
def make_link(target, link, password):   
    if target == None:
        target = request.args.get('target')
    if link == None:
        link = request.args.get('link')
    if password == None:
        password = request.args.get('password')
    if (not target.startswith("http://")) and (not target.startswith("https://")):
        target = "http://" + target
    if link in urls:
        return render_template("taken.html")
    else:
        if link == "":
            link = make_url()
        urls[link] = [target, password]
        return render_template("created.html", link=link, password=password)

@app.route('/delete', defaults={'link': None, 'password': None})
def delete_link(link, password):   
    if link == None:
        link = request.args.get('link')
    if password == None:
        password = request.args.get('password')
    try:
        password_real = urls[link][1]
        if password == str(password_real):
            del urls[link]
            return render_template("removed.html")
        else:
            return render_template("wrong.html")
    except KeyError:
        render_template("error.html")

@app.route('/change-link', defaults={'link': None, 'password': None, 'new_link': None})
def change_link(link, password, new_link):   
    if link == None:
        link = request.args.get('link')
    if password == None:
        password = request.args.get('password')
    if new_link == None:
        new_link = request.args.get('new_link')
    if new_link == "":
        new_link = make_url()
    try:
        password_real = urls[link][1]
        if password == str(password_real) and new_link not in urls:
            urls[new_link] = [urls[link][0], urls[link][1]]
            del urls[link]
            return render_template("changed.html")
        elif password != str(password_real):
            return render_template("wrong.html")
        elif new_link in urls:
            return render_template("taken.html")
    except KeyError:
        render_template("error.html")

@app.route('/change-target', defaults={'link': None, 'password': None, 'new_target': None})
def change_target(link, password, new_target):   
    if link == None:
        link = request.args.get('link')
    if password == None:
        password = request.args.get('password')
    if new_target == None:
        new_target = request.args.get('new_target')
    if (not new_target.startswith("http://")) and (not new_target.startswith("https://")):
        new_target = "http://" + new_target
    try:
        password_real = urls[link][1]
        if password == str(password_real):
            urls[link] = [new_target, password]
            return render_template("changed.html")
        else:
            return render_template("wrong.html")
    except KeyError:
        render_template("error.html")

@app.route('/decode', defaults={'link': None})
def decode_link(link):
    if link == None:
        link = request.args.get('link')
    target = urls[link][0]
    return render_template("decoded.html", target=target)

app.run(host='0.0.0.0', port=8080)