From c1e0f353f8688401392ba7c79744dac7490a1679 Mon Sep 17 00:00:00 2001 From: retoor Date: Thu, 2 Jan 2025 19:28:33 +0100 Subject: [PATCH] Woeii. --- setup.cfg | 4 +- src/metriki/__main__.py | 21 ++------ src/metriki/app.py | 26 ++++++---- src/metriki/static/metriki.js | 92 +++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 29 deletions(-) create mode 100644 src/metriki/static/metriki.js diff --git a/setup.cfg b/setup.cfg index 62bcc95..67f70b2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/src/metriki/__main__.py b/src/metriki/__main__.py index d44e1f5..2dc1130 100644 --- a/src/metriki/__main__.py +++ b/src/metriki/__main__.py @@ -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() diff --git a/src/metriki/app.py b/src/metriki/app.py index 27b710e..055919c 100644 --- a/src/metriki/app.py +++ b/src/metriki/app.py @@ -4,6 +4,7 @@ import aiohttp import json import uuid from datetime import datetime +import pathlib class BaseView(web.View): @@ -68,8 +69,8 @@ class EventView(BaseView): try: record = {} for field in self.document_fields: - if field in data: - record[field] = data[field] + if field in data: + record[field] = data[field] if len(record.keys()) == len(document_fields): return record except KeyError: @@ -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," - "Under the spreading chestnut tree." - ]),content_type='text/plain') + 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') diff --git a/src/metriki/static/metriki.js b/src/metriki/static/metriki.js new file mode 100644 index 0000000..b93f738 --- /dev/null +++ b/src/metriki/static/metriki.js @@ -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()