Work the Shell - Still Parsing the Twitter Stream

 in
How do you keep track of which tweets you've already answered?
But What Have We Already Seen?

The answer isn't that hard. The stream is newest to oldest, and the message ID values are assigned sequentially by the server, so all we need to do is cache the most recent message ID we've seen after we have answered all queries. Then, on subsequent invocations, compare each query ID to the most recent we've answered. If they're greater, we need to answer them. If not, we've already done so. Like this:

if [ "$id" == "$previouslatestid" -o $answered -eq 1 ] ; then
  echo "already answered query \"$msg\" from $name: skipped"
  answered=1
else
  ...

The previouslatestid is what's cached. We'll also capture the most recent ID of the current wave of queries like this:

if [ -z "$latestid" ] ; then
  latestid=$id        # store most recent ID
fi

Of course, there are a few more steps. We need to grab the cached value at the beginning of the script:

if [ -f "$lastidcache" ] ; then
  previouslatestid="$(cat "$lastidcache")"
else
  previouslatestid="0"
fi

And, we need to save it at the end:

echo $latestid > "$lastidcache"

That's it. I've run out of space, but the full script is available at ftp.linuxjournal.com/pub/lj/listings/issue191/10695.tgz. Next month, we'll polish it a bit and see what fun we can have with a tweetbot!

Dave Taylor has been hacking shell scripts for a really long time. He's the author of the popular Wicked Cool Shell Scripts and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.

______________________

Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix