aboutsummaryrefslogtreecommitdiff
path: root/app/util/log.py
blob: 2b3542bae318907dfc2c37853604db23bed24772 (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
import ip2locationio
import datetime
from ua_parser import user_agent_parser
from ip2locationio.ipgeolocation import IP2LocationIOAPIError

from database import SessionLocal
from var import LOG, API_KEY, IP_TO_LOCATION
from models import Link, Record

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):
    db = SessionLocal()

    # Get the redirect link and owner of the link
    redirect_link, owner = (
        db.query(Link.redirect_link, Link.owner)
        .filter(Link.link == link)
        .first()
    )

    if IP_TO_LOCATION:
        # 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:
            LOG.error(
                "Invalid API key or insufficient credits. Change the"
                " `config.ini` file if you no longer need IP geolocation."
            )
            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"]}'

    # Create the log record and commit it to the database
    link_record = Record(
        owner=owner,
        link=link,
        timestamp=timestamp,
        ip=ip,
        location=location,
        browser=browser,
        os=os,
        user_agent=user_agent,
        isp=isp,
    )
    db.add(link_record)
    db.commit()
    db.close()

    # Return the redirect link in order to properly redirect the user
    return redirect_link