Skip to content

Instantly share code, notes, and snippets.

@flocsy
Forked from rafi/siren.go
Created August 7, 2014 00:31
Show Gist options
  • Save flocsy/e23690a22dd67489ed86 to your computer and use it in GitHub Desktop.
Save flocsy/e23690a22dd67489ed86 to your computer and use it in GitHub Desktop.
package main
// Credits: http://stackoverflow.com/a/24683083/351947
import (
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"unicode/utf16"
"unicode/utf8"
)
const alertsUrl = "http://www.oref.org.il/WarningMessages/alerts.json"
type Record struct {
Id string `json:id`
Title string `json:title`
Data []string `json:data`
}
// LazyUTF16BytesToString converts UTF-16 encoded bytes, in big or little endian byte order,
// to a UTF-8 encoded string.
func LazyUTF16BytesToString(b []byte) string {
if len(b)%2 != 0 {
panic("len(b) % 2 != 0")
}
var codec binary.ByteOrder = binary.LittleEndian
if b[0] == 0xFE && b[1] == 0xFF { //check and strip the BOM
b = b[2:]
codec = binary.BigEndian
} else if b[0] == 0xFF && b[1] == 0xFE {
b = b[2:]
}
utf := make([]uint16, (len(b)+(2-1))/2)
for i := 0; i+(2-1) < len(b); i += 2 {
utf[i/2] = codec.Uint16(b[i:])
}
if len(b)/2 < len(utf) {
utf[len(utf)-1] = utf8.RuneError
}
return string(utf16.Decode(utf))
}
func main() {
client := &http.Client{}
req, err := http.NewRequest("GET", alertsUrl, nil)
perror(err)
req.Header.Add("Content-Type", "application/json; charset=utf-8")
res, err := client.Do(req)
perror(err)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
perror(err)
bodyString := LazyUTF16BytesToString(body)
var record Record
err = json.Unmarshal([]byte(bodyString), &record)
perror(err)
fmt.Println(record)
}
func perror(err error) {
if err != nil {
panic(err)
}
}
#!/usr/bin/env php
<?php
// Number of seconds to set polling rate
const INTERVAL = 3;
class Siren {
// Command to run when there's an alert
protected $command = '/usr/bin/notify-send ":msg"';
private $last_title;
public function poll()
{
$payload = file_get_contents('http://www.oref.org.il/WarningMessages/alerts.json');
if (empty($payload))
throw new Exception('Empty payload, aborting.');
// Converting encoding to UTF-8 and removing BOM
$payload = iconv('UCS-2', "UTF-8", $payload);
$payload = str_replace("\xEF\xBB\xBF", '', $payload);
$alerts = json_decode($payload, TRUE);
if (empty($alerts))
throw new Exception("Invalid payload. ERROR: ".json_last_error_msg());
if ($alerts['title'] != $this->last_title)
{
$this->notify($alerts['title']);
$this->last_title = $alerts['title'];
}
foreach ($alerts['data'] as $alert)
{
$this->notify($alert);
}
}
private function notify($msg)
{
$cmd = strtr($this->command, [ ':msg' => $msg ]);
exec($cmd);
}
}
$siren = new Siren;
echo 'Polling alerts every '.INTERVAL.' seconds...'."\n";
while (TRUE)
{
$siren->poll();
sleep(INTERVAL);
}
#!/usr/bin/env python2
#-*- coding: utf-8 -*-
import urllib2
import json
import subprocess
import time
opener = urllib2.build_opener()
req = urllib2.Request('http://www.oref.org.il/WarningMessages/alerts.json')
title = ""
while 1:
f = opener.open(req)
data = json.loads(f.read().decode('utf16'))
if (title != data['title']):
print(data['title'])
title = data['title'];
if (data['data']):
print(data['data']);
time.sleep(5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment