diff --git a/eintopf-radar-sync.py b/eintopf-radar-sync.py new file mode 100644 index 0000000..2b73caf --- /dev/null +++ b/eintopf-radar-sync.py @@ -0,0 +1,87 @@ +#!/bin/env python + +import requests +import json +from bs4 import BeautifulSoup + +print("hello world") +sys.exit() + +# Eintopf config +# Get Authorization token through login request +# http://localhost:3333/api/v1/swagger#/auth/login +eintopf_url = "https://karlsunruh.project-insanity.org" +eintopf_api_endpoint = eintopf_url + "/api/v1/events/" +eintopf_headers = { + "Authorization": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjJlZTA3MDgzLTVjYzktNGM5MS04ZThkLTFkNjhkNzZhZDc5YiIsIm5iZiI6MTQ0NDQ3ODQwMH0.hDQiwXBpIfEiOLP1QAXb9q8eQeaslIHlLN3CBdkHzQKdH0eZZCViEooIyKdZmoncQ0NQAExaitUbFnn6HcAITy8buBhIep6g0fRrfnTgqYOwelhJCXKySUwLe72sEthElaOfISKhvS9Tss4zd3NkNIfFDBVXMnmtOUXmrmlt7Z-5y9p4IiftqBKRA-Md4Uc6iiylSPi7ZZ0r23p2NrYJMyTiWS7-PfhNUt8GJ7HXjmX08VDTQs2lBnQH4c5n1lLCRkUUGpSgPg_2yBnSWN3z_3gQ_mOBNbvYTI2rc4i5fh6eQMIp4B5iL4Kt4Ebe-ikwQFXQ2INWCmemtQtB2pyVMg", + "Content-Type": "application/json" +} + +# Radar config +radar_group_id = "436012" +radar_api_endpoint = "https://radar.squat.net/api/1.2/search/events.json?fields=title,offline,date_time,body&facets[group][]=" + radar_group_id + +def strip_html_tags(text): + soup = BeautifulSoup(text, "html.parser") + return soup.get_text() + +def eintopf_post_event(title, location, description, time_start, time_end): + payload = { + "address": "Karlsruhe", + "category": "", + "description": strip_html_tags(description), + "image": "", + "involved": [ + { + "description": "Anonymous", + "name": "Anonymous" + } + ], + "lat": 0, + "lng": 0, + "location": location, + "name": title, + "organizers": ["Anonymous"], + "ownedBy": ["Anonymous"], + "published": True, + "start": time_start, + "end": time_end, + "tags": ["karlsruhe"], + "topic": "Veranstaltung" + } + response = requests.post(eintopf_api_endpoint, json=payload, headers=eintopf_headers) + + if response.status_code == 200: + return True + else: + return False + +response = requests.get(radar_api_endpoint) + +if response.status_code == 200: + data = response.json() + events = data["result"] + + radar_events = [] + + for event in events: + + event = events[event] + title = event["title"] + time_start = event["date_time"][0]["time_start"] + time_end = event["date_time"][0]["time_end"] + location = event["offline"][0]['title'] + description = event["body"]['value'] + + if eintopf_post_event(title, location, description, time_start, time_end): + print("Event successfully added:") + print(f"Title: {title}") + print(f"Time Start: {time_start}") + print(f"Location: {location}") + print('-' * 40) + else: + print("Submitting event failed") + sys.exit(1) +else: + print(f"Failed to retrieve data. Status code: {response.status_code}") + diff --git a/eintopf-sync.py b/eintopf-sync.py deleted file mode 100644 index 8d18d46..0000000 --- a/eintopf-sync.py +++ /dev/null @@ -1,30 +0,0 @@ -import requests -import json - -url = "https://radar.squat.net/api/1.2/search/events.json?fields=title,offline,date_time,body&facets[group][]=436012" -response = requests.get(url) - -if response.status_code == 200: - data = response.json() - events = data["result"] - - radar_events = [] - - for event in events: - - event = events[event] - title = event["title"] - time_start = event["date_time"][0]["time_start"] - time_end = event["date_time"][0]["time_end"] - location = event["offline"][0]['title'] - description = event["body"]['value'] - - # Print the formatted output - print(f"Title: {title}") - print(f"Time Start: {time_start}") - print(f"Location: {location}") - #print(f"Description: {body}") - print('-' * 40) -else: - print(f"Failed to retrieve data. Status code: {response.status_code}") - diff --git a/flake.lock b/flake.lock index 2a0d99f..d0c2498 100644 --- a/flake.lock +++ b/flake.lock @@ -5,11 +5,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1726560853, - "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", "owner": "numtide", "repo": "flake-utils", - "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", "type": "github" }, "original": { @@ -20,16 +20,16 @@ }, "nixpkgs": { "locked": { - "lastModified": 1730883749, - "narHash": "sha256-mwrFF0vElHJP8X3pFCByJR365Q2463ATp2qGIrDUdlE=", + "lastModified": 1734991663, + "narHash": "sha256-8T660guvdaOD+2/Cj970bWlQwAyZLKrrbkhYOFcY1YE=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "dba414932936fde69f0606b4f1d87c5bc0003ede", + "rev": "6c90912761c43e22b6fb000025ab96dd31c971ff", "type": "github" }, "original": { "id": "nixpkgs", - "ref": "nixos-24.05", + "ref": "nixos-24.11", "type": "indirect" } }, diff --git a/flake.nix b/flake.nix index e51b907..9514030 100644 --- a/flake.nix +++ b/flake.nix @@ -1,6 +1,6 @@ { inputs = { - nixpkgs.url = "nixpkgs/nixos-24.05"; + nixpkgs.url = "nixpkgs/nixos-24.11"; # Required for multi platform support flake-utils.url = "github:numtide/flake-utils"; }; @@ -9,23 +9,40 @@ flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; }; - - start = - pkgs.writeShellScriptBin "start" '' - set -e - ${pkgs.python3}/bin/python eintopf-sync.py - ''; in - { + rec { devShell = pkgs.mkShell { packages = with pkgs; with python3Packages; [ - python3 + python requests + beautifulsoup4 ]; }; - packages = { inherit start; }; - defaultPackage = start; + packages = flake-utils.lib.flattenTree { + eintopf-radar-sync = pkgs.python3Packages.buildPythonApplication { + pname = "eintopf-radar-sync"; + version = "0.0.1"; + format = "other"; + + src = self; + + dependencies = with pkgs.python3Packages; [ + python + requests + beautifulsoup4 + ]; + + installPhase = '' + install -Dm755 ${./eintopf-radar-sync.py} $out/bin/eintopf-radar-sync + ''; + }; + }; + + defaultPackage = packages.eintopf-radar-sync; + + # eintopf-radar-sync service module + nixosModule = (import ./module.nix); }); } diff --git a/module.nix b/module.nix new file mode 100644 index 0000000..16dc13b --- /dev/null +++ b/module.nix @@ -0,0 +1,50 @@ +{config, lib, pkgs, ...}: + +with lib; + +let + + cfg = config.services.eintopf-radar-sync; + +in + { + + options = { + services.eintopf-radar-sync = { + + enable = mkOption { + type = types.bool; + default = false; + description = '' + Enable eintopf-radar-sync daemon. + ''; + }; + + }; + }; + + config = mkIf cfg.enable { + + systemd.services."eintopf-radar-sync" = { + description = "eintopf-radar-sync script"; + after = [ "network.target" ]; + wantedBy = [ "multi-user.target" ]; + environment.PYTHONUNBUFFERED = "1"; + serviceConfig = { + ExecStart = "${pkgs.iwd-autocaptiveauth}/bin/iwd-autocaptiveauth --profileDir ${pkgs.iwd-autocaptiveauth}/profiles"; + Restart = "on-failure"; + User = "iwd-autocaptiveauth"; + RestartSec = 30; + WorkingDirectory = ''${pkgs.iwd-autocaptiveauth}/''; + }; + restartIfChanged = true; + }; + + }; + + meta = { + maintainers = with lib.maintainers; [ onny ]; + }; + + } +