added twitter interface

moved secrets into separate files with gitignore
master
Gunstick 2015-01-01 22:24:58 +01:00
parent 68934624dd
commit a1f4504497
9 changed files with 353 additions and 11 deletions

1
scripts/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
spaceapikey.txt

View File

@ -13,8 +13,11 @@ do
then
for plugin in "$0".d/*
do
"$plugin" pushed
logger -t $(basename $0) "called '$plugin pushed' with ret=$?"
if [ -x "$plugin" ]
then
"$plugin" pushed
logger -t $(basename $0) "called '$plugin pushed' with ret=$?"
fi
done
state="pushed"
else
@ -22,8 +25,11 @@ do
then
for plugin in "$0".d/*
do
"$plugin" released
logger -t $(basename $0) "called '$plugin released' with ret=$?"
if [ -x "$plugin" ]
then
"$plugin" released
logger -t $(basename $0) "called '$plugin released' with ret=$?"
fi
done
state="released"
fi

View File

@ -0,0 +1 @@
#put scripts here which are called the moment the lockbutton changes state

View File

@ -1,6 +1,7 @@
#!/bin/bash
# changes level2 state by the state of the main door
# i.e. people have to leave it open to keep status open
sleep 1 # silly way to debounce
if [ "$1" = "pushed" ] &&
[ "$(cat /run/spacestatus)" = "open" ]
then

View File

@ -1,6 +1,7 @@
#!/bin/bash
IGNORE_DOORLOCKBUTTON="no"
LockDir="/run/$(basename "$0").run"
spaceapikey="$(cat "$(dirname $0)"/spaceapikey.txt)"
P() {
while ! mkdir "$LockDir" 2>/dev/null
do
@ -34,18 +35,22 @@ then
chown www-data /run/spacestatus
fi
status=$(cat /run/spacestatus)
doorlockbutton=$(cat /run/doorlockbutton)
status="$(cat /run/spacestatus)"
oldstatus="$(cat /root/var/spacestatus)"
doorlockbutton="$(cat /run/doorlockbutton)"
nai=$(stat -c "%Y" /run/spacestatus) # get mtime as status change time
if [ "$status" = "open" ]
then
/usr/bin/curl --max-time 1 --silent --data key=96f7896f97asdf89u0a9s7d7fdasgsda88af --data-urlencode sensors='{"state":{"open":true,"lastchange":'"$nai"'}}' http://spaceapi.syn2cat.lu/sensor/set
/usr/bin/curl --max-time 1 --silent --data key="$spaceapikey" --data-urlencode sensors='{"state":{"open":true,"lastchange":'"$nai"'}}' http://spaceapi.syn2cat.lu/sensor/set
#logger -t $(basename $0) "sending status $status to spacapi ret=$?"
fi
for plugin in $(ls "$0".d)
do
"$0".d/"$plugin" "$status"
# logger -t $(basename $0) "called $plugin '$status'. ret=$?"
if [ -x "$0".d/"$plugin" ]
then
"$0".d/"$plugin" "$status" "$oldstatus"
logger -t $(basename $0) "called $plugin '$status' '$oldstatus'. ret=$?"
fi
done
if [ "$status" = "closed" ] && (
@ -54,7 +59,7 @@ then
# problem: if closing state but not actually shuting door for a longer time, the status in spaceapi
# will be the time of closing but not that of actually shutting the door
# but the status will only be updated once the door is shut
/usr/bin/curl --max-time 1 --silent --data key=96f7896f97asdf89u0a9s7d7fdasgsda88af --data-urlencode sensors='{"state":{"open":false,"lastchange":'"$nai"'}}' http://spaceapi.syn2cat.lu/sensor/set
/usr/bin/curl --max-time 1 --silent --data key="$spaceapikey" --data-urlencode sensors='{"state":{"open":false,"lastchange":'"$nai"'}}' http://spaceapi.syn2cat.lu/sensor/set
#logger -t $(basename $0) "sending status $status to spacapi ret=$?"
fi
@ -68,5 +73,5 @@ if [ "$status" = "closed" ]
then
presency=0
fi
/usr/bin/curl --max-time 1 --silent --data key=96f7896f97asdf89u0a9s7d7fdasgsda88af --data-urlencode sensors='{"sensors":{"people_now_present":[{"value":'"$presency"'}]}}' http://spaceapi.syn2cat.lu/sensor/set
/usr/bin/curl --max-time 1 --silent --data key="$spaceapikey" --data-urlencode sensors='{"sensors":{"people_now_present":[{"value":'"$presency"'}]}}' http://spaceapi.syn2cat.lu/sensor/set
V

1
scripts/upd_status.sh.d/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
twittertokens.php

View File

@ -0,0 +1,7 @@
put script here which are called once per minute
parameters to script are:
scriptname current_status previous_status
where current_status and previous_status are either "open" or "closed"
the script must have the executable bit.
parameter files can be here but must not have the execute bit

View File

@ -0,0 +1,263 @@
<?php
/**
* Twitter-API-PHP : Simple PHP wrapper for the v1.1 API
*
* PHP version 5.3.10
*
* @category Awesomeness
* @package Twitter-API-PHP
* @author James Mallison <me@j7mbo.co.uk>
* @license MIT License
* @link http://github.com/j7mbo/twitter-api-php
*/
class TwitterAPIExchange
{
private $oauth_access_token;
private $oauth_access_token_secret;
private $consumer_key;
private $consumer_secret;
private $postfields;
private $getfield;
protected $oauth;
public $url;
/**
* Create the API access object. Requires an array of settings::
* oauth access token, oauth access token secret, consumer key, consumer secret
* These are all available by creating your own application on dev.twitter.com
* Requires the cURL library
*
* @param array $settings
*/
public function __construct(array $settings)
{
if (!in_array('curl', get_loaded_extensions()))
{
throw new Exception('You need to install cURL, see: http://curl.haxx.se/docs/install.html');
}
if (!isset($settings['oauth_access_token'])
|| !isset($settings['oauth_access_token_secret'])
|| !isset($settings['consumer_key'])
|| !isset($settings['consumer_secret']))
{
throw new Exception('Make sure you are passing in the correct parameters');
}
$this->oauth_access_token = $settings['oauth_access_token'];
$this->oauth_access_token_secret = $settings['oauth_access_token_secret'];
$this->consumer_key = $settings['consumer_key'];
$this->consumer_secret = $settings['consumer_secret'];
}
/**
* Set postfields array, example: array('screen_name' => 'J7mbo')
*
* @param array $array Array of parameters to send to API
*
* @return TwitterAPIExchange Instance of self for method chaining
*/
public function setPostfields(array $array)
{
if (!is_null($this->getGetfield()))
{
throw new Exception('You can only choose get OR post fields.');
}
if (isset($array['status']) && substr($array['status'], 0, 1) === '@')
{
$array['status'] = sprintf("\0%s", $array['status']);
}
$this->postfields = $array;
return $this;
}
/**
* Set getfield string, example: '?screen_name=J7mbo'
*
* @param string $string Get key and value pairs as string
*
* @return \TwitterAPIExchange Instance of self for method chaining
*/
public function setGetfield($string)
{
if (!is_null($this->getPostfields()))
{
throw new Exception('You can only choose get OR post fields.');
}
$search = array('#', ',', '+', ':');
$replace = array('%23', '%2C', '%2B', '%3A');
$string = str_replace($search, $replace, $string);
$this->getfield = $string;
return $this;
}
/**
* Get getfield string (simple getter)
*
* @return string $this->getfields
*/
public function getGetfield()
{
return $this->getfield;
}
/**
* Get postfields array (simple getter)
*
* @return array $this->postfields
*/
public function getPostfields()
{
return $this->postfields;
}
/**
* Build the Oauth object using params set in construct and additionals
* passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
*
* @param string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json
* @param string $requestMethod Either POST or GET
* @return \TwitterAPIExchange Instance of self for method chaining
*/
public function buildOauth($url, $requestMethod)
{
if (!in_array(strtolower($requestMethod), array('post', 'get')))
{
throw new Exception('Request method must be either POST or GET');
}
$consumer_key = $this->consumer_key;
$consumer_secret = $this->consumer_secret;
$oauth_access_token = $this->oauth_access_token;
$oauth_access_token_secret = $this->oauth_access_token_secret;
$oauth = array(
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
$getfield = $this->getGetfield();
if (!is_null($getfield))
{
$getfields = str_replace('?', '', explode('&', $getfield));
foreach ($getfields as $g)
{
$split = explode('=', $g);
$oauth[$split[0]] = $split[1];
}
}
$base_info = $this->buildBaseString($url, $requestMethod, $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
$this->url = $url;
$this->oauth = $oauth;
return $this;
}
/**
* Perform the actual data retrieval from the API
*
* @param boolean $return If true, returns data.
*
* @return string json If $return param is true, returns json data.
*/
public function performRequest($return = true)
{
if (!is_bool($return))
{
throw new Exception('performRequest parameter must be true or false');
}
$header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
$getfield = $this->getGetfield();
$postfields = $this->getPostfields();
$options = array(
CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
);
if (!is_null($postfields))
{
$options[CURLOPT_POSTFIELDS] = $postfields;
}
else
{
if ($getfield !== '')
{
$options[CURLOPT_URL] .= $getfield;
}
}
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
if ($return) { return $json; }
}
/**
* Private method to generate the base string used by cURL
*
* @param string $baseURI
* @param string $method
* @param array $params
*
* @return string Built base string
*/
private function buildBaseString($baseURI, $method, $params)
{
$return = array();
ksort($params);
foreach($params as $key=>$value)
{
$return[] = "$key=" . $value;
}
return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return));
}
/**
* Private method to generate authorization header used by cURL
*
* @param array $oauth Array of oauth data generated by buildOauth()
*
* @return string $return Header used by cURL for request
*/
private function buildAuthorizationHeader($oauth)
{
$return = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key => $value)
{
$values[] = "$key=\"" . rawurlencode($value) . "\"";
}
$return .= implode(', ', $values);
return $return;
}
}

