-
Notifications
You must be signed in to change notification settings - Fork 0
/
myproject.py
283 lines (235 loc) · 8.88 KB
/
myproject.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
from flask import *
from flask import jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from sqlalchemy.orm import sessionmaker
import random
import string
import os
import re
import urllib
import hashlib
from sqlalchemy.sql import func
from flask_cors import CORS, cross_origin
regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
alias_regex = re.compile('^[A-Za-z0-9\-\_]+$')
app = Flask(__name__)
project_dir = os.path.dirname(os.path.abspath(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = "Your connection string is here."
db = SQLAlchemy(app)
migrate = Migrate(app, db)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
HASH_LENGTH = 6
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
# modal
class CpmbUrl(db.Model):
__tablename__ = 'cpmburl'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String())
url = db.Column(db.String())
md5 = db.Column(db.String())
hashd = db.Column(db.String())
visited = db.Column(db.Integer)
is_private = db.Column(db.Integer)
is_pinmyblogs_url = db.Column(db.Integer)
def __init__(self, name, url, md5, visited, hashd):
self.name = name
self.url = url
self.md5 = md5
self.visited = visited
self.hashd = hashd
self.is_private = 0
self.is_pinmyblogs_url = 0
def __repr__(self):
return self.name
def engine(url, alias=None):
print("hello")
if alias == None or alias == "":
hash = generate_new_random(length=HASH_LENGTH) if url else ""
if hash is not "":
md5 = hashlib.md5(url.encode()).hexdigest()
result = db.session.query(CpmbUrl).filter(CpmbUrl.md5 == md5)
r = [row for row in result if row.md5 == md5]
if not r:
me = CpmbUrl(name="", url=url, md5=md5, hashd=hash, visited=0)
db.session.add(me)
db.session.commit()
else:
hash = r[0].hashd
return ({
"status": "success",
"message": "Short Url generated.",
"short_url": "goshort.in/{0}".format(hash),
"hash": hash,
"ori_url": url
})
else:
result = db.session.query(CpmbUrl).filter(CpmbUrl.hashd == alias)
r = [row for row in result if row.hashd == alias]
if len(r) == 0:
md5 = hashlib.md5(url.encode()).hexdigest()
me = CpmbUrl(name="", url=url, md5=md5, hashd=alias, visited=0)
db.session.add(me)
db.session.commit()
return ({
"status": "success",
"message": "Short Url generated.",
"short_url": "goshort.in/{0}".format(alias),
"hash": alias,
"ori_url": url
})
else:
return {
"status": "error",
"message": "Alias is already used.",
"ori_url": url,
"short_url": "goshort.in/{0}".format(alias),
"alias": alias,
}
# apis
@app.route("/api/v1/create", methods=["GET", 'POST'])
@cross_origin()
def api_create():
try:
url = request.args.get('url')
alias = request.args.get('alias')
message = ""
if url is None or url is "":
status = "empty" if url is "" else "missing"
message = "Request param url is " + status + "."
elif re.match(regex, url) is None:
message = "Invalid weblink in request param url."
else:
alias_empty_none = bool(alias is not None and alias is not "")
if alias_empty_none:
alias_status = re.match(alias_regex, alias)
if alias_status is not None:
return jsonify(engine(url, alias=alias))
elif alias_status is None:
message += "Request param alias may contain letters, numbers, and dashes."
elif alias is None or alias is "":
return jsonify(engine(url, alias=None))
return jsonify({"status": "error", "message": message})
except Exception as e:
print(e)
return jsonify({"status": "error", "message": message})
@app.route("/api/v1/track", methods=["GET", 'POST'])
# @cross_origin()
def api_track():
hashd = request.args.get('hash') or ""
if hashd is not None and hashd is not "":
result = db.session.query(CpmbUrl).filter(CpmbUrl.hashd == hashd)
r = [row for row in result]
# print(r)
if not r:
return jsonify({
"status": "error",
"message": "Url not found.",
"hash": hashd
})
else:
hash = r[0].hashd
count = r[0].visited
print(r)
return jsonify({
"status": "success",
"message": "Url clicked {0} times.".format(count),
"short_url": "goshort.in/{0}".format(hash),
"hash": hash,
"click_count": count,
"ori_url": r[0].url
})
render_template('track.html', count=r[0].visited, hashd=hash, url=r[0].url)
return jsonify({
"status": "error",
"message": "Request parameter(s) [hash] is blank or wrong."
})
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/robots.txt')
@app.route('/sitemap.xml')
def static_from_root():
return send_from_directory(app.static_folder, request.path[1:])
@app.route("/term_of_use", methods=["GET", 'POST'])
def terms_of_use():
return render_template('termsofuse.html')
@app.route("/disclaimer", methods=["GET", 'POST'])
def disclaimer():
return render_template('disclaimer.html')
@app.route("/contact", methods=["GET", 'POST'])
def contact():
return redirect("http://pinmyblogs.com/support")
@app.route("/privacy", methods=["GET", 'POST'])
def privacy():
return render_template('privacy.html')
@app.route("/", methods=["GET", 'POST'])
def index():
return render_template('index.html')
@app.route("/create", methods=["GET", 'POST'])
def create():
url = request.args.get('url') or ""
status = True if url == "" else True
hash = ""
if url is not None and url is not "":
sgt = re.match(regex, url) is not None
status = True if sgt == True else False
hash = generate_new_random(length=HASH_LENGTH) if status else ""
if hash is not "":
md5 = hashlib.md5(url.encode())
md5 = md5.hexdigest()
result = db.session.query(CpmbUrl).filter(CpmbUrl.md5 == md5)
r = [row for row in result if row.md5 == md5]
if not r:
me = CpmbUrl(name="", url=url, md5=md5, hashd=hash, visited=0)
db.session.add(me)
db.session.commit()
else:
hash = r[0].hashd
return render_template('create.html', url=url, status=status, hash=hash)
@app.route("/track", methods=["GET", 'POST'])
def track():
hashd = request.args.get('t') or ""
if hashd is not None and hashd is not "":
print(hashd)
result = db.session.query(CpmbUrl).filter(CpmbUrl.hashd == hashd)
print(result)
r = [row for row in result]
print(r)
if not r:
return render_template('track.html', dummy=hashd)
else:
hash = r[0].hashd
print(r)
render_template('track.html', count=r[0].visited, hashd=hash, url=r[0].url)
return render_template('track.html')
@app.route("/<path:dummy>", methods=["GET", 'POST'])
def index1(dummy=None):
if dummy is not None:
result = db.session.query(CpmbUrl).filter(CpmbUrl.hashd == dummy.strip()).all()
r = [row.url for row in result if row.hashd == dummy]
if r:
a = db.session.query(CpmbUrl).filter(CpmbUrl.hashd == dummy.strip()).update(
{'visited': CpmbUrl.visited + 1})
db.session.commit()
return redirect(r[0])
else:
return render_template('index.html', dummy=dummy)
return redirect(url_for("index"))
# helper functions
def generate_new_random(length=6, chars=string.ascii_letters + string.digits):
gen = ''.join(random.choice(chars) for _ in range(length))
result = db.session.query(CpmbUrl).filter(CpmbUrl.hashd == gen)
r = [row.md5 for row in result if row.md5 == gen]
return generate_new_random() if r else gen
if __name__ == "__main__":
app.run(debug=False)
app.run(host='0.0.0.0')