Skip to content

Instantly share code, notes, and snippets.

@nilsmagnus
nilsmagnus / jsonb.sql
Last active November 11, 2023 10:57
postgres med json
insert into artikler(article_type, content)
values (1, '{"articleId": "xyz123", "meta":{"tag":"bil", "title":"ny elbil", "codes":["a","b"]}}'::jsonb);
select content::text
from artikler;
select content -> 'meta' ->> 'title' as tittel
from artikler
where content -> 'meta' ->> 'tag' = 'bil';
@nilsmagnus
nilsmagnus / readme.md
Created May 26, 2023 06:27
Goroutines mem test profiling

create a test-file

touch main_test.go

paste the code for a test:

package main

import (
@nilsmagnus
nilsmagnus / main.dart
Created April 7, 2023 19:13
DateTimeCategoryAxis bug
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@nilsmagnus
nilsmagnus / twitch.sh
Created May 16, 2022 09:42
Stream to twitch with libcamera-vid and ffmpeg
#!/bin/bash
STREAM_KEY=<insert your twitch stream key>
## you might want to use a different twitch-server, see https://stream.twitch.tv/ingests/
while true
do
libcamera-vid -o - --brightness 0.1 -t 0 -g 30 --width 640 --height 480 | ffmpeg -re -f h264 -i pipe:0 -vcodec copy -strict experimental -f flv rtmp://osl.contribute.live-video.net/app/$STREAM_KEY
# pause a bit if things fail before restart
sleep 60
done
@nilsmagnus
nilsmagnus / exec_returning_id.go
Created May 12, 2022 04:56
Function to return id from postgresql query
package pgsql-helper
import (
"database/sql"
"fmt"
)
//ExecReturningId executes query and returns id.
// The query must specify what field is returned.
// Example:
@nilsmagnus
nilsmagnus / validate.js
Last active September 13, 2021 13:25
Validate norwegian organization number with javascript
// validate org number according to https://www.brreg.no/om-oss/oppgavene-vare/alle-registrene-vare/om-enhetsregisteret/organisasjonsnummeret/
function validateNorwegianOrgNumber(rawNumber: string): boolean {
if (
rawNumber == null ||
rawNumber.trim().length !== 9 ||
!/^\d*$/.test(rawNumber.trim())
)
return false;
const numbers = rawNumber.trim();
const controlNumbers = [3, 2, 7, 6, 5, 4, 3, 2];
@nilsmagnus
nilsmagnus / getcookies.go
Created September 9, 2021 11:57
follow redirects with go and collect all the cookies returned
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
@nilsmagnus
nilsmagnus / expandable_fab.dart
Created July 28, 2021 12:09
Expandable fab with flutter and material design
import 'dart:math' as math;
import 'package:flutter/material.dart';
@immutable
class ExpandableFab extends StatefulWidget {
const ExpandableFab({
Key? key,
this.initialOpen,
this.spacing = 70,
@nilsmagnus
nilsmagnus / gist:2cb8c52f2ef6e7c34f20ff45cee1e786
Last active November 19, 2022 10:52
Post form-data with flutter/dart
import 'package:http/http.dart'; // http library, http:0.13.0
import 'dart:developer'; // logging locally
String? get baseUrl => "http://localhost:8081"
Future<Response> postForm(String path, Map<String, String> body, {Map<String, String>? customHeaders}) async {
final headers = commonHeaders()
..addAll(customHeaders ?? {})
..addAll({"Content-Type": "application/x-www-form-urlencoded"});
log("POST $baseUrl$path with body $body and headers $headers", name: "http_service");
# Web streaming example
# Source code from the official PiCamera package
# http://picamera.readthedocs.io/en/latest/recipes2.html#web-streaming
import io
import picamera
import logging
import socketserver
from threading import Condition
from http import server