The personal URL shortener
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.1 KiB

#!/usr/bin/env python3
import sys
import typing
import argparse
import logging
import pathlib
import asyncio
import time
from import Data
from shortenit.pointer import Pointer
from shortenit.config import Config
from shortenit.shortener import Shortener
from shortenit.db import DB
from shortenit.logger import setup_logging
from shortenit.web import Web, SiteHandler
from shortenit.exceptions import DBConnectionFailed
PROJECT_ROOT = pathlib.Path(__file__).parent.parent
CONFIGURATION = f'{PROJECT_ROOT}/config/config.yaml'
# Setup logging
logger = logging.getLogger(__name__)
def main() -> typing.NoReturn:
Main method
parser = argument_parse()
args = parser.parse_args()
debug = True if args.verbose > 0 else False
verbosity_level = verbosity(args.verbose)
setup_logging(args.logger, verbosity_level)
config = Config(CONFIGURATION).get_config()
db_config = config.get('CouchDB', None)
server_config = config.get('Server', None)
if db_config:
with DB(db_config) as db:
handler = SiteHandler(config, db, shorten_url, lenghten_url)
web = Web(handler, debug=debug) = server_config.get('host', None)
web.port = server_config.get('port', None)
except DBConnectionFailed as e:
def shorten_url(configuration: dict, database: DB,
data: str, ttl):
shortener = Shortener(database.pointers_db,
configuration.get('Shortener', None))
identifier = shortener.get_id()
if identifier:
_data = Data(database.data_db,
pointer = Pointer(database.pointers_db, identifier)
pointer.generate_pointer(_data.identifier, ttl)
return pointer.identifier
return None
def lenghten_url(database: DB, identifier: str):
pointer = Pointer(database.pointers_db)
data = Data(database.data_db, identifier=pointer.data_hash)
def argument_parse() -> argparse.ArgumentParser:
Method to extract the arguments from the command line.
:returns: The argument parser.
parser = argparse.ArgumentParser(
description="Generates rundeck resources "
"file from different API sources.")
'-v', '--verbose', action='count', default=0,
help='Verbosity level to use.')
'-l', '--logger', type=str,
help='The logger YAML configuration file.')
return parser
def verbosity(verbose: int):
Method to set the verbosity.
:param verbose: The verbosity set by user.
:returns: The verbosity level.
if verbose == 0:
return logging.ERROR
elif verbose == 1:
return logging.WARNING
elif verbose == 2:
return logging.INFO
elif verbose > 2:
return logging.DEBUG
if __name__ == '__main__':