Tag: programming

  • Using Perl’s Net::Twitter to Harvest Keyword Searches

    So you’ve decided to dive into social media marketing on behalf of your church and/or charitable organization.

    In fact, you’ve been wisely leveraging bit.ly with Twitter or ow.ly with HootSuite to track and measure your outbound links — but you find yourself in need a more ‘industrial strength‘ means of tracking who is saying what about your organization or an upcoming event.

    You also want to speed up your WordPress blog as it’s been gagging when your Twitter RSS feed goes all 503 on you because Ashton Kutcher tweeted about his toenail clippings.

    Recipe for Success

    As I mentioned in my post last Tuesday entitled ‘Strategy vs. Tactics and your Social Media Activities ,’ I’ve been playing around with some of the cool social networking tools one can find in the CPAN library.

    Today I want to provide a quick snippet on how to use the Net::Twitter to write a simple PERL program to harvest a search.

    To do this, it mean installing the Net::Twitter library. You’ll likely need root or sudo privileges to make this happen. If you don’t know what root or sudo means, then you’ll want to contact your hosting provider.

    That said, once you get it installed, the next step is to go to the Twitter Search page and create an advanced search. From the resulting query string should give you all the parameters you need, for example:

    Based on the above example, I created the following script by using the nano editor for a file called ‘eastertweets.pl‘:

    #!/usr/bin/perl
    use Net::Twitter;
    use Net::Twitter::Search;
    use Scalar::Util 'blessed';
    # Just the Search API; exceptions thrown on error
    $nt = Net::Twitter->new(traits => [qw/API::Search/]);
    eval {
       # Parameters: q, callback, lang, rpp, page, since_id, geocode, show_user
       my $r = $nt->search({
          q=>"\"easter service\" OR \"sunrise service\"",
          lang=>"en",
          geocode=>"35.769804,-78.781622,40mi",
          rpp=>50,
          since=>"2010-03-21"
       });
       for my $status ( @{$r->{results}} ) {
          print "\@$status->{from_user}";
          print "\t$status->{created_at}\n";
          print "\t\t$status->{text}\n";
          print "-----------------------------------------------------\n";
       }
    };
    if ( my $err = $@ ) {
       die $@ unless blessed $err && $err->isa('Net::Twitter::Error');
       warn "HTTP Response Code: ", $err->code, "\n",
       "HTTP Message......: ", $err->message, "\n",
       "Twitter error.....: ", $err->error, "\n";
    }
    



    Once I created the file, it was simply a matter of modifying it to execute, then calling it:

    chmod a+x eastertweets.pl
    $HOME/eastertweets.pl

    So Why Bother?

    Now this approach by itself is a lot of work for little return. However, here are some things you might want to do with the sample above that would provide some big return value:

    1. feed this into a SQL database history via Perl DBI;
    2. create comma separated values and pipe it into a running log file;
    3. aggregate the returns with other searches into a single RSS file on your server for both the sake of speed and feeding localized dashboards;
    4. grep the returns for other key words, sending email notifications on hot items, while deleting those spammy items that make your feed so noisy;
    5. create a RESTFul web service that dynamically feeds your WordPress blog of select queries using Ajax via jQuery.

    The point is, once the data is captured, you can pretty much do anything you want with it programatically.

    For me, I’m thinking it might be fun to grab user IDs and feed their demographic information into some sort of analytics engine; or at least have some fun with Google Maps.

    Anyway, enjoy the example. If you expand on it, don’t forget to come back and provide a link. I’d be interested to see how this snippet evolves.

  • Setting up multiple test sites in XAMPP via virtual sites

    It’s NEVER a good idea to test new designs, programs and/or learn new stuff on a production website. This article describes how to create multiple virtual servers on a Windows 7 platform using XAMPP to create a perfect Linux/Apache like test bed.XAMPP + Win7 = great platform to test WordPress, MovableType and   Drupal

    Some Context

    I’m in the process of re-factoring some websites I’ve let go fallow far too long. Part of this process includes setting up a Linux-like test site on my brand new Windows7-driven Lenovo U350 via XAMPP.

    Yeah, I know, that was a lot all at once, so let’s break some of this down for those of you who don’t code for a living:

    What’s XAMPP?

    The WikiPedia defines XAMPPas follows:

    (pronounced /ˈzæmp/ or /ˈɛks.æmp/[1]) is a free and open source cross-platform web server package, consisting mainly of the Apache HTTP Server, MySQL database, and interpreters for scripts written in the PHP and Perl programming languages …

    … The program is released under the terms of the GNU General Public License and acts as a free web server capable of serving dynamic pages. XAMPP is available for Microsoft Windows, Linux, Solaris, and Mac OS X, and is mainly used for web development projects..

    In short, XAMPP gives me a Linux/LAMP development platform on a Windows based machine.

    My Situation

    Whether it’s learning something for work, or working on a church website, often find myself jumping between languages such as Perl, PHP and Python … and content ‘manglement’ systems such as WordPress, Drupal and MovableType, I find it’s easier to keep things organized if I:

    1. keep each project in its own path
    2. establish a virtual server for each project
    3. enter the project name in the address bar of my browser

    Getting it done

    By default, “localhost” is the default domain name for your PC. It resolves to IP address 127.0.0.1.

    But just as a hosting provider can support several domain names on a single IP address, so too can your Windows system.

    Below are the steps to get this done:

    Step 1 – identify the new host

    Unlike Windows XP or Vista,  for Windows 7 you’ll need to right click on the NotePad program and “Run as Administrator” as pictured below:

    Notepad - Open as Admin

    This is because the file we want to edit is now protected. That file is located at:

    
    C:\Windows\System32\drivers\etc\hosts
    
    

    Once you’ve opened the file and on or about line 23, edit your file so it reads:

    
    127.0.0.1       localhost
    127.0.0.1       drupal
    
    

    Save it, close your notepad editor, so you don’t shoot yourself in the foot in admin mode.

    Step 2 – establish the virtual host

    Keep in mind, the primary purpose of XAMPP is to give you an Apache server that runs on your local machine.

    That in mind, you’ll need to edit one more file:

    
    notepad C:\xampp\apache\conf\extra\httpd-vhosts.conf
    
    

    Once in, you’ll want to modify it so it reads:

    
    NameVirtualHost *:80
    <VirtualHost *:80>
     ServerAdmin postmaster@dummy-host.localhost
     DocumentRoot "C:/xampp/htdocs"
     ServerName localhost:80
     ServerAlias localhost
     ErrorLog "logs/dummy-host.localhost-error.log"
     CustomLog "logs/dummy-host.localhost-access.log" combined
    </VirtualHost>
    <VirtualHost *:80>
     ServerAdmin postmaster@drupal-host.localhost
     DocumentRoot "C:/xampp/htdocs/drupal"
     ServerName drupal:80
     ServerAlias drupal
     ErrorLog "logs/drupal-host.localhost-error.log"
     CustomLog "logs/drupal-host.localhost-access.log" combined
    </VirtualHost>
    
    

    Note, in the default XAMPP install, the above is commented out, and the hosts are dummy and dummy2. I simply un-commented everything and renamed dummy2 to drupal.

    Step 3

    Restart your Apache server. The easiest way to do this is stop and start the server through the can be done through the console as pictured below:

    XAMPP Console

    Step 4 – Test It

    Finally, you’ll want to test it by entering “drupal” in the address bar of the browser of your choice.

    Before you do that, you may want to create the directory C:\xampp\htdocs\drupal …

    … and then add an index.html, .php, .pl OR .py file to provide the ubiquitous “Hello World!” to demonstrate everything is running as planned.

    Wrap-up

    Additional Resources

    I’m not the first person to write on this topic, nor will I be the last. That said, here are some other sites that offer similar tutorials in case the one above is still as clear as mud.

    Why Bother?

    Some of you may be wondering why bother at all? Why not just work on your live site.

    Personally, as an IT professional with a couple of decades experience, I can say with utter certainty – backed-up with copious examples – that this is a recipe for disaster.

    Instead, why not simply take an old box and install a Linux distribution such as Ubuntu or Fedora … or do what I did, took a new box an added XAMPP.

    Either way, you’ll be glad you did when one of your tests or learning experiences fries your non-production site.