diff options
author | Parker <contact@pkrm.dev> | 2024-02-25 02:15:31 -0600 |
---|---|---|
committer | Parker <contact@pkrm.dev> | 2024-02-25 02:15:31 -0600 |
commit | 0ea4abca33363f0bbdffa181b60beefc247774fa (patch) | |
tree | 0a6dcd17d5a6d7d9fdb1395548fb33cedf559f2c /app/func/log.py | |
parent | f3ff78bc8db5d8e7938407a62d56410cc72ce3a7 (diff) |
Creation
Diffstat (limited to 'app/func/log.py')
-rw-r--r-- | app/func/log.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/app/func/log.py b/app/func/log.py new file mode 100644 index 0000000..0a7dddc --- /dev/null +++ b/app/func/log.py @@ -0,0 +1,74 @@ +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, request): + 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(request.remote_addr) + 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() + ip = request.remote_addr + user_agent = request.user_agent.string + 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 |