Last active
August 29, 2015 14:03
-
-
Save remore/2915398b9c0a83716c31 to your computer and use it in GitHub Desktop.
toy scripts for ego search which support php >5.2 and up. usage: $./twitter_search.sh | php parse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$result = json_decode(file_get_contents('php://stdin')); | |
$dir = realpath(dirname(__FILE__)); | |
// check if there is new tweet | |
if($result->statuses[0]->id != file_get_contents($dir.'/latest_tweet_id.dat')){ | |
// record latest tweet id | |
file_put_contents($dir.'/latest_tweet_id.dat', $result->statuses[0]->id); | |
// iterate search result | |
$mail_body = ""; | |
$date = new DateTime(); | |
foreach($result->statuses as $tweet){ | |
$created = new DateTime($tweet->created_at); | |
$created->modify("+9 hour"); | |
if ($date->format('d') != $created->format('d')){ | |
$mail_body .= "\n----------------------\n"; | |
$date = new DateTime($tweet->created_at); | |
} | |
$mail_body .= sprintf("%s\n[%s] @%s(%s) %s\n\n", $tweet->text, $created->format("Y-m-d H:i"), $tweet->user->screen_name, $tweet->user->name, ($tweet->entities->urls)?$tweet->entities->urls[0]->expanded_url:" - "); | |
} | |
mail("[email protected]", "Ego Search Times - ".date('l H:i'), $mail_body, "From: ego-search-checker"); | |
} else { | |
exit(0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Search for the latest tweets | |
# | |
# This script is based in part on a solution proposed by Mike Bounds | |
# in the twitter discussion forum: https://dev.twitter.com/discussions/14460 | |
################## | |
# Modified to fix searching for hashtags # and usernames @ by Greg Fenton | |
# http://labby.co.uk | |
# http://labby.co.uk/2013/08/twitter-search-hashtag-or-username-from-bash/ | |
################## | |
set -o errexit | |
screen_name=remore | |
query="foobar OR k.swd.cc OR github.com/remore" | |
query=`echo "$query" | sed 's/%/%25/g' | sed 's/ /%20/g' | sed 's/@/%40/g' | sed 's/=/%3D/g' | sed 's/#/%23/g' | sed 's/\//%2F/g'` | |
since= | |
tweet_count=40 # the number of tweets you want to retrieve | |
consumer_key="XXXXXX" | |
consumer_secret="YYYYY" | |
oauth_token="ZZZZZ" | |
oauth_secret="FOOBAR" | |
timestamp=`date +%s` | |
nonce=`date +%s%T555555555 | openssl base64 | sed -e s'/[+=/]//g'` | |
unsorted="count=${tweet_count} | |
oauth_consumer_key=${consumer_key} | |
oauth_token=${oauth_token} | |
oauth_nonce=${nonce} | |
oauth_version=1.0 | |
oauth_signature_method=HMAC-SHA1 | |
oauth_timestamp=${timestamp} | |
screen_name=${screen_name} | |
q=${query} | |
since_id=${since}" | |
sorted=`echo "$unsorted" | sort | tr '\n' "~" | sed 's/%/%25/g' | sed 's/~/%26/g' | sed 's/@/%40/g' | sed 's/=/%3D/g' | sed 's/#/%23/g' | sed 's/...$//'` | |
signature_base_string="GET&https%3A%2F%2Fapi.twitter.com%2F1.1%2Fsearch%2Ftweets.json&${sorted}" | |
signature_key="${consumer_secret}&${oauth_secret}" | |
oauth_signature=`echo -n ${signature_base_string} | openssl dgst -sha1 -hmac ${signature_key} -binary | openssl base64 | sed -e s'/+/%2B/' -e s'/\//%2F/' -e s'/=/%3D/'` | |
header="Authorization: OAuth oauth_consumer_key=\"${consumer_key}\", oauth_nonce=\"${nonce}\", oauth_signature=\"${oauth_signature}\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"${timestamp}\", oauth_token=\"${oauth_token}\", oauth_version=\"1.0\"" | |
result=`curl --silent --get 'https://api.twitter.com/1.1/search/tweets.json' --data "count=${tweet_count}&q=${query}&screen_name=${screen_name}&since_id=${since}" --header "Content-Type: application/x-www-form-urlencoded" --header "${header}"` | |
echo $result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment