aboutsummaryrefslogtreecommitdiff
path: root/app/func/log.py
blob: da6594ae3bcd84726235fc92848fc48ed260a3e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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