This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Main file for performing url shortening""" | |
from aiohttp import web | |
# instead of adding routes one by one we use the RouteTtableDef which will auto collect all @route annotations | |
routes = web.RouteTableDef() | |
# this will create a route 127.0.0.1:8000/shorten | |
@routes.get("/shorten") | |
async def shorten(request): | |
"""Function to accept HTTP request with long url & call shortening logic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aiohttp import web | |
from http import HTTPStatus | |
import validators | |
import requests | |
def validate_url_format(longurl: str): | |
"""This function returns True if the longurl is in a valid format""" | |
return validators.url(longurl) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import json | |
import string | |
# get the ngrok tunnel url | |
def get_ngrok_url(): | |
"""Function to get ngrok url""" | |
url = "http://localhost:4040/api/tunnels" | |
res = requests.get(url) | |
res_unicode = res.content.decode("utf-8") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
from aiohttp import web | |
# create a sqllite db | |
connection = sqlite3.connect("urlshortner.db") | |
cursor = connection.cursor() | |
routes = web.RouteTableDef() | |
if __name__ == "__main__": |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def validate_url_format(longurl: str): | |
"""This function returns True if the longurl is in a valid format""" | |
return validators.url(longurl) | |
def validate_url(longurl: str): | |
"""This function returns True if the longurl is a valid web address""" | |
try: | |
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'} | |
response = requests.get(longurl, headers=headers) | |
return response.status_code == HTTPStatus.OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@routes.get("/{code}") | |
async def redirect_to_longurl(request): | |
"""Function to retrieve long URL based on code | |
:param request: HTTP request | |
""" | |
data = request.match_info["code"] | |
longurl = cursor.execute("SELECT longurl from URLSHORTNER where code = ?",(data,)).fetchone() | |
print(longurl[0]) | |
return web.HTTPFound(longurl[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from email import header | |
from aiohttp import web | |
import sqlite3 | |
import validators | |
import requests | |
import requests.exceptions | |
from http import HTTPStatus | |
import json | |
import string | |
import random |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, Response, jsonify | |
from flask_sqlalchemy import SQLAlchemy | |
app = Flask(__name__) | |
db = SQLAlchemy(app) | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' | |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | |
class Todo(db.Model): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from turtle import * | |
import turtle as tur | |
t = tur.Turtle() | |
tur.speed(6) | |
tur.bgcolor("black") | |
tur.color("gold") | |
tur.pensize(5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import logging | |
import os | |
import subprocess | |
import sys | |
import time | |
import urllib | |
from logging import getLogger | |
from dotenv import load_dotenv |
OlderNewer