├── .gitignore ├── LICENSE ├── README.md ├── app.py ├── requirements.txt └── templates └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # Installer logs 29 | pip-log.txt 30 | pip-delete-this-directory.txt 31 | 32 | # Unit test / coverage reports 33 | htmlcov/ 34 | .tox/ 35 | .nox/ 36 | .coverage 37 | .coverage.* 38 | .cache 39 | nosetests.xml 40 | coverage.xml 41 | *.cover 42 | .hypothesis/ 43 | 44 | # Translations 45 | *.mo 46 | *.pot 47 | 48 | # Django stuff: 49 | *.log 50 | local_settings.py 51 | db.sqlite3 52 | 53 | # Flask stuff: 54 | instance/ 55 | .webassets-cache 56 | 57 | # Sphinx documentation 58 | docs/_build/ 59 | doc/source/_build/ 60 | 61 | # PyBuilder 62 | target/ 63 | 64 | # Jupyter Notebook 65 | .ipynb_checkpoints 66 | 67 | # IPython 68 | profile_default/ 69 | ipython_config.py 70 | 71 | # pyenv 72 | .python-version 73 | 74 | # celery beat schedule file 75 | celerybeat-schedule 76 | celerybeat.pid 77 | 78 | # SageMath parsed files 79 | *.sage.py 80 | 81 | # dotenv 82 | .env 83 | .venv 84 | 85 | # virtualenv 86 | venv/ 87 | ENV/ 88 | env.bak/ 89 | env/ 90 | 91 | # Spyder project settings 92 | .spyderproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | 103 | # Pyre type checker 104 | .pyre/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # URLShortener 2 | 3 | URLShortener is a simple Python-based web application using Flask that shortens long URLs. 4 | 5 | ## Features 6 | 7 | - Input a long URL to get a shortened version. 8 | - Redirect from the shortened URL to the original URL. 9 | 10 | ## Installation 11 | 12 | 1. Clone the repository: 13 | ```bash 14 | git clone https://github.com/YOUR_USERNAME/URLShortener.git 15 | cd URLShortener 16 | ``` 17 | 18 | 2. Create a virtual environment and activate it: 19 | ```bash 20 | python -m venv venv 21 | source venv/bin/activate # On Windows use `venv\Scripts\activate` 22 | ``` 23 | 24 | 3. Install the required packages: 25 | ```bash 26 | pip install -r requirements.txt 27 | ``` 28 | 29 | 4. Run the application: 30 | ```bash 31 | python app.py 32 | ``` 33 | 34 | 5. Open your web browser and go to `http://127.0.0.1:5000`. 35 | 36 | ## License 37 | 38 | This project is licensed under the MIT License. 39 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request, redirect, render_template 2 | from flask_sqlalchemy import SQLAlchemy 3 | import string 4 | import random 5 | 6 | app = Flask(__name__) 7 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///urls.db' 8 | app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False 9 | db = SQLAlchemy(app) 10 | 11 | class URL(db.Model): 12 | id = db.Column(db.Integer, primary_key=True) 13 | original_url = db.Column(db.String(500), nullable=False) 14 | short_url = db.Column(db.String(10), unique=True, nullable=False) 15 | 16 | def __repr__(self): 17 | return f'' 18 | 19 | def generate_short_url(): 20 | characters = string.ascii_letters + string.digits 21 | while True: 22 | short_url = ''.join(random.choice(characters) for _ in range(6)) 23 | if not URL.query.filter_by(short_url=short_url).first(): 24 | return short_url 25 | 26 | @app.before_first_request 27 | def create_tables(): 28 | db.create_all() 29 | 30 | @app.route('/', methods=['GET', 'POST']) 31 | def index(): 32 | if request.method == 'POST': 33 | original_url = request.form['original_url'] 34 | if original_url: 35 | short_url = generate_short_url() 36 | new_url = URL(original_url=original_url, short_url=short_url) 37 | db.session.add(new_url) 38 | db.session.commit() 39 | return render_template('index.html', short_url=short_url) 40 | return render_template('index.html') 41 | 42 | @app.route('/') 43 | def redirect_to_url(short_url): 44 | url = URL.query.filter_by(short_url=short_url).first_or_404() 45 | return redirect(url.original_url) 46 | 47 | if __name__ == '__main__': 48 | app.run(debug=True) 49 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask 2 | Flask-SQLAlchemy 3 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | URL Shortener 7 | 8 | 47 | 48 | 49 |
50 |

URL Shortener

51 |
52 | 53 | 54 |
55 | {% if short_url %} 56 |
57 | Shortened URL: {{ request.host }}/{{ short_url }} 58 |
59 | {% endif %} 60 |
61 | 62 | 63 | --------------------------------------------------------------------------------