Inspirated

 
 

May 16, 2010

HOWTO: Query WordPress posts in CMS Made Simple

Filed under: Blog — krkhan @ 3:53 pm

While I run my blog at inspirated.com, I aggregate posts related to coding at the subdomain code.inspirated.com. The websites are run through WordPress and CMS Made Simple respectively.

For the latter, I needed to find a way of fetching blog posts from the main site for linking. Initially, I used tag feeds for this purpose. That is, I used the RSS module for CMS-MS and fetched the feed for a particular tag (e.g., inspirated.com/tag/code/feed). This worked well for a while until the feeds became large and I noticed that only the most recent 10 posts were showing up in the listings.

Digging around, I found this piece of documentation which explains how one can use custom queries for collecting WordPress posts from a blog. There was a catch however as the code for doing so could only be run globally. In other words, if I tried running the code mentioned on the page inside a User Defined Tag in CMS-MS I would get strange errors.

The solution was to run the code in a separate PHP file. Here’s how:

  1. Create a file named wp.php in your CMS-MS folder with the following code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    <?php
    require_once('/path/to/wordpress/wp-blog-header.php');
    // edit the path in the line above to point to your wp-blog-header.php
     
    $tag = isset($_REQUEST['--tag']) ? $_REQUEST['--tag'] : 'code';
    $count = isset($_REQUEST['--count']) ? $_REQUEST['--count'] : '-1';
    $after = isset($_REQUEST['--after']) ? $_REQUEST['--after'] : '1970-01-01';
     
    function filter_where($where = '') {
        global $after;
        $where .= " AND post_date >= '".$after."'";
        return $where;
    }
    add_filter('posts_where', 'filter_where');
     
    query_posts('tag='.$tag.'&posts_per_page='.$count);
     
    echo "<ul>";
    if ( have_posts() ) : while ( have_posts() ) : the_post();
        echo "<li>";
        echo "<a href=\"";
        echo the_permalink();
        echo "\">";
        echo the_title();
        echo "</a>";
        echo " (";
        echo the_time('F jS, Y');
        echo ")";
        echo "</li>\n";
    endwhile; else:
        echo "<li>No posts found</li>\n";
    endif;
    echo "</ul>";
     
    wp_reset_query();
    ?>
  2. Add a User Defined Tag in CMS-MS with the following code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    $path = 'whoami | php -q /path/to/wp.php';
    // edit the path in the line above to point to the wp.php created in previous step
     
    // whoami command piped for no reason because my script wasn't
    // producing any output without it
     
    if(isset($params['tag'])) {
        $path .= ' --tag='.$params['tag'];
    }
     
     
    if(isset($params['count'])) {
        $path .= ' --count='.$params['count'];
    }
     
    if(isset($params['after'])) {
        $path .= ' --after='.$params['after'];
    }
     
    echo `$path`;
  3. Use the tag in any CMS-MS page with any of the following combinations:
    • List all posts with the tag code:
      {wp_posts_with_tag tag="code"}
    • List 10 posts with the tag code:
      {wp_posts_with_tag tag="code" count="10"}
    • List all posts with the tag code after May 1st, 2009:
      {wp_posts_with_tag tag="code" after="2009-05-01"}

Custom queries are very powerful once you get them working. Anyone planning on using them should take a look at the function reference for getting to grips with the flexibility they offer.

Tags: , , , , ,

May 14, 2010

Summer of Code Progress: Ready to Launch(pad)

Filed under: Blog — krkhan @ 11:51 pm

As the initial stage of my GSoC project is heavily involved with the Launchpad core, the first step for being ready was to get it running in a clean lean virtual machine. Bad rhyming I know. But trust me, trying to setup Launchpad locally with a borked internet connection is worse.

So, after some ∞ attempts at using rocketfuel-setup, I got stuck because my internet gave up on each run before Bazaar repositories could download. To top it off, once the repositories did download the VM decided to become unresponsive as it had only 384 MB of RAM and unpacking a huge repository like Launchpad’s core required a lot more resources than that.

Love for open-source followed as I downloaded the repositories on a separate machine, transferred them to the VM using NFS and then was able to modify the rocketfuel-setup so that it would do only the remaining work. The result:

Virtual Launchpad Snapshot #1
(Click on the thumbnail for larger version.)

The added bonus of doing the whole process on VM was the ability to go back to a clean state in case something went haywire. A few months ago, I had moved over to Sun’s VirtualBox from Redhat’s Virtual Machine Manager and this proved to be helpful on many accounts. Apart from being sleeker and more friendlier, VirtualBox offers advanced features such as PulseAudio integration on host and hierarchical snapshots. Using the shots, now I can just select a state where Launchpad was launched clean … :

Virtual Launchpad Snapshot #2
(Click on the thumbnail for larger version.)

… click on restore and viola, I’ll have the same instance up and running again within a few seconds.

Tags: , , , , , ,

May 11, 2010

Summer of Code Progress: Ubuntu Community Bonding

Filed under: Blog — krkhan @ 8:53 pm

Over the past few days, I have been preparing myself for GSoC work in various ways. Officially, these few weeks are defined as “Community Bonding Period” for Summer of Code participants. Meaning, selected students are expected to spend time for getting everything set for the real development which starts on May 26th.

The first thing on my priority list was to find a way for fail-safe IRC communication. The end solution was documented in my previous post. In a nutshell, now I have this IRC client running all the time to which I can connect occasionally and go-through the activity since my last visit.

Next up was discussing with Bryce the list of things that should be ready before the next stage. Setting up Launchpad locally was pinpointed as the real PITA for beginning the development. He advised me on a few other aspects as well, including why I should aim to complete the major coding portions of my work before the mid-term evaluation. Bryce has been the X.org maintainer for Ubuntu so he’s also new to Launchpad development like me. However, unlike me he has been able to set up a Launchpad instance on Lucid Lynx. I have been trying a bit unsuccessfully to do the same with Karmic Koala for a few days now. The rocketfuel-setup quits on me after complaining about download-cache directory not being available in the devel tree. I’m hoping to look more into that tonight.

As for the miscellaneous things, Google gifted all the participants an year of ACM Student Membership. Without sarcasm, ACM has one of the most confusing user-management system I have ever used. I already had a web-account on ACM and hence my username was not available for using this membership. Doesn’t make sense? Exactly. With that said, on the brighter side of things was the fact that ACM’s help-desk was prompt and solved my registration problems within a few emails of correspondence. The ACM email address still isn’t forwarding emails properly but I guess that’ll take a few days to resolve.

On the community side of things, I thought it would be convenient to have important information about my organization’s projects in one place. On that account, here’s a list of my fellow Ubuntu participants for this year’s Summer of Code:

Student Project Mentor
Sarah Strong The Great Clipboard Fixing Galore Project Ted Gould
Michal Karnicki Android U1: Ubuntu One client for Android Stuart Langridge
Jacob Peddicord services-admin configuration and Upstart-ification David Bensimon
Dylan McCall Harvest user interface improvements Daniel Holbach
Urban Skudnik Home user backup solution/Deja Dup improvements Michael Terry
Dmitrijs Ledkovs USB-creator Improvements Evan Dandrea
Kamran Khan Bug Triaging Improvements for Launchpad/Arsenal Bryce Harrington
Harald Sitter Ubuntu One for the KDE workspace Jonathan Riddell
Peter Gardenier Software Center Improvements Matthew Thomas
Andres Rodriguez Lazo Testdrive Front End Dustin Kirkland

Good luck to everyone on board!

Tags: , , , , , , ,

April 27, 2010

Finally there — Google Summer of Code 2010

Filed under: Blog — krkhan @ 1:02 am

GSoC 2010 Logo

Fate has a curious sense of humor. After having 8 of my proposals rejected in past 3 years for Google Summer of Code, I had 2 of them selected in the same year. In GSoC terminology, I became a “duplicate” student. Now, the standard practice for such cases is that the administrators of both organizations interested in the student get together in a “deduplication” meeting and resolve the conflict. However, Maria Randazzo (the program administrator from the Ubuntu organization) was kind enough to ask my preference regarding the project I’d like to work upon.

This put me in a rather uneasy position as mentors from both organizations (Bryce Harrington from Ubuntu & Alexey Khoroshilov from The Linux Foundation) had been really helpful during the application process. In the end however, I chose Ubuntu since its proposal focused on Arsenal and Python which I found relatively more familiar than Alien and Perl. I had also already collaborated with Bryce for some Arsenal patches before so it was easier for me to get up-to-speed with the Ubuntu proposal.

The proposal itself is in fact publicly viewable at this link. Summarizing my feelings, I’ve never looked forward to a summer as much as this one.

“If a June night could talk, it would probably boast it invented romance code.” —Bern Williams

Tags: , , , , , , , , ,

April 11, 2010

Workaround for getting received SMS’ sender number in PyS60

Filed under: Blog — krkhan @ 9:22 pm

It’s fairly simple in PyS60 to get the sender’s details whenever a new SMS is received. However, if the sender has an entry in your address book these details translate to just the contact name. That way, if the contact has multiple numbers you have no idea which number was used to send the text. Here’s an ugly little workaround for this issue:

received-sms-number.py

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def message_received(id, box):
    box.sms_messages()
    sender = box.address(id)
 
    if sender[0] != '+' or sender[1:].isdigit() == False:
        # Try to find the sender's number from log.
        logged_sms = logs.sms(mode = 'in')[0]
 
        # To confirm that the log entry is the same one we're concerned
        # about, do some checks.
        if logged_sms['subject'] == sms_text[:64]:
            number = logged_sms['number']
            contacts_db = contacts.open()
            found_contact = contacts_db.find(number)
            if found_contact:
                title_match = True
                for token in sender.split():
                    if found_contact[0].title.find(token) == -1:
                        title_match = False
                if title_match:
                    sender = number
 
    print "SMS Received from: ", sender

You can use this little snippet of code to reply back to the sender even when there’s a matching entry in the contacts database. This in turn opens up a whole new range of interesting ideas for auto-responding applications.

Tags: , , , , , ,

March 8, 2010

Using overlays for source code listings in LaTeX Beamer

Filed under: Blog — krkhan @ 9:33 pm

The standard way of including source code listings in Beamer is to use the semiverbatim environment. Needless to say, it does not provide all the syntax highlighting and line-numbering love of the listings package. Combine the two and you have something pretty as well as extremely helpful in delivering presentations which have code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
\documentclass{beamer}
 
\setbeamercovered{transparent}
 
\usepackage{pxfonts}
\usepackage{listings}
 
\begin{document}
 
\lstset{language=python,
        numbers=left,
        numberstyle=\tiny,
        showstringspaces=false,
        aboveskip=-40pt,
        frame=leftline
        }
 
\begin{frame}[fragile]
\frametitle{\texttt{parrot.py}}
\begin{semiverbatim}
\pause
\begin{lstlisting}
if __name__ == "__main":
 
\end{lstlisting}
 
\pause
\begin{lstlisting}[firstnumber=last]
    print "Oh yes, the, uh,
           the Norwegian Blue..."
    print "What's, uh...
           What's wrong with it? "
 
\end{lstlisting}
 
\pause
\begin{lstlisting}[firstnumber=last]
    print "I'll tell you what's wrong with it,
           my lad."
    print "'E's dead,
           that's what's wrong with it!"
 
\end{lstlisting}
\end{semiverbatim}
\end{frame}
 
\end{document}

Overlayed Listings in Beamer

Tags: , , , ,

February 26, 2010

Facebook Friends Graph v0.2 — Deb and RPM packages for Ubuntu and Fedora

Filed under: Blog — krkhan @ 2:36 am

