├── README.md
├── urls
├── templates
└── index.html
├── todo
├── .gitignore
└── app.py
/README.md:
--------------------------------------------------------------------------------
1 | flask_hh
2 | ========
3 |
4 | Simple URL Shortener that I built during a Flask hacker hour.
5 |
--------------------------------------------------------------------------------
/urls:
--------------------------------------------------------------------------------
1 | http://google.com|ZKMBQW
2 | http://usacs.rutgers.edu|UBDMCX
3 | http://usacs.rutgers.edu|GVVJQK
4 | http://usacs.rutgers.edu|DVPXAZ
5 | http://sniper.vverma.net/|DFIOGN
6 | http://google.com|CBGSZC
7 |
--------------------------------------------------------------------------------
/templates/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Simple URL Shortener
4 |
5 |
6 | I shorten URLs for you
7 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/todo:
--------------------------------------------------------------------------------
1 | The only way to learn web dev is to do it.
2 | - 100k lines of code.
3 |
4 | - Installing Python and Flask. - Easier on Macs and Linux, but doable on Windows.
5 | - Try to write exactly what I wrote from scratch.
6 | - Come to our meeting on Monday -> MOAR PIZZA -> and do this there.
7 | - If you get stuck, ask people questions about how to get unstuck.
8 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.py[cod]
2 |
3 | # C extensions
4 | *.so
5 |
6 | # Packages
7 | *.egg
8 | *.egg-info
9 | dist
10 | build
11 | eggs
12 | parts
13 | bin
14 | var
15 | sdist
16 | develop-eggs
17 | .installed.cfg
18 | lib
19 | lib64
20 | __pycache__
21 |
22 | # Installer logs
23 | pip-log.txt
24 |
25 | # Unit test / coverage reports
26 | .coverage
27 | .tox
28 | nosetests.xml
29 |
30 | # Translations
31 | *.mo
32 |
33 | # Mr Developer
34 | .mr.developer.cfg
35 | .project
36 | .pydevproject
37 |
--------------------------------------------------------------------------------
/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, render_template, request, redirect
2 | import string, random
3 |
4 | app = Flask(__name__)
5 |
6 | def random_string():
7 | length = 6
8 | rv = ""
9 |
10 | for i in range(length):
11 | rv += random.choice(string.ascii_uppercase)
12 |
13 | return rv
14 |
15 |
16 | @app.route('/')
17 | def home():
18 | if 'url' in request.args:
19 | url = request.args['url']
20 |
21 | shortened_url_code = random_string()
22 |
23 | with open('urls', 'a') as handle:
24 | handle.write(url+'|'+shortened_url_code+"\n")
25 |
26 | handle.close()
27 |
28 | shortened_url = 'http://localhost:5000/short?code='+shortened_url_code
29 |
30 | return 'I received '+url+' as input. Here is your shortened URL '+shortened_url
31 | else:
32 | return render_template('index.html')
33 |
34 | @app.route('/short')
35 | def short():
36 | if 'code' in request.args:
37 | code = request.args['code']
38 |
39 | with open('urls') as handle:
40 | for line in handle.readlines():
41 | long_url, short = line.strip().split('|')
42 |
43 | if short == code:
44 | return redirect(long_url)
45 | return 'Your code was not found'
46 |
47 |
48 | else:
49 | return 'Bad request'
50 |
51 |
52 | if __name__ == '__main__':
53 | app.run('0.0.0.0', debug=True)
54 |
--------------------------------------------------------------------------------