This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfrontend.py
executable file
·100 lines (78 loc) · 2.91 KB
/
frontend.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
import json
from django.utils.encoding import smart_str
from jinja2 import Template
import commonregex
parser = commonregex.CommonRegex()
def fixnewlines(message):
return message.replace('\n', ' <br> ')
# def shortify_string(message):
# message = message.split(" ")
# new_message = ""
# for mess in message :
# if len(mess) > 25 :
# mess = " <a href='" + mess + "' target='_blank'> " + mess[0:25] + "... </a> "
# new_message = new_message + mess + " "
# return new_message
def truncate(message,length):
while length < len(message):
if message[length] == " ":
return message[0:length]
length = length+1
return message[0:len(message)-1]
def truncate_length(message,length):
while length < len(message):
if message[length] == " ":
if length == len(message)-1:
return None
else:
return 1
length = length+1
return None
def enable_links(message):
if '<video width="320" height="240" controls>' in message :
return message
links = parser.links(message)
links = list(set(links))
url_identifier = ["www","http","bit.ly",".com",".co.in"]
for link in links:
flag = 0
for keyword in url_identifier :
if keyword in link :
flag = 1
break
if flag is 0 :
break
http_link = link
if not link.startswith('http'):
http_link = 'http://{}'.format(link)
if len(link) < 25:
link = link[0:25]
message = message.replace(link, ' <a href=\'{}\' target=\'_blank\'> {} </a> '.format(http_link, link) , 1)
else:
# message = shortify_string(message)
message = message.replace(link, ' <a href=\'{}\' target=\'_blank\'> {} </a> '.format(http_link, link[0:25]+'...') ,1 )
return message
def get_html(data):
template_raw = open('feed.tmpl', 'r').read()
for post in data:
if 'message' in post:
if (type(post['message']) is str):
post['message'] = fixnewlines(post['message'])
if 'flag' not in post :
post['message'] = enable_links(post['message'])
post['flag'] = 1
post['message'] = post['message'].replace("\"","'")
post['short_message'] = truncate(post['message'],150)
post['read_more'] = truncate_length(post['message'],150)
json.dump(data, open('docs/feed.json', 'w'))
template = Template(template_raw)
html = template.render(data=data)
# smart_str helps in unicode rendering
return smart_str(html)
def write_html(data, file_name):
html = get_html(data)
file_handle = open(file_name, 'w', encoding="utf8")
file_handle.write(html)
if __name__ == "__main__":
data = json.load(open('docs/feed.json', 'r'))
write_html(data, 'docs/index.html')