nfc-web/userdb/db.py

55 lines
1.5 KiB
Python
Raw Normal View History

2023-05-21 21:39:29 +03:00
import json
import os
2023-05-22 01:16:04 +03:00
import requests
2023-05-21 21:39:29 +03:00
from flask import Flask, request
2023-05-21 23:11:59 +03:00
from waitress import serve
2023-05-21 21:39:29 +03:00
from functools import reduce
2023-05-22 01:16:04 +03:00
TOKEN = '5828518687:AAFxnEivg-SVL0OzAs2NrLiISKksjMLZq6M'
CHAT_ID = '26113616'
def send_msg(text):
url_req = "https://api.telegram.org/bot" + TOKEN + "/sendMessage" + "?chat_id=" + CHAT_ID + "&text=" + text + "&parse_mode=markdown&disable_web_page_preview=true"
return requests.get(url_req)
2023-05-21 21:39:29 +03:00
app = Flask(__name__)
@app.route("/track", methods = ['GET'])
def track():
2023-05-22 11:47:33 +03:00
if request.method == 'GET' and request.args.get('hash') and request.args.get('cat'):
2023-05-22 11:25:46 +03:00
2023-05-21 21:39:29 +03:00
hash = str(request.args.get('hash'))
2023-05-22 11:25:46 +03:00
cat = str(request.args.get('cat'))
2023-05-21 21:39:29 +03:00
ua = str(request.args.get('ua'))
url = str(request.values.get("url") or request.headers.get("Referer"))
2023-05-22 11:25:46 +03:00
2025-09-27 13:55:46 +03:00
if os.path.exists('db.json'):
with open('db.json', 'r') as db:
2023-05-21 21:39:29 +03:00
db = json.loads(db.read())
else:
db = {}
if hash in db.keys():
2023-05-22 11:47:33 +03:00
visits = db[hash][cat]
2023-05-21 21:39:29 +03:00
else:
visits = list()
visits.append(url)
visits = list(set(visits))
db.update({
hash : {
2023-05-22 11:25:46 +03:00
cat : visits,
2023-05-21 21:39:29 +03:00
'agent' : ua
}
})
2023-05-22 01:16:04 +03:00
2023-05-22 11:55:02 +03:00
send_msg(f'User <{hash}> visited { url } on { ua } ({ str(len(db[hash][cat])) })')
2023-05-22 01:16:04 +03:00
2025-09-27 13:55:46 +03:00
with open('db.json', 'w+') as dbw:
2023-05-21 21:39:29 +03:00
dbw.write(json.dumps(db, indent=4))
2023-05-22 11:25:46 +03:00
return str(len(db[hash][cat]))
2023-05-21 21:39:29 +03:00
else:
2023-05-22 12:00:25 +03:00
return str(0)
2023-05-21 21:39:29 +03:00
2023-05-21 23:20:51 +03:00
serve(app, host='0.0.0.0', port=8001)