Commit 8fff8fe3 authored by Sylvain Coulange's avatar Sylvain Coulange
Browse files

ajout appstat agent country

parent 12faee28
......@@ -602,12 +602,14 @@ def writeLog(Type,user,mot,oldMot,newMot,lang):
print("New log: lang={} ; user={} ; word={} ; type={}".format(lang,user,mot,Type))
return noIdDict(newlog)
def addStat(app, module, ip, lang):
def addStat(app, module, ip, agent, country, lang):
newStat = {
'date':datetime.datetime.now(),
'app': app,
'module': module,
'ip': ip,
'agent': agent,
'country': country,
'lang': lang
}
if ip!="127.0.0.1": users_appstats.insert_one(newStat)
\ No newline at end of file
users_appstats.insert_one(newStat)
\ No newline at end of file
......@@ -4,7 +4,8 @@ from .models import Phonograph, Data, Entree, commitData
from django.http import JsonResponse
from .liaisons import *
from .english_spacyExceptions import getOneWordList
import json, spacy, subprocess, re
import json, spacy, subprocess, re, requests
from user_agents import parse
print('Chargement des modèles de langue...')
print('fr_core_news_md...')
......@@ -348,12 +349,28 @@ def getLog(request):
return JsonResponse({"logs":rep})
def addStat(request):
colis = json.loads(request.body)
app = colis["app"]
module = colis["module"]
ip = get_client_ip(request)
lang = colis["lang"]
txtphono.addStat(app, module, ip, lang)
if get_client_ip(request)!="127.0.0.1":
colis = json.loads(request.body)
app = colis["app"]
module = colis["module"]
ip = get_client_ip(request)
agent = parse(request.META['HTTP_USER_AGENT'])
country = getCountry(get_client_ip(request))
lang = colis["lang"]
txtphono.addStat(app, module, ip, agent, country, lang)
return JsonResponse({})
def getCountry(ip):
endpoint = f'https://ipinfo.io/{ip}/json'
response = requests.get(endpoint, verify = True)
if response.status_code != 200:
return 'Status:', response.status_code, 'Problem with the request. Exiting.'
exit()
return JsonResponse({})
\ No newline at end of file
data = response.json()
print(data)
return data['country']
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment