diff options
Diffstat (limited to 'app/func')
-rw-r--r-- | app/func/generate_api_key.py | 23 | ||||
-rw-r--r-- | app/func/link/delete.py | 20 | ||||
-rw-r--r-- | app/func/link/delrecords.py | 20 | ||||
-rw-r--r-- | app/func/link/records.py | 23 | ||||
-rw-r--r-- | app/func/link/renew.py | 23 | ||||
-rw-r--r-- | app/func/log.py | 72 | ||||
-rw-r--r-- | app/func/newlink.py | 30 | ||||
-rw-r--r-- | app/func/remove_old_data.py | 24 |
8 files changed, 0 insertions, 235 deletions
diff --git a/app/func/generate_api_key.py b/app/func/generate_api_key.py deleted file mode 100644 index a40c96a..0000000 --- a/app/func/generate_api_key.py +++ /dev/null @@ -1,23 +0,0 @@ -import sqlalchemy -from sqlalchemy import exc -import random -import string - -from db import engine - -""" -Generate and return a randomized API key string for the user -Keys are composed of 20 uppercase ASCII characters -""" -def generate_api_key(): - with engine.begin() as conn: - while True: - try: - api_key_string = ''.join(random.choices(string.ascii_uppercase, k=20)) - conn.execute(sqlalchemy.text('INSERT INTO keys(api_key) VALUES(:api_key)'), [{'api_key': api_key_string}]) - conn.commit() - break - except exc.IntegrityError: - continue - - return api_key_string
\ No newline at end of file diff --git a/app/func/link/delete.py b/app/func/link/delete.py deleted file mode 100644 index 97b696e..0000000 --- a/app/func/link/delete.py +++ /dev/null @@ -1,20 +0,0 @@ -import sqlalchemy - -from db import engine - -""" -Delete the specified link from the users associated links -""" -def delete_link(link, owner): - with engine.begin() as conn: - try: - link_owner = conn.execute(sqlalchemy.text('SELECT owner FROM links WHERE link = :link'), [{'link': link}]).fetchone()[0] - except TypeError: - return 404 - - if owner == link_owner: - with engine.begin() as conn: - conn.execute(sqlalchemy.text('DELETE FROM links WHERE link = :link'), [{'link': link}]) - return link - else: - return 401
\ No newline at end of file diff --git a/app/func/link/delrecords.py b/app/func/link/delrecords.py deleted file mode 100644 index d82bfa5..0000000 --- a/app/func/link/delrecords.py +++ /dev/null @@ -1,20 +0,0 @@ -import sqlalchemy - -from db import engine - -""" -Delete all of the IP log records that are associated with a specific link -""" -def delete_link_records(link, owner): - with engine.begin() as conn: - try: - link_owner = conn.execute(sqlalchemy.text('SELECT owner FROM links WHERE link = :link'), [{'link': link}]).fetchone()[0] - except TypeError: - return 404 - - if owner == link_owner: - with engine.begin() as conn: - conn.execute(sqlalchemy.text('DELETE FROM records WHERE link = :link'), [{'link': link}]) - return link - else: - return 401
\ No newline at end of file diff --git a/app/func/link/records.py b/app/func/link/records.py deleted file mode 100644 index 56bb6d2..0000000 --- a/app/func/link/records.py +++ /dev/null @@ -1,23 +0,0 @@ -import sqlalchemy - -from db import engine - -""" -Retrieve all records associated with a specific link -""" -def get_link_records(link, owner): - with engine.begin() as conn: - try: - link_owner = conn.execute(sqlalchemy.text('SELECT owner FROM links WHERE link = :link'), [{'link': link}]).fetchone()[0] - except TypeError: - return 404 - - if owner == link_owner: - with engine.begin() as conn: - records = conn.execute(sqlalchemy.text('SELECT timestamp, ip, location, browser, os, user_agent, isp FROM records WHERE owner = :owner and link = :link'), [{'owner': owner, 'link': link}]).fetchall() - if not records: - return 204 - else: - return 401 - - return records
\ No newline at end of file diff --git a/app/func/link/renew.py b/app/func/link/renew.py deleted file mode 100644 index bcf0550..0000000 --- a/app/func/link/renew.py +++ /dev/null @@ -1,23 +0,0 @@ -import sqlalchemy -import datetime - -from db import engine - -""" -Renew a specified link so that the user can continue logging through that URL -Adds 7 days from the current date -""" -def renew_link(link, owner): - with engine.begin() as conn: - try: - link_owner = conn.execute(sqlalchemy.text('SELECT owner FROM links WHERE link = :link'), [{'link': link}]).fetchone()[0] - except TypeError: - return 404 - - if owner == link_owner: - with engine.begin() as conn: - expire_date = datetime.datetime.date(datetime.datetime.now()) + datetime.timedelta(days=7) - conn.execute(sqlalchemy.text('UPDATE links SET expire_date = :expire_date WHERE link = :link'), [{'expire_date': expire_date, 'link': link}]) - return link, expire_date - else: - return 401
\ No newline at end of file diff --git a/app/func/log.py b/app/func/log.py deleted file mode 100644 index da6594a..0000000 --- a/app/func/log.py +++ /dev/null @@ -1,72 +0,0 @@ -import ip2locationio -import sqlalchemy -import datetime -import validators -from ua_parser import user_agent_parser -from dotenv import load_dotenv -import os -from ip2locationio.ipgeolocation import IP2LocationIOAPIError - -from db import engine - -load_dotenv() -try: - ip_to_location = os.getenv('IP_TO_LOCATION').upper().replace('"', '') - if ip_to_location == 'TRUE': - api_key = os.getenv('API_KEY').replace('"', '') - else: - api_key = "NO_API_KEY" - - base_url = os.getenv('BASE_URL').replace('"', '') -# .env File does not exist - likely a docker run -except AttributeError: - ip_to_location = str(os.environ['IP_TO_LOCATION']).upper().replace('"', '') - if ip_to_location == 'TRUE': - api_key = str(os.environ('API_KEY')).replace('"', '') - else: - api_key = "NO_API_KEY" - - base_url = str(os.environ('BASE_URL')).replace('"', '') - -if not validators.url(base_url): - print(base_url) - print('BASE_URL varaible is malformed.') - exit() - -configuration = ip2locationio.Configuration(api_key) -ipgeolocation = ip2locationio.IPGeolocation(configuration) - -""" -Create a new log record whenever a link is visited -""" -def log(link, ip, user_agent): - with engine.begin() as conn: - try: - redirect_link, owner = conn.execute(sqlalchemy.text('SELECT redirect_link, owner FROM links WHERE link = :link'), [{'link': link}]).fetchone() - except TypeError: - return base_url - - with engine.begin() as conn: - if ip_to_location == 'TRUE': - # Get IP to GEO via IP2Location.io - try: - data = ipgeolocation.lookup(ip) - location = f'{data["country_name"]}, {data["city_name"]}' - isp = data['as'] - # Fatal error, API key is invalid or out of requests, quit - except IP2LocationIOAPIError: - print('Invalid API key or insifficient credit. Change .env file if you do not need IP to location feature.') - location = '-, -' - isp = '-' - else: - location = '-, -' - isp = '-' - - timestamp = datetime.datetime.now() - ua_string = user_agent_parser.Parse(user_agent) - browser = ua_string['user_agent']['family'] - os = f'{ua_string["os"]["family"]} {ua_string["os"]["major"]}' - - conn.execute(sqlalchemy.text('INSERT INTO records (owner, link, timestamp, ip, location, browser, os, user_agent, isp) VALUES (:owner, :link, :timestamp, :ip, :location, :browser, :os, :user_agent, :isp)'), [{'owner': owner, 'link': link, 'timestamp': timestamp, 'ip': ip, 'location': location, 'browser': browser, 'os': os, 'user_agent': user_agent, 'isp': isp}]) - - return redirect_link
\ No newline at end of file diff --git a/app/func/newlink.py b/app/func/newlink.py deleted file mode 100644 index 06776e6..0000000 --- a/app/func/newlink.py +++ /dev/null @@ -1,30 +0,0 @@ -import validators -import random -import string -import datetime -import sqlalchemy -from sqlalchemy import exc - -from db import engine - -""" -Generate and return a new randomized link that is connected to the user -Links are composed of 5 uppercase ASCII characters + numbers -""" -def generate_link(redirect_link, owner): - if not validators.url(redirect_link): - return 422 - - with engine.begin() as conn: - choices = string.ascii_uppercase + '1234567890' - while True: - try: - link = ''.join(random.choices(choices, k=5)) - expire_date = datetime.datetime.date(datetime.datetime.now()) + datetime.timedelta(days=7) - conn.execute(sqlalchemy.text('INSERT INTO links(owner, link, redirect_link, expire_date) VALUES (:owner, :link, :redirect_link, :expire_date)'), [{'owner': owner, 'link': link, 'redirect_link': redirect_link, 'expire_date': expire_date}]) - conn.commit() - break - except exc.IntegrityError: - continue - - return link, expire_date diff --git a/app/func/remove_old_data.py b/app/func/remove_old_data.py deleted file mode 100644 index 96d08fa..0000000 --- a/app/func/remove_old_data.py +++ /dev/null @@ -1,24 +0,0 @@ -import sqlalchemy -import datetime - -from db import engine - -""" -Remove all links and associated records when the expire date has passed -""" -def remove_old_data(): - with engine.begin() as conn: - today = datetime.datetime.date(datetime.datetime.now()) - old_links = conn.execute(sqlalchemy.text('SELECT link FROM links WHERE expire_date < :today'), [{'today': today}]) - - delete_links = [] - - for row in old_links: - link = row.link - delete_links.append({'link': link}) - - if delete_links: - with engine.begin() as conn: - conn.execute(sqlalchemy.text('DELETE FROM links WHERE link = :link'), delete_links) - conn.execute(sqlalchemy.text('DELETE FROM records WHERE link = :link'), delete_links) - conn.commit() |