Category: Code

PHP Binance API

I’ve been spending some time helping out the the PHP Binance API over on github – php binance.

check back in soon, i’ll be releasing a containerized PHP bot for currency trading 🙂

For now you can get started with the API

Clone

git clone https://github.com/jaggedsoft/php-binance-api.git

Clone

PHP Binance API


Nautilus thumbnail generator (threaded)

I found a modified a python script to do multi threaded nautilus thumbnail generation.
With a large NAS, i don’t want to spend time waiting for thumbnails to be made, while entering a folder.

Run this as a cron job once a week and have thumbnails pre-generated 🙂

The code

#!/usr/bin/python
import os
import sys
import gi
import threading
import time

threads = []

gi.require_version('GnomeDesktop', '3.0')
from gi.repository import Gio, GnomeDesktop

def make_thumbnail(factory, filename):
    mtime = os.path.getmtime(filename)
    # Use Gio to determine the URI and mime type
    f = Gio.file_new_for_path(filename)
    uri = f.get_uri()
    info = f.query_info(
        'standard::content-type', Gio.FileQueryInfoFlags.NONE, None)
    mime_type = info.get_content_type()

    if factory.lookup(uri, mtime) is not None:
        print "FRESH       %s" % uri
        return False

    if not factory.can_thumbnail(uri, mime_type, mtime):
        print "UNSUPPORTED %s" % uri
        return False

    thumbnail = factory.generate_thumbnail(uri, mime_type)
    if thumbnail is None:
        print "ERROR       %s" % uri
        return False

    print "OK          %s" % uri
    factory.save_thumbnail(thumbnail, uri, mtime)
    return True

def thumbnail_folder(factory, folder):
    for dirpath, dirnames, filenames in os.walk(folder):
        for filename in filenames:
            t = threading.Thread(target=make_thumbnail, args=(factory,os.path.join(dirpath, filename),))
            threads.append(t)
            t.start()

            while len(threads) > 4:
                time.sleep(1)
                i = 0
                while i < len(threads):
                    if threads[i].is_alive():
                        i = i + 1
                    else:
                        del threads[i]


def main(argv):
    factory = GnomeDesktop.DesktopThumbnailFactory()
    for filename in argv[1:]:
        if os.path.isdir(filename):
            thumbnail_folder(factory, filename)
        else:
            make_thumbnail(factory, filename)

if __name__ == '__main__':
    sys.exit(main(sys.argv))

Eir SMS Web Text in PHP

Simple PHP script to send EIR SMS texts via Eir website

The code

<?php

$cookie_file = "cookie.txt";
$eir_username = "xxx@xxx.com";
$eir_password = "QWERTYUIOP";
$mynumber = "+353861112222";
$recipient "$mynumber"; 
$message = "simple text message";


$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://my.eir.ie/login"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36');
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_exec($ch); 
curl_close($ch); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://my.eir.ie/rest/brand/3/portalUser/authenticate"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"emailAddress\":\"" . $eir_username . "\",\"password\":\"" . $eir_password . "\"}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type:application/json' ) );
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_exec($ch); 
curl_close($ch); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://my.eir.ie/mobile/webtext/mobileNumbers/" . $mynumber . "/messages?ts=" . time() ); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"content\":\"" . $message . "\",\"recipients\":[\"" . $recipient . "\"]}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type:application/json' ) );
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_exec($ch); 
curl_close($ch); 

Raspberry pi 3 CSI camera with motion /dev/video0

There are a number of topics on the web, about getting a modified version of motion (motion-mmcal) to work with raspberry pi v2.1 CSI camera.

A simpler method is to expose the camera interface through the standard video 4 linux kernel interface /dev/video0.

This can be achieved by simply enabling the video 4 linux kernel module and installing the standard motion.

Install motion

apt-get install motion

Enable the kernel module on boot

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

i2c-dev
cuse
bcm2835-v4l2

Load the module without reboot

modprobe bcm2835-v4l2

Start service as normal

systemctl enable motion
systemctl start motion

Raspberry pi 3 disable red and green lights

Disabling the lights on raspberry pi has been documented a number of times on the web.
If differs from model to model, however the following method just sets the brightness to zero and should work on all models.

Modify your /etc/rc.local too like the following

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

echo 0 > /sys/class/leds/led1/brightness
echo 0 > /sys/class/leds/led0/brightness

exit 0