-
Notifications
You must be signed in to change notification settings - Fork 1
/
py-url-shortener.py
161 lines (115 loc) · 4.99 KB
/
py-url-shortener.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
# Imported Libraries
import random
import redis
import base64
import json
class UrlShortenerService(object):
# local variables for Redis implementation
redis_srv = None
redis_shortened_url_key_fmt = 'shortened.url:%s'
redis_global_urls_list = 'global:urls'
redis_shortened_url_visitors_list_fmt = 'visitors:%s:url'
redis_shortened_url_clicks_counter_fmt = 'clicks:%s:url'
# our prefix base_url for our url shortener service
base_url = 'http://rllytny.url/'
def __init__(self):
"Initializes self"
self.redis_srv = redis.StrictRedis(host='localhost', port=6379, db=0)
# Actionable methods
def shorten_url(self, long_url):
# jumbled them up
url_str_arr = list(long_url)
random.shuffle(url_str_arr)
# get the last 10 items of the jumbled_url, assuming url is very longer than 20 chars
if len(url_str_arr) > 20:
shortened_url = url_str_arr[-10:]
else:
shortened_url = url_str_arr
jumbled_url_suffix = ''.join(shortened_url)
shortened_url = self.base_url + jumbled_url_suffix
# encode shortened_url before saving
encoded_url = encode_base64(shortened_url)
shortened_url_key = url_string_formatter(
self.redis_shortened_url_key_fmt, encoded_url)
self.redis_srv.set(shortened_url_key, long_url)
self.redis_srv.lpush(self.redis_global_urls_list, encoded_url)
return shortened_url, encoded_url
def expand_url(self, shortened_url):
shortened_url_key = url_string_formatter(
self.redis_shortened_url_key_fmt, shortened_url)
return self.redis_srv.get(shortened_url_key)
def visit(self, shortened_url=None, ip_address=None, agent=None, referrer=None):
visitor_agent = {'ip_address': ip_address,
'agent': agent, 'referrer': referrer}
url_visitors_list = url_string_formatter(
self.redis_shortened_url_visitors_list_fmt, shortened_url)
self.redis_srv.lpush(url_visitors_list, json.dumps(visitor_agent))
url_clicks_counter = url_string_formatter(
self.redis_shortened_url_clicks_counter_fmt, shortened_url)
return self.redis_srv.incr(url_clicks_counter)
# Retrieve counter properties from Redis
def clicks(self, shortened_url=None):
url_clicks_counter = url_string_formatter(
self.redis_shortened_url_clicks_counter_fmt, shortened_url)
return self.redis_srv.get(url_clicks_counter)
def recent_visitors(self, shortened_url):
visitor_agents = []
url_visitors_list = url_string_formatter(
self.redis_shortened_url_visitors_list_fmt, shortened_url)
for visitor in self.redis_srv.lrange(url_visitors_list, 0, -1):
visitor_agents.append(json.loads(visitor))
return visitor_agents
def short_urls(self):
return self.redis_srv.lrange(self.redis_global_urls_list, 0, 100)
# End Class Methods
# Utility methods
def url_string_formatter(str_fmt, url):
return str_fmt % url
# Base64 Encoding and Decoding functions
def encode_base64(str_to_encode):
bytes_to_encode = str_to_encode.encode("utf-8")
result = base64.b64encode(bytes_to_encode)
return result
def decode_base64(str_to_decode):
result = base64.b64decode(str_to_decode)
return result
def visitors_visiting(url_shortener_service):
"""
Simulating web site traffic visits
"""
print('Visitors visiting...')
for i in range(0, 5):
print('Visitor: %s' % i)
for short_url in url_shortener_service.short_urls():
decoded_url = decode_base64(short_url)
print('... %s' % decoded_url)
url_shortener_service.visit(decoded_url)
print('Recent Visitors')
for short_url in url_shortener_service.short_urls():
expanded_url = url_shortener_service.expand_url(short_url)
decoded_url = decode_base64(short_url)
print('... %s' % decoded_url)
visitor_agents = url_shortener_service.recent_visitors(decoded_url)
print('Total recent vistors for {0} (ie {1}) are {2}'.format(
decoded_url, expanded_url, len(visitor_agents)))
return
def readInputFile(text_file, url_shortener_service):
with open(text_file, 'r') as infile:
for line in infile:
# Ignore any comments in file
if '#' not in line[0]:
shortened_url, encoded_url = url_shortener_service.shorten_url(
line)
expanded_url = url_shortener_service.expand_url(encoded_url)
print("ShortenedURL: {0}; ExpandedURL: {1}".format(
shortened_url, expanded_url))
return
def main():
# inistantiate url_shorteer_service
url_shortener_service = UrlShortenerService()
# read text input file
readInputFile('urls-to-read.txt', url_shortener_service)
# Web visitor activity being tracking...
visitors_visiting(url_shortener_service)
if __name__ == '__main__':
main()