This commit is contained in:
retoor 2025-01-02 19:28:33 +01:00
parent 71a032e3ef
commit c1e0f353f8
4 changed files with 114 additions and 29 deletions

View File

@ -22,6 +22,6 @@ where = src
[options.entry_points]
console_scripts =
metrici.serve = metriki.__main__:main
metrici.stats = metriki.stats:main
metriki.serve = metriki.__main__:main
metriki.stats = metriki.stats:main

View File

@ -1,23 +1,10 @@
import asyncio
from boeh import BooeehBot, env
async def main_async():
url = "https://matrix.org"
username = "@retoor2:matrix.org"
password = env.secret4
bot = BooeehBot(url, username, password)
try:
await bot.start()
except KeyboardInterrupt:
await bot.stop()
import aiohttp
from aiohttp import web
from metriki.app import Application
def main():
asyncio.run(main_async())
web.run_app(Application(db_path="sqlite:///metriki.db"),host="0.0.0.0",port=4000)
if __name__ == "__main__":
main()

View File

@ -4,6 +4,7 @@ import aiohttp
import json
import uuid
from datetime import datetime
import pathlib
class BaseView(web.View):
@ -130,17 +131,22 @@ class EventView(BaseView):
class Application(BaseApplication):
async def __init__(self, db_path, *args, **kwargs):
def __init__(self, db_path, *args, **kwargs):
self.view_count = 0
self.event_count = 0
await super().__init__(db_path=db_path, *args, **kwargs)
super().__init__(db_path=db_path, *args, **kwargs)
self.router.add_get("/", self.index_handler)
self.router.add_view("/event", EventView)
self.router.add_static("/static", pathlib.Path(__file__).parent.joinpath("static"))
async def index_handler(self, request):
return web.Response(
text="\n".join(["Under the spreading chestnut tree"
"I sold you, and you sold me"
"There lie they, and here lie we,"
text="\n".join([
"\n".join([
"Under the spreading chestnut tree",
"I sold you, and you sold me",
"There lie they, and here lie we,",
"Under the spreading chestnut tree."
]),content_type='text/plain')
]),
])
,content_type='text/plain')

View File

@ -0,0 +1,92 @@
class Metriki {
url = null
_isConnected = false
_isRendered = false
set isRendered() {
this._isRendered = true
this.mount()
}
set isConnected() {
this._isConnected = true
this.mount()
}
get isRendered() {
return this._isRendered
}
get isConnected() {
return this._isConnected
}
mount() {
if(this.isRendered && this.isConnected) {
this.ws.send("render")
}
}
emit(data){
this.ws.send(JSON.stringify(data))
}
emitVisit(){
this.emit({"href":window.location.href,"html":document.documentElement.outerHTML,"title":document.title,"domain":window.location.host})
this.addEventListeners()
}
addEventListeners(){
const me = this
document.addEventListener('mousemove', (e) => {
me.emit({
"event":"mousemove",
"page_x":e.pageX,
"page_y":e.pageY,
"screen_x":e.screenX,
"screen_y":e.screenY,
"client_x":e.clientX,
"client_y":e.clientY,
"target_x":e.target.offsetLeft,
"target_y":e.target.offsetTop
})
})
document.addEventListener('scroll', (e) => {
me.emit({
"event":"scroll",
"scroll_height":document.documentElement.scrollHeight,
"scroll_left":document.documentElement.scrollLeft,
"scroll_top":document.documentElement.scrollTop,
"client_width":document.documentElement.clientWidth,
"client_height":document.documentElement.clientHeight
})
})
document.addEventListener('click', (e) => {
me.emit({
"event":"click",
"target":e.target.id || e.target.className || e.target.tagName
})
})
}
constructor() {
this.url = window.location.href.replace("http://", "").replace("https://", "").replace("www.", "")
if this.url.endswith("/"):
this.url += "event"
esle
this.url += "/event"
console.log(this.url)
const me = this
this.ws = new WebSocket(this.url)
this.ws.onopen = () => {
me.isConnected = true
}
this.ws.onmessage = (e) => {
console.log(e.data)
}
document.addEventListener('DOMContentLoaded', () => {
me.isRendered = true
})
}
}
const metriki = new Metriki()