57
scripts/upd_status.sh.d/tweet Executable file
View File

@ -0,0 +1,57 @@
#!/usr/bin/php
<?php
/*
* Tezza
* https://github.com/J7mbo/twitter-api-php
* */
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
/*
$settings = array(
'oauth_access_token' => 'xxxx',
'oauth_access_token_secret' => 'yyyy',
'consumer_key' => 'xxxxx',
'consumer_secret' => 'yyyyy'
);
*/
require_once('twittertokens.php'); /* put the tokens inside separate file which is in .gitignore */
/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';
file_put_contents( '/var/test.txt', $argv );
/*
if ( $spaceAPI ) {
$level2status = $spaceAPI['state']['open'];
var_dump( $level2status );
if ( $level2status != file_get_contents('/var/cronjobs/lvl2_tweet/lastKnownState.txt') ) {
if ( $level2status ) {
$status = 'It\'s ' . date( 'H:i', $spaceAPI['state']['lastchange'] ) . ' and we are open \o/, come in and create something awesome =) #statusUpdate';
file_put_contents( '/var/cronjobs/lvl2_tweet/lastKnownState.txt', '1' );
} else {
$status = 'We just closed our doors at ' . date( 'H:i', $spaceAPI['state']['lastchange'] ) . '. See you very soon... #statusUpdate';
file_put_contents( '/var/cronjobs/lvl2_tweet/lastKnownState.txt', '0');
}
var_dump( $status );
// POST fields required by the URL above. See relevant docs as above
$postfields = array(
'status' => $status
);
// Perform a POST request and echo the response
$twitter = new TwitterAPIExchange($settings);
echo $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
}
}
*/