├── Procfile
├── urls.db
├── requirements.txt
├── README.md
├── templates
└── home.html
└── app.py
/Procfile:
--------------------------------------------------------------------------------
1 | web: gunicorn app:app
--------------------------------------------------------------------------------
/urls.db:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/raunaqness/microlink/HEAD/urls.db
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | click==6.7
2 | Flask==0.12
3 | itsdangerous==0.24
4 | Jinja2==2.9.5
5 | MarkupSafe==0.23
6 | Werkzeug==0.11.15
7 | gunicorn==19.7.0
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Microlink
2 |
3 | A simple url-shortner made with 🐍 and lag 🙂
4 |
5 | Try it out at [http://nikka-ja.herokuapp.com/](http://nikka-ja.herokuapp.com/)
6 |
7 | ## License
8 |
9 | This project is licensed under the WTFPL (Do What the Fuck You Want To Public License) License - [WTFPL License](https://en.wikipedia.org/wiki/WTFPL)
10 |
11 |
--------------------------------------------------------------------------------
/templates/home.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
16 |
17 |
18 |
19 |
20 | {% if not short_url %}
21 |
37 | {% else %}
38 |
47 | {% endif %}
48 |
49 |
50 |
51 |
52 | © Raunaq Singh Ji (Pulbangash Waale), 2018
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, request, render_template, redirect
2 | from math import floor
3 | from sqlite3 import OperationalError
4 | import string
5 | import sqlite3
6 | try:
7 | from urllib.parse import urlparse # Python 3
8 | str_encode = str.encode
9 | except ImportError:
10 | from urlparse import urlparse # Python 2
11 | str_encode = str
12 | try:
13 | from string import ascii_lowercase
14 | from string import ascii_uppercase
15 | except ImportError:
16 | from string import lowercase as ascii_lowercase
17 | from string import uppercase as ascii_uppercase
18 | import base64
19 | import hashlib
20 |
21 | app = Flask(__name__)
22 |
23 | host = 'http://localhost:5000/'
24 |
25 | def shorten_url(url):
26 | '''
27 | Yaha Algorithm lag raha hai, kisi ko btana mat
28 | '''
29 | salt = "swaad anusaar"
30 |
31 | url = url.decode('utf-8')
32 | m = hashlib.md5()
33 | s = (url + salt).encode('utf-8')
34 |
35 | m.update(s)
36 |
37 | final_id = m.hexdigest()[-6:].replace('=', '').replace('/', '_')
38 | return(final_id)
39 |
40 | def add_to_db(url):
41 | '''
42 | Yaha Database me entry hogi
43 | '''
44 |
45 | original_url = url
46 | shortened_url = shorten_url(original_url)
47 |
48 | try:
49 | with sqlite3.connect('urls.db') as conn:
50 | cursor = conn.cursor()
51 | res = cursor.execute(
52 | "INSERT INTO WEB_URL (ID, URL) VALUES (?, ?)",
53 | (shortened_url, original_url)
54 | )
55 | _lastrowid = (res.lastrowid)
56 | except:
57 | pass
58 |
59 | return(shortened_url)
60 |
61 | def table_check():
62 | create_table = """
63 | CREATE TABLE WEB_URL(
64 | ID TEXT PRIMARY KEY,
65 | URL TEXT
66 | );
67 | """
68 | # Checks if table exists, else creates it
69 | with sqlite3.connect('urls.db') as conn:
70 | cursor = conn.cursor()
71 | try:
72 | cursor.execute(create_table)
73 | except OperationalError:
74 | pass
75 |
76 |
77 | @app.route('/', methods=['GET', 'POST'])
78 | def home():
79 | if request.method == 'POST':
80 | original_url = str_encode(request.form.get('url'))
81 | if urlparse(original_url).scheme == '':
82 | url = 'http://' + original_url
83 | else:
84 | url = original_url
85 | encoded_string = add_to_db(url)
86 | return render_template('home.html', short_url= encoded_string)
87 | return render_template('home.html')
88 |
89 |
90 | @app.route('/')
91 | def redirect_short_url(short_url):
92 | if(short_url is not ""):
93 | decoded = short_url
94 | url = '/' # fallback if no URL is found
95 | with sqlite3.connect('urls.db') as conn:
96 | cursor = conn.cursor()
97 | res = cursor.execute('SELECT URL FROM WEB_URL WHERE ID=?', [decoded])
98 | try:
99 | short = res.fetchone()
100 | if short is not None:
101 | url = short[0]
102 | except Exception as e:
103 | print(e)
104 | return redirect(url)
105 |
106 |
107 | if __name__ == '__main__':
108 | table_check()
109 | app.run(debug=True)
110 |
--------------------------------------------------------------------------------