Skip to content

Instantly share code, notes, and snippets.

View astagi's full-sized avatar
💫

Andrea Stagi astagi

💫
View GitHub Profile
@astagi
astagi / eben.js
Created July 30, 2021 15:35
Get elements between even numbers using only reduce
const elementsBetweenEvenNumbers = (arr) => {
return arr.reduce((x, current) => {
x[0].push(current)
if (x[0].length === 3) {
if (x[0][0] % 2 === 0 && x[0][2] % 2 === 0)
x[1].push(x[0][1])
x[0] = x[0].slice(1)
}
return x
}, [[],[]])[1]
import requests
def get_all_planets():
page = 0
planets = []
next_url = "https://swapi.co/api/planets/"
while next_url:
resp = requests.get(next_url).json()
next_url = resp['next']
@astagi
astagi / rotate_top.js
Created December 23, 2019 11:41
R2D2
const MSG_ROTATE = [0x0A,0x17,0x0F];
let convertDegreeToHex = (degree) => {
var view = new DataView(new ArrayBuffer(4));
view.setFloat32(0, degree);
return Array
.apply(null, { length: 4 })
.map((_, i) => view.getUint8(i))
}
let writePacket = (characteristic, buff, waitForNotification=false, timeout=0) => {
return new Promise(function(resolve, reject) {
let dataRead = [];
let checkIsAValidRequest = (dataRead) => {
if (dataRead[5] != 0x00) {
characteristic.removeListener('data', listenerForRead);
reject(dataRead[5]);
}
const noble = require('noble');
let connectTheDroid = (address) => {
return new Promise((resolve, reject) => {
noble.on('discover', (peripheral) => {
if (peripheral.address === address) {
noble.stopScanning();
peripheral.connect( (e) => {
peripheral.discoverServices([CONNECT_SERVICE], (error, services) => {
services[0].discoverCharacteristics([CONNECT_CHAR], (error, characteristics) => {
const ESC = 0xAB;
const SOP = 0x8D;
const EOP = 0xD8;
const ESC_ESC = 0x23;
const ESC_SOP = 0x05;
const ESC_EOP = 0x50;
let seq = 0;
let buildPacket = (init, payload=[]) => {
@astagi
astagi / calculate_chk.js
Last active December 23, 2019 10:54
R2D2
let calculateChk = (buff) => {
let ret = 0x00;
for (let i = 0 ; i < buff.length ; i++) {
ret += buff[i];
}
ret = ret & 255;
return (ret ^ 255);
}
@astagi
astagi / constants.js
Created December 22, 2019 21:30
R2D2 Reverse
const CONNECT_SERVICE = "00020001574f4f2053706865726f2121";
const CONNECT_CHAR = "00020005574f4f2053706865726f2121";
const MAIN_SERVICE = "00010001574f4f2053706865726f2121";
const MAIN_CHAR = "00010002574f4f2053706865726f2121";
const MSG_CONNECTION = [0x75,0x73,0x65,0x74,0x68,0x65,0x66,0x6F,0x72,0x63,0x65,0x2E,0x2E,0x2E,0x62,0x61,0x6E,0x64];
const MSG_INIT = [0x0A,0x13,0x0D];
const MSG_OFF = [0x0A,0x13,0x01];
from createsend import Subscriber
from createsend.createsend import BadRequest
class CampaignMonitorException(Exception):
pass
class CampaignMonitorListSubscriber():
@astagi
astagi / phantomjs_install.sh
Last active April 15, 2019 20:53 — forked from ianchen06/phantomjs_install.sh
How to install PhantomJS on CentOS/RHEL
yum install fontconfig freetype freetype-devel fontconfig-devel libstdc++
wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
tar -xjvf ~/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 --strip-components 1 /opt/phantomjs/
sudo yum install libffi-devel openssl-devel
pip install pyopenssl pyasn1
pip install requests[security]