Skip to content

Instantly share code, notes, and snippets.

Avatar
🪕

Andrea Stagi astagi

🪕
View GitHub Profile
@astagi
astagi / eben.js
Created Jul 30, 2021
Get elements between even numbers using only reduce
View eben.js
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]
View swapi_with_generators.py
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']
View rotate_top.js
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))
}
View write_packet.js
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]);
}
View connect_the_droid.js
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) => {
View build_packet.js
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=[]) => {
View calculate_chk.js
let calculateChk = (buff) => {
let ret = 0x00;
for (let i = 0 ; i < buff.length ; i++) {
ret += buff[i];
}
ret = ret & 255;
return (ret ^ 255);
}
View constants.js
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];
View campaign_monitor.py
from createsend import Subscriber
from createsend.createsend import BadRequest
class CampaignMonitorException(Exception):
pass
class CampaignMonitorListSubscriber():
@astagi
astagi / phantomjs_install.sh
Last active Apr 15, 2019 — forked from ianchen06/phantomjs_install.sh
How to install PhantomJS on CentOS/RHEL
View phantomjs_install.sh
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]