-
Notifications
You must be signed in to change notification settings - Fork 0
/
shorten.py
238 lines (178 loc) · 5.79 KB
/
shorten.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
#!/usr/bin/env python3
import re
import sys
import requests
from urllib.parse import quote
def download_table():
r = requests.get('http://www.unicode.org/Public/idna/latest/IdnaMappingTable.txt')
return r.text
def read_table():
# Source: http://www.unicode.org/Public/idna/latest/IdnaMappingTable.txt
with open('IdnaMappingTable.txt', 'rb') as f:
return f.read().decode('utf-8')
def int2unicode(i):
return chr(i)
def code2unicode(code):
return int2unicode(int(code, 16))
def parse_table(table):
lines = table.split('\n')
# strip header
lines = lines[11:]
entries = {}
for line in lines:
match = re.match(r'^([0-9A-F]{4,}(?:\.\.[0-9A-F]{4,})?)\s+;\s+([^;#]+?)\s+(?:;((?: [0-9A-F]{4,})+)\s+)?#.+$', line)
if match is None:
continue
parts = match.groups()
chars = parts[0]
state = parts[1].strip()
if state != 'mapped':
continue
if '..' in chars:
start, end = [int(char, 16) for char in chars.split('..')]
chars = []
for code in range(start, end + 1):
chars.append(int2unicode(code))
else:
chars = [code2unicode(chars)]
mapping = parts[2].strip().split(' ')
for char in chars:
entries[char] = ''.join([code2unicode(code) for code in mapping])
return entries
def create_lookup(parsed):
reverse = {}
for key, value in parsed.items():
if value not in reverse:
reverse[value] = []
reverse[value].append(key)
return reverse
def optimize(lookup):
# remove 1:1 mappings
optimized = {}
for long, shortcuts in lookup.items():
for shortcut in shortcuts:
if len(shortcut) < len(long):
if (long not in optimized) or (len(shortcut) < len(optimized[long][0])):
# shortcut is the first or shorter
optimized[long] = [shortcut]
elif len(shortcut) == len(optimized[long][0]):
# shortcut is not longer than the previously found one
optimized[long].append(shortcut)
return optimized
class Node:
def __init__(self, value):
self.value = value
self.id = None
def __hash__(self):
return hash(self.id)
def __eq__(self, other):
return self.id == other.id
def __str__(self):
return str((self.id, self.value))
def __repr__(self):
return self.__str__()
# Directed Acyclic Graph
class DAG:
def __init__(self):
self.nodes = []
self.edges_to = {}
self.edges_from = {}
def add_node(self, value=None):
node = Node(value)
self.nodes.append(node)
node.id = len(self.nodes) - 1
return node
def add_edge(self, node_from, node_to):
if node_from not in self.edges_from:
self.edges_from[node_from] = []
self.edges_from[node_from].append(node_to)
if node_to not in self.edges_to:
self.edges_to[node_to] = []
self.edges_to[node_to].append(node_from)
def predecessors(self, node):
if node in self.edges_to:
return self.edges_to[node][:]
else:
return []
def successors(self, node):
if node in self.edges_from:
return self.edges_from[node][:]
else:
return []
def shorten_optimal(lookup, string):
# create graph
g = DAG()
# insert starting point
start = g.add_node()
# insert all 'regular' nodes + edges
char_nodes = []
last = start
for char in string:
n = g.add_node(char)
char_nodes.append(n)
g.add_edge(last, n)
last = n
# insert destination point
end = g.add_node()
g.add_edge(last, end)
# insert all shortcuts
for long, shortcuts in lookup.items():
# select one of the possible shortest shortcuts
shortcut = shortcuts[0]
n = g.add_node(shortcut)
# check all occurrences
start_pos = 0
while True:
start_pos = string.find(long, start_pos)
if start_pos == -1:
break
start_node = char_nodes[start_pos]
end_node = char_nodes[start_pos + len(long) - 1]
for from_node in g.predecessors(start_node):
for to_node in g.successors(end_node):
g.add_edge(from_node, n)
g.add_edge(n, to_node)
start_pos += 1
# prepare
pred = {start: None}
dist = {start: 0}
# find shortest path
q = [start]
while len(q) > 0:
current = q.pop(0)
print(current, q)
for succ in g.successors(current):
distance = dist[current] + 1
if succ in dist:
if distance < dist[succ]:
dist[succ] = distance
pred[succ] = current
else:
dist[succ] = distance
pred[succ] = current
q.append(succ)
# reconstruct path
path = []
current = end
while current is not None:
path.insert(0, current)
current = pred[current]
return ''.join(n.value for n in path[1:-1])
def shorten_url(url):
table = read_table()
# table = download_table()
parsed = parse_table(table)
lookup = create_lookup(parsed)
optimized = optimize(lookup)
shortened = shorten_optimal(optimized, url)
print('[*] Shortened from {} to {} chars!'.format(len(url), len(shortened)))
print('[+] Result: ', shortened)
print('[+] Encoded:', quote(shortened))
def main():
if len(sys.argv) != 2:
print('Usage: {} <url>'.format(sys.argv[0]))
exit(1)
url = sys.argv[1]
shorten_url(url)
if __name__ == "__main__":
main()