I’ve created an easy to use cross platform desktop translator using the Titanium runtime. It’s powered by Google’s Language APIs so you can translate over 40 languages accurately. No ads, no spyware and no premium version. It’s exactly the same on all platforms.
Titanium App Pre-Release 2 has just hit the shelves. This release includes Python and Ruby support/integration. As of today there is little documented and no examples about Python integration so in this tutorial I’ll show you how to make a basic application that:
Set up Python imports
Allows Python classes to be called in Javascript
This isn’t a beginners guide to Titanium, nor is it a tutorial in Python. The App itself has alot of flaws, you need to enter the full wikipedia URL (which is counter-productive) and if the ‘Page is up for citation’ it doesn’t work. Feel free to develop/redistribute a better version. The tutorial is intended to show you Python/Titanium integration.
‘
The well designed final product.
Getting Titanium
The first step is to get Titanium itself. It has native packages for Mac, Linux and Windows. Titanium Download Page. You can also build the latest and greatest version from source on github. Create a new project. As of PR2 you can choose to include Javascript libraries in your project. These link off Google APIs which are horribly slow compared to including the library locally. I believe in future releases the libraries will be bundled with the application itself. For now I recommend downloading your own copy of jQuery and including it yourself.
First Steps
We’re going to need jQuery so download yourself a copy and put it in the Resources directory. Open up index.html and include jQuery..
body{background-color:#292929;color:white}#content{background:black; } a {color:orange;}
And set the HTML..
<h1>Quicky</h1>
<p>
<input type="text" id="f1" style="width:300px;" />
<input type="submit" id="s1" value="Look up Wiki Page" />
</p>
<div id="content">
</div>
Code Logic
When the “Look up Wiki Page” is clicked, Javascript will capture the value of the form and send it to a Python function. We will then capture the response in Javascript and display it on the page.
The Python
For the application we are going to use the python library pyQuery. It makes selecting HTML elements and getting their values a breeze. Download it and put the directory containing the pyquery files in the Resources directory. As of PR2 (march 4th, 09) Python imports do not work properly. They don’t check for the file specified in the local directory but do check the Titanium’s python path. To get around this we can just add the Resources directory to the python path. We want to add the below code to our Index.html file so we need to wrap it in script tags.
We are going to write our own file/class for the wiki “scraper”. In Resources create a new file called wiki.py
importos, sysfrom pyquery import PyQuery as pq
fromurllibimport FancyURLopener
class MyOpener(FancyURLopener):
version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'class WikiGet:
def fetch(self,url):
myopener = MyOpener()
page = myopener.open(url)
read = page.read()
d = pq(read)
output = d("p:first").html()return output
Our fetch method takes the parameter url, which will be “declared” by Javascript. We need to declare our own urllib class as Wikipedia requires a genuine user agent (otherwise a 403). As you can see the poorly written hackish code, we grab the first paragraph element on the page. We then return output. printing does not work!
Back in Resources/Index.html we need to include the wiki class that we just wrote. We can do this with a basic Python import. (The below code goes after the import ‘hack’ we wrote above). To use our Python class that we just wrote, we need to expose it to the API. To do that we attach the class to the window object. In this example we are attaching a class but it can easily be a method or variable.
from wiki import*
window.wiki = WikiGet()
Thats the last of the Python code, we can finish it up in Javascript . The below code is jQuery, when the HTML submit button is pressed it gets the value from the form and sends it to the method fetch. Remember in the code above we attached the WikiGet() Python class to window.wiki? We can now access that in Javascript through wiki (ignoring the window. prefix). Since fetch() was a method of our class, we call it normally (class.method). We pass the parameter (the wikipedia URL) and save the output in the variable result. Finally we set the div#content’s innerHTML to result.
I recently installed the 64 bit version of Sidux, a Debian based Linux distro, on my desktop. Sidux uses bleeding edge packages, the opposite of Debian Etch. I’ve always used a 32 bit distro and flash installation has always been straight foward. Flash 10 was recently released and Adobe offer a native Linux 64bit version. It’s pretty easy to install.
Download it from the Adobe website.
Close Firefox/Iceweasel
Extract it, then copy it to the ~/.mozilla/plugins/ directory
I wanted to make an IRC bot for some channels I’m on that can do ‘useful’ things. A not-so-useful but still pretty cool thing was tweeting from within IRC. The system isn’t dynamic - you need to hard code the twitter account’s username and password in but it works. I figure this will be useful in a collabarated project - you don’t need to give everyone the password to the twitter account, but they can still post to it. This isn’t going to be a redistributable bot, its a hack.
I’m basing my bot off of one provided by Twisted in their demos. Linky. It logs messages said in the channel to a file, which is a must for any bot. Building on that, it’s fairly easy to implement posting to twitter. We need to import a couple more Python modules. re (regular expressions) comes with Python, you need to download python-twitter and install it from here.
importreimport twitter
Around line 100 there is:
if msg.startswith(self.nickname + ":"):
msg = "Hello %s I am powered by Python"%userself.msg(channel, msg)self.logger.log("<%s> %s"%(self.nickname, msg)
We can copy that block of code and use it to build additional ‘modules’ on. Just after that block we can start building our Twitter poster. We want the user to input ~twitter “message here”.
if msg.startswith("~tweet"):
We then need to get the text they are tweeting. We can do this with a rough regular expression.
tweet = re.findall(r'"(.*?)"', msg)
Now that we’ve got the message we can easily post it to twitter.
api = twitter.Api(username='twitter_username', password='twitter_password')
status = api.PostUpdate(tweet)
The message has posted, let’s return a message to the channel saying that the tweet was successful and log it.
At the moment it’s pretty basic. Anyone can say ~tweet “I am an idiot” and it wll appear on your twitter page. We can easily setup nick name authentication.
ifuser == 'ircnickname':
So, the full code.
ifuser == 'nickname':
if msg.startswith("~tweet"):
tweet = re.findall(r'"(.*?)"', msg)
api = twitter.Api(username='twitter_username', password='twitter_password')
status = api.PostUpdate(tweet)self.msg(channel, 'Tweet Sent')self.logger.log("<%s> %s"%(self.nickname, 'Tweet Sent'))
I’ll be writing some more ‘modules’ and posting them later.
A few months ago I bought my Sciphone from DealExtreme for $130 ish USD. Since then a new online store, mysciphoneshop.com, has appeared and it offers a wider range of Sciphones including replacement screens, cables, headphones and a wider range of phones themselves. They offer a newer version of the Sciphone i68+ , which I bought from DealExtreme, for $120 which is $10 less. From the looks of things it’s faster, slimmer and it allows you to change the icons on your ‘desktop’ and background image. They also offer a Sciphone that comes with the Google Android operating system and it has Wifi! It has a 1500 mAh battery, apposed to the i68+’s 1200 mAh so it should last just as long, if not longer. This Android phone is $175, a bargain. Wifi, Android and no contract. If my current i68+ breaks down, I’m probably going to buy one of those.