Code Shop http://journlr.com A bit of Ruby, a bit of JavaScript, a bit of love... posterous.com Thu, 01 Oct 2009 22:21:00 -0700 Simple Vim Posterous Plugin http://journlr.com/simple-vim-posterous-plugin http://journlr.com/simple-vim-posterous-plugin

I like writing in Vim! It is one of those things that I am so used to
that every other text editor feels unnatural and going against the
grain. So I thought I'd write a small plugin to post to Posterous. Add
the following code to your .vimrc. Also make sure you have python
enabled in vim as well as have markdown2 for python installed.

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
python << EOF
# Post to Blog
import vim
import urllib2, urllib
import base64
import markdown2
 
blog_email = 'my@email.com'
blog_password = 'mYpA$$W0RD'
site_id = "123456"
api_url = 'http://posterous.com/api/newpost'
 
def post_blog():
    title = vim.current.buffer[0]
    body = markdown2.markdown('\n'.join(vim.current.buffer[2:]))
 
    params = urllib.urlencode({
        'site_id': site_id,
        'title': title,
        'body': body,
        'autopost': '0'
    })
 
    req = urllib2.Request(api_url, params)
    base64string = base64.encodestring('%s:%s' % (blog_email, blog_password))[:-1]
    req.add_header("Authorization", "Basic %s" % base64string)
    handle = urllib2.urlopen(req)
    print handle.read()
 
    vim.command('set nomodified')
EOF

The following need to be changed: blog_email, blog_password, and
site_id. site_id can be grabbed by running the following on the
terminal:
curl -u my@email.com:mYpA$$W0RD http://posterous.com/api/getsites
Substitute the email and password. You will get a list of your blogs
so choose the id of the blog you want to post to.

The format of the text should be as follows: the first line is the
title and the proceeding lines are the body:
This is the title of the post This is the body of the post.

This code is pretty straightforward and sparse because I am using this
to post code snippets to a private blog. Check out the Posterous API to add more
options and whatnot. Also take this as a start and expand upon it.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Thu, 01 Oct 2009 03:24:00 -0700 Simple Clojure Http Client http://journlr.com/simple-clojure-http-client http://journlr.com/simple-clojure-http-client I'm learning Clojure and needed a simple http client. Here is the one
I have written using the java libraries.

(import '(java.net URL HttpURLConnection) '(java.io InputStreamReader BufferedReader)) (defn http-client-get-content [reader content] (let [line (.readLine reader)] (if (nil? line) content (recur reader (str content "\n" line))))) (defn http-client-get [url] (let [connection (.openConnection (URL. url))] (doto connection (.setRequestMethod "GET") (.setDoOutput true) (.setReadTimeout 10000) (.connect)) (http-client-get-content (BufferedReader. (InputStreamReader. (.getInputStream connection))) "")))

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Mon, 14 Sep 2009 21:05:00 -0700 Increasing Swap Space http://journlr.com/increasing-swap-space http://journlr.com/increasing-swap-space

Lately, I've been trying to compile Chromium on my desktop and I had been constantly getting an error associated with linking:

collect2: ld terminated with signal 9 [Killed]

After Googling around apparently the problem had to do with running out of swap space which causes the linking to fail. Chromium needs almost 700mb more memory to link than I was providing so it was failing. Interestingly, Linux has a really clean solution to increase the swap space.

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576 sudo mkswap /swapfile sudo swapon /swapfile

These three commands will create an empty file of 1gb, make that a swap file, and turn it on to be used.

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Thu, 06 Aug 2009 00:55:10 -0700 Economist Mailer Bot http://journlr.com/economist-mailer-bot http://journlr.com/economist-mailer-bot There is one thing I have to admit Posterous has taught me and that is that email can be a powerful medium. I spend a lot of time in email even if I don't realize it and usually always have my Gmail window open.

Anyways, I enjoy reading the Economist, but it's quite difficult to figure out how much of the magazine is left other then a wild guess so I wrote an Economist Mailer Bot which essentially sends out a bunch of emails (one email per article) based on a cron schedule that you set (careful if you set it too soon your'll get a lot of duplicates).

The nice thing about this is that you can see you progress through your article queue (especially if you use filters) as well as being able to read the articles on my iPhone email client when on Bart. Yay, no need to write an app. 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Fri, 31 Jul 2009 22:40:22 -0700 Loading images from the Application Directory on iPhone http://journlr.com/loading-images-from-the-application-directory http://journlr.com/loading-images-from-the-application-directory This is how you load an Image from the local application directory on the iPhone.

   [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Polareye" ofType:@"jpg" inDirectory:@""]];

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Tue, 28 Jul 2009 21:42:31 -0700 Twitcures http://journlr.com/twitcures http://journlr.com/twitcures When I last wrote about my NCurses Twitter client Twitcurses I just sort of glaced over it. So here is a bit more detailed usage of the app. It basically looks something like this.


The client refreshes after a set amount of time as set in the config file. I have set the default to 2 minutes which can be reset to whatever you please. But note there is a limit of 140 requests an hour (hey I just got why they limit it to 140...). To refresh your tweets press a capital R.

Sending a tweet is as easy as pressing a cap T. Which should pop up a message requesting you for a tweet.



If you want to cancel just enter a blank line.

Also the cool thing I added is using the Mac 'say' command to speak your new Tweets from the last time you refreshed. So basically whenever it refreshes it speaks any new tweets that have been created after the last time you refreshed. (This means it won't speak the first time you load the app since that will result in it speaking all of the tweets). I will add a mute feature promptly for the time when you don't want it to speak, also a config option for this would be useful.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Mon, 27 Jul 2009 11:35:00 -0700 iPolarized - an open source Polaroid app for iPhone http://journlr.com/ipolarized-an-open-source-polaroid-app-for-ip http://journlr.com/ipolarized-an-open-source-polaroid-app-for-ip

Here is yet another piece of code that I am making open source. It is called iPolarized. I am not done with it, but thought I'd make it open source in the early process. The major functionality that of making the picture a polaroid is done, but all the stuff related to making the app intuitive is sort of missing, plus there are a few bugs in there I need to work out.

Here are some example photos:

Anyways, enjoy the code here I am releasing it under GPL3. And for this project please donate if you find this code useful. I really need money!

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Mon, 27 Jul 2009 00:16:46 -0700 NCurses based Twitter Client http://journlr.com/ncurses-based-twitter-client http://journlr.com/ncurses-based-twitter-client With all the Twitter based programs that are out there I thought I'd add my contribution to that mix. It's a simple ncurses based twitter client. It still needs a bit of work, but it's in a dogfooding stage. Download the code here.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Fri, 10 Jul 2009 21:09:00 -0700 Command Line Flickr Uploader http://journlr.com/flickr-uploader http://journlr.com/flickr-uploader

I dislike GUI tools and posting to Flickr usually seems like a chore more than as an enjoyable means of posting new pictures. With that in mind I have written this command line Flickr uploader. It will be a constant work in progress where I will add features as I need to use them. So with that in mind download it from here.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Thu, 09 Jul 2009 22:40:00 -0700 Posterous and Github Gists http://journlr.com/posterous-and-github-gists http://journlr.com/posterous-and-github-gists

In a sign of being awesome Posterous now lets you post Github Gists which are automatically converted into the embed code. Basically, what you need to do is paste the link to the gist in question and it is automatically translated into the embed code.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Thu, 09 Jul 2009 17:00:00 -0700 html5 WebWorker http://journlr.com/html5-webworker http://journlr.com/html5-webworker

Click here to download:
webworkers.tar.gz (0 KB)

JavaScript doesn't have threads which is annoying to the say least when you need to do multiple things at once or even need to poll a service. Services such as gmail have gotten away with it by using iframes which they use to communicate with the various backend services. Coming up with a form of threading that works in JavaScript has resulted in some interesting models. However, html5 introduces a form of threading into the fold which should result in a cry of joy which are called WebWorkers.

The model of the WebWorker is more along the lines of the actor model through message passing as opposed to the mutex driven threading we all know and "love." Let us see an example of how this works.



This example shows how you can run two threads together. The first thread fib goes on an infinite loop finding the Fibonacci sequence. Without threads on a tradition browser this would lock up the view rendering it useless, but with WebWorkers we see that we can continue using the window. The worker does a postMessage whenever a new Fibonacci sequence has been calculated. The worker defines a handler called onmessage which shows allows you to define what to do with a worker response. The second worker sayhello shows how one can send and receive messages from the worker. If you define an onmessage handler in the worker script, messages can be passed to the worker which can then be handled by that thread.

Currently, the WebWorker is supported under Safari, Chrome and Firefox. Hopefully, more browsers (looking at you Redmond) support more of html5 so we can begin using the next generation of web technology and end the current generation's hacks.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Wed, 08 Jul 2009 16:32:00 -0700 Mephisto to Posterous http://journlr.com/mephisto-to-posterous http://journlr.com/mephisto-to-posterous

Here is the code for Mephisto to Posterous. Hmm seems all my code on this blog is about Posterous. Need to diversify.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Thu, 28 May 2009 03:59:00 -0700 wp2posterous: WordPress to Posterous Importer http://journlr.com/wp2posterous-wordpress-to-posterous-importer http://journlr.com/wp2posterous-wordpress-to-posterous-importer

Here is the code to import WordPress blog into Posterous. Basically what
happens is the post is imported with the categories becoming tags. Other
then that it should import fine. I added a second delay between post so
it doesn't load up their server. 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Sat, 23 May 2009 20:37:00 -0700 Posterous posting code. http://journlr.com/posterous-posting-code http://journlr.com/posterous-posting-code

This is some JRuby code used to publish some existing posts from a database to posterous. Just a simple script which also shows how to use JDBC and net/http.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra
Sat, 28 Mar 2009 19:38:00 -0700 Being Thrifty with Thrift http://journlr.com/being-thrifty-with-thrift http://journlr.com/being-thrifty-with-thrift

I've been using Thrift a RPC  framework from Facebook. It allows you to define a service outline and generate a template in various languages which you can then use to create the service. What this means is you can say use Haskell for you server and hook it up with a Ruby client or vice versa. I installed thrift on my Mac from the subversion repository. Here are the commands to install the pre-reqs and thrift itself assuming you have MacPorts. sudo port install boost libevent svn co https://svn.apache.org/repos/asf/incubator/thrift/trunk thrift cd thrift ./bootstrap ./configure --prefix=/opt/local/ make make install Now that's settled here is an example of a thrift definition file. Named myauth.thrift


namespace rb MyAuth
namespace py myauth

struct User {
1: string username,
2: string password
}

enum LoginStatus {
SUCCESS,
FAIL
}

service Authentication {
string say_hello(),
LoginStatus login(1:User cred)
}

As you can see this has several components to it. The first the namespace definitions. These are the modules/packages/namespaces which this service belongs to. You can define one for each of the languages that you are going to code for. The second is the User struct which holds the data that we will be working with in the service, third the enums and finally the service definition. The service definition has two methods: say_hello which returns a string and login which returns a LoginStatus taking in a User struct as an argument. This only uses a subset of the definition syntax to show a small example of what you can do with the service. To see a more thorough example of the definition file go to the Thrift Wiki. Once you have written the definition compile the file (here I'm going to use Ruby and Python): thrift --gen rb --gen py myauth.thrift Now write the server code (in ruby):


require 'thrift'
$:.push('gen-rb')

require 'Authentication'
require 'myauth_constants'

class AuthenticationHandler
def say_hello
puts "thrift client connected"
"hello thrift client"
end

def login cred
if cred.username == 'hello' && cred.password == 'world'
puts "logged in"
return MyAuth::LoginStatus::SUCCESS
end

puts "great pie of fail"
MyAuth::LoginStatus::FAIL
end
end

handler = AuthenticationHandler.new
processor = MyAuth::Authentication::Processor.new(handler)
transport = Thrift::ServerSocket.new(9090)
transportFactory = Thrift::BufferedTransportFactory.new()
server = Thrift::SimpleServer.new(processor, transport, transportFactory)

puts "Starting the server..."
server.serve()
puts "done."

Write the client code (in python):


import sys
sys.path.append('gen-py')

from myauth import Authentication
from myauth.constants import *

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

transport = TSocket.TSocket('localhost', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)

auth = Authentication.Client(protocol)

transport.open()

print auth.say_hello()

user = User()
user.username = 'hello'
user.password = 'world'

print "Login: %s" % auth.login(user)

user2 = User()
user2.username = 'failed'
user2.password = 'world'

print "Login: %s" % auth.login(user2)

What thrift gives you is a transport layer as well as a protocol layer so you don't have to worry about mucking around with sockets. There are also several protocol layers defined so you could use a JSON protocol for example. All in all this is a useful technology for using the right tool for the job, i.e the right programming language for the job. By working with different languages thrift gives the programmer the capability of writing different services using the strengths of the appropriate language.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/68361/photo.jpg http://posterous.com/people/15Zzt2e7kgV Abhi Yerra abhi Abhi Yerra