Skip to content

Instantly share code, notes, and snippets.

View bitsnaps's full-sized avatar
🌍
Working @ CorpoSense

Ibrahim H. bitsnaps

🌍
Working @ CorpoSense
View GitHub Profile
@bitsnaps
bitsnaps / JarExtractor.groovy
Created June 2, 2018 18:08
Extract Jar file pragmatically with Groovy
/*
Original Java Code from here:
https://stackoverflow.com/questions/1529611/how-to-write-a-java-program-which-can-extract-a-jar-file-and-store-its-data-in-s
*/
import java.util.jar.*
def jar = new JarFile("path/to/MyLib.jar")
def jarFile = new File(jar.name)
def distDir = "${jarFile.parent+File.separator}"
@bitsnaps
bitsnaps / HelloAwt.groovy
Created June 3, 2018 11:42
Simple AWT Gui App using what looks like Builder Pattern with Groovy
import java.awt.*
import java.awt.event.*
new Frame(title:"Hello AWT", size:[300, 200], locationRelativeTo: null, visible: true).with {
def tf = add({ new TextField(text:"www.google.com", bounds: [50,50,150,20])}() )
def l = add({ new Label(bounds: [50,100,250,20]) }())
add({
def b = new Button(label:"Find IP", bounds: [50,150,60,30])
b.addActionListener({ l.text = "IP of ${tf.text} is ${java.net.InetAddress.getByName(tf.text).hostAddress}" })
b}())
@bitsnaps
bitsnaps / JarMaker.groovy
Created June 3, 2018 18:27
Create a Jar file programatically using Groovy
import java.util.jar.*
def outputFile = "MyLib.jar"
def distDir = "path/to/content/MyLib"
/*Manifest manifest = new Manifest()
manifest.mainAttributes[Attributes.Name.MANIFEST_VERSION] = "1.0"*/
def jar = new JarOutputStream(new File("${new File(distDir).parent}/${outputFile}").newOutputStream()/*, manifest*/)
new File(distDir).eachFileRecurse {
@bitsnaps
bitsnaps / JarUpdater.groovy
Created June 5, 2018 13:50
Update Jar file by adding new ones with Groovy
import java.util.jar.*
def outputFile = new File("path/to/mylib/MyLib.jar")
def newFile1 = new File("path/to/new/file/NewClass1.class")
def newFile2 = new File("path/to/new/file/NewClass2.class")
updateJarFile(outputFile, newFile1, newFile2)
def updateJarFile(File jarFile, File... files){
def tmpFile = File.createTempFile('tempJar', '.tmp')
@bitsnaps
bitsnaps / scrape_chinabestprice.groovy
Created August 8, 2018 01:14
Simple groovy crawler from chinabestprice.com
@Grab('org.jsoup:jsoup:1.11.2')
import org.jsoup.*
import org.jsoup.nodes.*
import org.jsoup.select.*
import groovy.json.*
class Main {
static main(args){
@bitsnaps
bitsnaps / simple_random_encryption.groovy
Created September 4, 2018 10:18
Groovy simple random encryption
/*
groovy equivalent of java encryption snippet:
https://gist.github.com/bitsnaps/9cea5e16e09065d20c22db6a4d15cb65
*/
def encrypt(def str){
str.collect{s -> (Math.random()*9+1 as int).with{"${it}${((s.toCharacter() as int^it) as byte[]).encodeHex()}"} }.join('-')
}
def decrypt(def str){
str.split('-').collect{((it[1..2].decodeHex()[0] as int)^ it[0] as int) as char}.join()
}
@bitsnaps
bitsnaps / install-wp.sh
Created December 13, 2018 22:14
Wordpress CLI tool (wp-cli) script installation automation
#! /bin/bash
# Script tested on MacOS 10.14 with Wordpress 5.0
# Original article at:
# https://deliciousbrains.com/automating-local-wordpress-site-setup-scripts/
# Usage:
# ./install-wp.sh MyNewSite
@bitsnaps
bitsnaps / TikaMetadataExtractor.groovy
Created June 19, 2019 21:04 — forked from kaimst/TikaMetadataExtractor.groovy
A Groovy script that extracts metadata from files using Apache Tika. Works recursively on a file hierarchy and writes all found metadata into a single xml file.
/**
* Copyright 2013 Kai Sternad
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@bitsnaps
bitsnaps / matrices.groovy
Created June 21, 2019 20:47
Matrix manipulation with groovy by graxxia library
@Grab('org.graxxia:graxxia:1.0.1')
import graxxia.*
def m = new Matrix([[3,4,5],[6,2,3]])
println(m)
def m1 = [[1,2,3],[2,6,3],[7,4,2]] as Matrix
def m2 = [[2,6,4],[2,3,5],[7,6,5]] as Matrix
def m3 = [[2,3,4],[8,6,2],[10,4,3]] as Matrix
println(m1*2)
@bitsnaps
bitsnaps / sendemail.ppy
Created July 3, 2019 18:57
Send an email using sendinblue service api
from sys import stderr
import requests
# make sure this example works
# curl -s http://api.coindesk.com/v1/bpi/currentprice.json | python -c "import json, sys; print(json.load(sys.stdin)['bpi']['USD']['rate'])"
def send_email():
resp = requests.post('https://api.sendinblue.com/v2.0/email', headers={'api-key':'YOUR_API_KEY'},
json={"to":{"useremail@provider.com":"FULL NAME"}, "from":["youremail@provider.com","Your Full Name"], "subject":"Your Solution","html":"Welcome to <h1>Solution</h1>"})
if resp: #.status_code==200: