Use ua-parser for better results

This commit is contained in:
Mathias DUPEUX 2024-12-30 16:48:02 +01:00
parent 06d7ab80e9
commit 6aef5ed35e
2 changed files with 8 additions and 12 deletions

View file

@ -1,11 +1,11 @@
import psycopg2
from psycopg2 import OperationalError
import time import time
from typing import Optional, Any
import logging import logging
import psycopg2
from os import getenv from os import getenv
import httpagentparser
from dotenv import load_dotenv from dotenv import load_dotenv
from typing import Optional, Any
from psycopg2 import OperationalError
from ua_parser import parse_os, parse_user_agent
load_dotenv() load_dotenv()
@ -153,12 +153,7 @@ if isInit:
def log(ip:str, useragent:str, day:int) -> bool: def log(ip:str, useragent:str, day:int) -> bool:
if not isInit: return True if not isInit: return True
ua_data = httpagentparser.detect(useragent) os = parse_os(useragent).family
os = ua_data.get('os', {}).get("name", "unknown") browser = parse_user_agent(useragent).family
browser = ua_data.get('browser', {}).get("name", "unknown")
success = db.execute_query(f"INSERT INTO cinema_queries (ip, time, browser, os, day) VALUES (\'{ip}\', current_timestamp, \'{browser}\', \'{os}\', {day});") success = db.execute_query(f"INSERT INTO cinema_queries (ip, time, browser, os, day) VALUES (\'{ip}\', current_timestamp, \'{browser}\', \'{os}\', {day});")
return success return success
if __name__ == "__main__":
ua ="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0"
print(httpagentparser.detect(ua))

View file

@ -2,3 +2,4 @@ Flask
psycopg2 psycopg2
requests requests
python-dotenv python-dotenv
ua-parser[regex]