-
Notifications
You must be signed in to change notification settings - Fork 3
/
core.py
62 lines (49 loc) · 2.02 KB
/
core.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
import config
import re
import os
import random
def Namer() -> str:
"""generate a random name for us as file name to for url.
name will end with .html
Returns:
str: name of the file
"""
file_name = None
while not file_name and not os.path.exists(os.path.join(config.DIR, str(file_name))):
for i in range(1, random.randint(1, config.MAX_URL_LENGTH + 1)):
if not file_name:
file_name = ''
file_name += random.choice(config.CHARS)
return file_name+'.html' if file_name != 'index' else Namer()
def Make(link: str, title: str, desc: str, click: bool = False, name: str = None) -> str:
"""make the link director
Args:
link (str): the link to be shortend.
title (str): title in the site.
desc (str): description for each link page.
click (bool, optional): redirect on open the link or click to open it. Defaults to True.
name (str): uniq name for use as url
Returns:
str: return address of the link affter deploying out.
"""
if click:
path = os.path.join(config.DIR, config.CLICK_TO_OPEN)
else:
path = os.path.join(config.DIR, config.REDIRECT_ON_OPEN)
with open(os.path.join(config.DIR, config.BASE), 'r') as fli:
fli = fli.read()
with open(path, 'r') as theme:
fli = re.sub('\{\{.*content.*\}\}', theme.read(), fli)
fli = re.sub('\{\{.*link.*\}\}', '"' + link + '"', fli)
fli = re.sub('\{\{.*title.*\}\}', title, fli)
fli = re.sub('\{\{.*description.*\}\}', desc, fli)
if not os.path.exists(config.PUBLISH_DIR):
os.mkdir(config.PUBLISH_DIR)
if name:
with open(os.path.join(config.PUBLISH_DIR, name+'.html'), 'w') as fliw:
fliw.write(fli)
return fliw.name
else:
with open(os.path.join(config.PUBLISH_DIR, Namer()), 'w') as fliw:
fliw.write(fli)
return fliw.name