Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
KrasnovAS
short_url_generator
Commits
9a247b88
Commit
9a247b88
authored
Oct 01, 2020
by
AntonKras
Browse files
Добавлена функция "добавления соответствия {small_url:long_url}"
parent
a0cca0a3
Changes
3
Hide whitespace changes
Inline
Side-by-side
app.py
View file @
9a247b88
from
flask
import
Flask
,
render_template
,
redirect
,
make_response
from
flask
import
Flask
,
render_template
,
redirect
,
request
,
abort
,
flash
import
re
,
os
,
random
,
string
import
yaml
from
werkzeug.urls
import
url_fix
from
urllib.parse
import
urlparse
,
urlunparse
,
quote
app
=
Flask
(
__name__
)
app
.
secret_key
=
os
.
urandom
(
24
)
@
app
.
route
(
'/'
,
methods
=
[
'GET'
])
@
app
.
route
(
'/'
,
methods
=
[
'GET'
,
'POST'
])
def
main
():
return
render_template
(
'index.html'
)
@
app
.
route
(
'/'
,
defaults
=
{
'path'
:
''
})
@
app
.
route
(
'/add'
,
methods
=
[
'GET'
,
'POST'
])
def
add
():
url
=
request
.
args
.
get
(
"url1"
)
if
check_if_input_is_url
(
url
)
is
None
:
flash
(
'URL введен некорректно'
)
return
redirect
(
'http://127.0.0.1:5000/'
)
else
:
short_url
=
generate_short_url
()
data
=
{
short_url
:
url
}
with
open
(
'static/urls.yml'
,
'a+'
,
encoding
=
'utf8'
)
as
f
:
yaml
.
dump
(
data
,
f
,
default_flow_style
=
False
,
allow_unicode
=
True
)
f
.
close
()
return
redirect
(
'http://127.0.0.1:5000/'
)
@
app
.
route
(
'/<path:path>'
)
def
redirect_somewhere
(
path
):
yml_dict
=
extract_data_from_yml
(
'static/urls.yml'
)
print
(
yml_dict
)
if
path
in
yml_dict
:
return
redirect
(
"https://www."
+
yml_dict
[
path
])
return
redirect
(
yml_dict
[
path
])
else
:
return
make_response
(
404
)
return
abort
(
404
)
def
extract_data_from_yml
(
urls
)
->
dict
:
...
...
@@ -26,17 +45,36 @@ def extract_data_from_yml(urls) -> dict:
:return dict:
"""
final_dict
=
{}
with
open
(
urls
,
'r'
)
as
f
:
content
=
f
.
read
().
split
(
'
\n
'
)
with
open
(
urls
,
'r'
)
as
stream
:
try
:
final_dict
=
yaml
.
safe_load
(
stream
)
except
yaml
.
YAMLError
as
exc
:
print
(
exc
)
return
final_dict
content
=
content
[:
-
1
]
for
elem
in
content
:
final_dict
[
elem
.
split
(
':'
)[
0
]]
=
elem
.
split
(
':'
)[
1
]
return
final_dict
def
generate_short_url
()
->
str
:
rand_str
=
lambda
n
:
''
.
join
([
random
.
choice
(
string
.
ascii_lowercase
)
for
i
in
range
(
n
)])
return
rand_str
(
5
)
def
check_if_input_is_url
(
url
)
->
None
or
not
None
:
regex
=
re
.
compile
(
r
'^(?:http|ftp)s?://'
# http:// или https://
r
'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'
# домен
r
'localhost|'
# localhost...
r
'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
# ...иди ip
r
'(?::\d+)?'
# optional port
r
'(?:/?|[/?]\S+)$'
,
re
.
IGNORECASE
)
answer
=
re
.
match
(
regex
,
url
)
return
answer
# def myquote(url):
# parts = urlparse(url)
# return urlunparse(parts._replace(path=quote(parts.path)))
if
__name__
==
'__main__'
:
app
.
debug
(
True
)
app
.
run
()
static/urls.yml
View file @
9a247b88
yah444:yahoo.com
ram124:rambler.ru
ah712:yandex.ru
yah44
:
yahoo.com
ram24
:
rambler.ru
ah712
:
yandex.ru
kzgcz
:
https://studfiles.net/preview/6716220/
vktlm
:
https://www.oslogic.ru/knowledge/598/shpargalka-po-osnovnym-komandam-postgresql/
templates/index.html
View file @
9a247b88
...
...
@@ -5,11 +5,19 @@
<title>
Title
</title>
</head>
<body>
Введите ссылку в адресной строке
{#
<p><b>
Введите длинную ссылку:
</b><br>
#}
{#
<form
action=
"/add"
>
#}
{#
<input
type=
"text"
size=
"40"
>
#}
{#
<button>
ff
</button>
#}
{#
</form>
#}
<p><b>
Введите длинную ссылку:
</b><br>
<form
action=
"/add"
>
<input
type=
"text"
size=
"40"
name=
"url1"
>
<button>
Создать новое соответствие
</button>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul
class=
flashes
>
{% for message in messages %}
<li>
{{ message }}
</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
</form>
</body>
</html>
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment