Skip to content

Instantly share code, notes, and snippets.

View michalbcz's full-sized avatar

Michal Bernhard michalbcz

View GitHub Profile
@michalbcz
michalbcz / delete_duplicated.groovy
Created November 11, 2022 09:14
groovy : naive implementation of deleting duplicated files
/*
Delete duplicated files
Naive implementation which consider duplicated files as files with same file size in bytes.
Note: this implementation delete *.jpg files but you can easily change it with another regexp. See line 17.
*/
import groovy.io.*
@michalbcz
michalbcz / slashy_string_ending_with_backslash_issue.groovy
Last active November 11, 2022 07:38
groovy: what happened when you use backslash as last character in "Slashy string"
/*
Example what happened when you use backslash in Slashy string (https://groovy-lang.org/syntax.html#_slashy_string)
Note: there is a note that you are not supposed to do that in https://groovy-lang.org/syntax.html#_special_cases
"A consequence of slash escaping is that a slashy string can’t end with a backslash"
*/
import groovy.io.*
// def dir = new File(/C:\Users/) // WORKS (removed last slash)
@michalbcz
michalbcz / console_save.js
Created April 8, 2021 13:20
browser : add console#save to save data from console to file
// inject this to your browser's developer tools console
(function(console){
console.save = function(data, filename){
if(!data) {
console.error('Console.save: No data')
return;
}
if(!filename) filename = 'console.json'
if(typeof data === "object"){
data = JSON.stringify(data, undefined, 4)
@michalbcz
michalbcz / json-to-ndjson.md
Created March 27, 2021 13:08 — forked from bzerangue/json-to-ndjson.md
JSON to NDJSON

NDJSON is a convenient format for storing or streaming structured data that may be processed one record at a time.

  • Each line is a valid JSON value
  • Line separator is ‘\n’

1. Convert JSON to NDJSON?

cat test.json | jq -c '.[]' > testNDJSON.json
@michalbcz
michalbcz / decimal_to_hex_puzzler.groovy
Last active March 7, 2021 08:15
DEC to HEX puzzler (see gist's revision for problem and diff to this solution)
/* THIS PUZZLER IS SOVLED, SEE REVISION OF THIS GIST TO SEE ORIGINAL PROBLEM */
decimalValue = 9989
hexValue = toHex(code)
assert hexValue == '2705'
// safe net
expectedValue = Integer.toHexString(decimalValue)
assert hexValue == expectedValue : "Your impl should work as expected. Result should be: ${expectedValue}"
@michalbcz
michalbcz / vanilla-js-jquery-like-examples.js
Last active January 14, 2021 05:37
vanilla js (not working in IE10 and older mostly)
// JSON
const data = await (await fetch('/my-url')).json();
// Post
await fetch('/my-url', { method: 'POST', body: data });
// Request
try {
const resp = await fetch('/my-url');
// ...
@michalbcz
michalbcz / gist:f8e7fcdc2fe17605d3966f17956c40c0
Last active December 11, 2020 07:56
turn off SSL verification #2
// methods
private static SSLSocketFactory createSslSocketFactory(X509TrustManager trustManager) {
try {
final SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new TrustManager[]{trustManager}, new SecureRandom());
return context.getSocketFactory();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
return ExceptionUtils.rethrow(e);
}
@michalbcz
michalbcz / gist:334d7c6585850752d3640c4ef407fc22
Last active April 12, 2020 13:53
bash get url resource (witgout wget or curl or something else)
{ echo -e "GET / HTTP/1.0\r\nHost: ftp.gnu.org\r\n\r" >&3; cat <&3 ; } 3<> /dev/tcp/ftp.gnu.org/80
@michalbcz
michalbcz / zbranekvalitne-questing-including-groups-scraper.js
Created September 7, 2019 09:11
Puppetteer based nodejs scraper of https://zbranekvalitne.cz/zbrojni-prukaz/testove-otazky including categorizing to groups
const puppeteer = require('puppeteer')
const fs = require('fs')
// this wrapper means immediatelly execute this code
void(async () => {
const url = 'https://zbranekvalitne.cz/zbrojni-prukaz/testove-otazky'
try {
console.log("I am scraping questions from " + url)
const browser = await puppeteer.launch({
@michalbcz
michalbcz / zbranekvalitne-scraper.js
Last active September 7, 2019 09:10
Puppetteer based nodejs scraper of https://zbranekvalitne.cz/zbrojni-prukaz/testove-otazky. Questions are not categorized by groups. Just all the questions.
const puppeteer = require('puppeteer')
const fs = require('fs')
// this wrapper means immediatelly execute this code
void(async () => {
const url = 'https://zbranekvalitne.cz/zbrojni-prukaz/testove-otazky'
try {
console.log("I am scraping questions from " + url)
const browser = await puppeteer.launch({