Wednesday, December 2, 2015

Posting to Twitter Part 2

Welcome to the second installment on how I automate my twitter posts.

Part 2: Decoding the pages
Sending the Fire/EMS pages to twitter  is similar to the CAD system.  I still use the TTYtter program, however another program is needed to create the mp3's.

I run Twotonedetect (http://www.twotonedetect.net/) which decodes the pages.  Currently I run it on a windows machine, however I would eventually like to migrate it over to a raspberrypi setup.

The system starts running at 7:15pm and shuts down at 5:59pm.  This allows the county to do their nightly pager tests and not flood my twitter account with useless pages.

TTD has a nice feature where after it decodes and creates the mp3, it will run a command.  This is where it calls my script that moves the file to the webserver, creates the link and posts to twitter.  The process is a bit different than the CAD posts, but the end result is the same.  The mp3's are kept for 7 days before being automatically deleted, so if you want to save the file, you have a week to do so before it is gone forever.

Part 3: So how did this all start anyway?
I'm not sure of the exact year, probably around 2007, we knew of a News/Fire notification service that sent out text pages on 900Mhz using the old text pagers.  I setup a decoder program called PDW (which would email you the decoded pages), along with an old Pro-2004 scanner with a discriminator tap and set out to find the frequency.  Few days later we found it and had it up and running and were sending pages to our phones.

That got old quick.  We had stuff coming in from NJ, PA, NY, CT and beyond.  I changed it to email it to my server and then I wrote a script to filter out only the things we wanted to see.  During this time, I learned more scripting than I had from the $3000 shell scripting course my company sent me to.

It wasn't long before I decided I didn't want to look at my phone every 10 minutes, so I set it up to send the pages to AOL IM via a program called centerim.  Eventually, AOL shut the account down thinking I was spamming and eventually we moved over to gtalk.  More people wanted in and eventually we had about 10 people on the system and the script ballooned to over 1000 lines of code, mainly for filtering.  It was ugly but it worked.

At the time, many of us were on the County TAMI system, but we only had access to a few departments.  The Warren County CAD one day showed up and after looking at it, I set out to add that to our system (the script for that hasn't changed much since).  Now we had access to everything.

Eventually, the 900Mhz Frequency went away and most people outside of the county dropped off.  It was at this time I got tired of seeing everything from the county on my IM, and started to filter by specific towns.  I still did want access to see everything in case I needed it.  I first experimented with RSS which worked but did not refresh automatically.  Eventually I went with twitter, where we are today.

Today we are at over 500 follows, many who do not have access to a police scanner, and now can keep up with what is happening around them.  In addition to twitter, IM has been replaced with a mobile app called Pushover.  There are about 10 user's who get alerted through it now (mostly Firefighters & OEM members).



That is the history of @WCAlerts and now @NorCoAlerts.



How do you get this stuff on twitter?

This is a common question.  If you follow one of my notification twitter accounts @WCAlerts or @NorCoAlerts, the first thing you realize is that everything is completely automated, unlike some of the other ones where some guy is cutting and pasting information in his spare time.  I will manually post if there is an ongoing incident and I happen to be at the computer, but 99% of my info is done without any user intervention.

So how do I do it.  The very short answer is this:
/usr/bin/perl /usr/local/bin/ttytter.pl -status=$msg -ssl

A less short answer is I use a program called TTYtter (http://www.floodgap.com/software/ttytter/).  Once you set it up, you can send a twitter post through the command line, with the $msg variable above being the actual post.  This is  done in a linux bash shell, so you either need to be running linux/Unix, or cygwin (https://www.cygwin.com/) under windows.  This should also work in MacOS 10, although I have never tried it.

Setting up TTYtter is actually pretty easy, however getting your data to a state where you can easily send it is not. 

My Backround
I went from working as a paid EMT back in the early 90's  the IT industry.  The last 15+ years I have dealt mainly with Unix/Linux systems.  Everything is done via a bash script.  These scripts are  very specific to my systems and the county websites, so it isn't something I can just hand someone and say go for it.They are adaptable and if another county comes online with a CAD, should be easily made to work with it.  I was able to get Northampton County up and running very quickly.  

Part 1 - CAD Posts
Sending out the CAD entries from Warren & Northampton Counties work pretty much the same way.  The scripts are a bit different due to differences in their formats, however they both basically work the same way.

Every couple of minutes I pull down all the CAD entries into a file (using lynx).  Then I do a series of of ugly unix commands (awk, sed, cut), and remove all the spaces and unwanted content.  The end result is everything in the CAD listed in a file which looks like this:

MVA W/INJURIES , RTE 57, MANSFIELD TWP
FALL 94 YOF, 73 W JOHNSTON ST, WASHINGTON BORO
CHEST PAIN 49 YOM, 130 POLKVILLE RD, KNOWLTON TWP
ELECTRICAL FIRE , 1041 RIDGE ST #33A, PHILLIPSBURG TO

The next step is I take each line of the file, compare it to an archive of all the previous posted entries.  If the entry does not exist, I post to twitter and add it to the archive.  If it is in the archive file, it moves on to the next line.  I keep about 75 of the newest entries to compare to.

That is really the high level explanation of how it works.  The next Blog post will cover the fire page mp3's (Part 2), and for those really bored, the history on how this whole thing developed (Part 3).