Thanks to Christoph Korn, Ubuntu users can now install the package with a single click from the GetDeb repository. The Deb file itself is available on the release page here, along with an RPM for Fedora users.

The looks:

Facebook Friends Graph v0.2 Screenshot

And the hooks:

Changelog:

  • Fixed:
    • Bug #522735: Facebook: Application Request Limit Reached
    • Bug #523378: Connection reset by peer
    • Bug #522487: Facebook Friends Graph fails when friends have a dash in their name [patch by Little Jawa]
Tags: , , , , , , , , , , , , , , ,

February 21, 2010

Bookmark Undertaker v0.3 — Picking up the threads

Filed under: Blog — krkhan @ 8:09 pm

Threads are love. Threads are speed. And more often than not, threads are a consistent PITA. However, I’ve had an accidental epiphany just a few hours ago:

When in doubt When you need to communicate among threads, use synchronized Queues.”

There. This magic mantra will solve more issues in your life than you can ever imagine, and certainly more than I expected.

Getting back to the topic at hand, adding threading support to the program has sped up the bookmark checking process by a factor of about 435895234. Coupled with fixing of some parsing bugs, Bookmark Undertaker v0.3 is finally capable of providing a quick, stable and consistent way of sanitizing your Firefox favorites:

Boomark Undertaker v0.3 Screenshot

This time, I’ve also tried to provide Deb and RPM packages on the release page for easy installation by the Debian/Ubuntu/Fedora populace.

Ushering in the era of communist applications:

“If everyone gives one thread, the poor person will have a shirt.” — Russian Proverb

Tags: , , , , , , , , , , , , , , , ,

February 19, 2010

Facebook Friends Graph on Ubuntu

Filed under: Blog — krkhan @ 12:04 am

I never really thought anyone other than me would be interested in seeing gargantuan graphs of their friends’ connections until I found out through this post on the OMG! Ubuntu! blog that my application was included in the GetDeb repository for Ubuntu users. I have not used Ubuntu myself since about never, but apparently you can now install the application on Karmic Koala with just a few clicks.

Edit: I have now tested the installation on Karmic myself and can guarantee that it indeed works without any fuss. Gotta love Launchpad/Ubuntu.

The application itself was in a pretty much skeletal state of being so I was a little taken aback by the exposure. Nevertheless, I was reminded of the famous aphorism apropos of open source development:

“Release early, release often.” — Linus Torvalds

And indeed, the bug reports that came from users were a valuable byproduct of the Ubuntu push as I had stopped development on the script after it started working fine for me.

Tags: , , , , , , , , , , , , , , ,

February 7, 2010

Faking User-Agent with PyS60

Filed under: Blog — krkhan @ 12:00 am

“Anyone who slaps a ‘this page is best viewed with Browser X’ label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network.” — Tim Berners-Lee in Technology Review, July 1996

People never learn. Slapping such labels is one thing, they even go as far as adopting brain-dead practices of checking user-agent strings and refusing service to any browser not originating from Redmond. For example, Opera Mobile — the sexiest mobile application on planet — works astonishingly well for Javascript websites. Nevertheless, when trying to browse my university’s academic management portal on it I am presented with a big ugly “We’re dumb, you need to open this page with Internet Explorer 6.0 or later because it uses JAVASCRIPTXX0RZ.” Even though Opera does allow spoofing of user-agent strings, the fake strings still contained “Symbian” as the operating system which still resulted in incompatibility errors.

As ever, Python came to the rescue. Firing up the Twisted framework, I created a simple HTTP proxy which modifies the user-agent string on the fly. Peaches:

Opera Mobile User Agent Spoofed
User-Agent Proxy Screeshot

The script is still very quirky and is the farthest thing from what you’d call a stable solution. You can download the inital release here. The zip file contains the tiny proxy server script as well as Twisted and Zope dependencies. Good luck with trying to counter retards who’re doing everything they can to avoid compatibility. Yes, even 14 years after Sir Tim’s veracious proclamation.

Tags: , , , , , , , , , , ,
« Previous PageNext Page »