Category Archives: Web Programming

Some example python code to fetch weather forecasts…

Need to get some weather information? The website forecast.io has a nice web based service you can use up to one thousand times a day for free. I was thinking of using it for an automated sprinkler application, but just to test it, I wrote this simple chunk of python to try it out. To use it yourself, you’ll need to get your own API key and modify it to use your own latitude and longitude. It’s not that amazing, but you might find it of use.

[sourcecode lang=”python”]
#!/usr/bin/env python

# __ _
# / _|___ _ _ ___ __ __ _ __| |_
# | _/ _ \ ‘_/ -_) _/ _` (_-< _|
# |_| \___/_| \___\__\__,_/__/\__|
#
# A python program which used some publically available
# web apis to find out what the forecast will be.
#

# You’ll need an API key below… you get 1000 requests per day for free.
# Go to forecast.io and sign up.

API="PUT_YOUR_OWN_API_KEY_HERE"
URL="https://api.forecast.io/forecast/"

# Your latitude and longitude belong here, I use SF for example
LAT= 37.7833
LNG=-122.4167

directions = ["N", "NNE", "ENE", "E", "ESE", "SSE", "S", "SSW", "WSW", "W", "WNW", "NNW"]

def bearing_to_direction(bearing):
d = 360. / 12.
return directions[int((bearing+d/2)/d)]

import sys
import os
import time
import optparse
import json

import urllib2

now = time.time()
cached = False

if os.path.exists("WEATHER.cache"):
f = open("WEATHER.cache")
parsed = json.loads(f.read())
f.close()
if now – parsed["currently"]["time"] < 900:
cached = True

if cached:
print "::: Using cached data…"
else:
print "::: Reloading cache…"
req = urllib2.Request(URL+API+"/"+("%f,%f"%(LAT,LNG)))
response = urllib2.urlopen(req)
parsed = json.loads(response.read())
f = open("WEATHER.cache", "w")
f.write(json.dumps(parsed, indent=4, sort_keys=True))
f.close() ;

c = parsed["currently"]
print ":::", time.strftime("%F %T", time.localtime(c["time"]))
print "::: Conditions:", c["summary"]
print "::: Temperature:", ("%.1f" % c["temperature"])+u"\u00B0"
print "::: Dew Point:", ("%.1f" % c["dewPoint"])+u"\u00B0"
print "::: Humidity:", ("%4.1f%%" % (c["humidity"]*100.))
print "::: Wind:", int(round(c["windSpeed"])), "mph", bearing_to_direction(c["windBearing"])

d = parsed["daily"]["data"][0]
print "::: High:", ("%.1f" % d["temperatureMax"])+u"\u00B0"
print "::: Low:", ("%.1f" % d["temperatureMin"])+u"\u00B0"

d = parsed["hourly"]["data"]

for x in d[:12]:
print time.strftime("\t%H:%M", time.localtime(x["time"])), x["summary"], ("%.1f" % x["temperature"])+u"\u00B0"
[/sourcecode]

Lightweight Web servers

Don’t you hate it when you see something that you want to investigate further, but then you can’t remember what the project is called? That’s what happened to me: I recently saw some cool little web server, implemented as a single C file, and that could either embed or be embedded in other applications.

And for the life of me, I can’t remember the name.

While searching, I did find this cool list of lightweight web servers that was collected by IBM. It’s got a lot of them, and included most of the ones I knew about, but none seem to be the one I was looking for. I’ll keep looking, but I’m archiving this link because it will undoubtedly be useful in the future.

List of Lightweight Web servers, courtesy of IBM

Addendum: The one I was looking for was Mongoose..

Addendum2: The link above appears to be dead. But Wikpedia provides this comparison of lightweight webservers which may provide links to the information you need. I’ve used nginx, mongoose, boa, and thttpd, and all seemed very good with some interesting varying features. Check them all out.

Q: Should blogs make font choices for you, or not?

I’ve been making some minor tweeks to the excellent 1024 px WordPress theme that I started using a few weeks ago. I found a small issue with the CSS for images that are supposed to be center (a priority mistake meant it didn’t work) and I’ve made a few other minor tweaks. I finally got around to considering some questions regarding fonts, and I thought I’d ask you, my readers, give me the benefit of your opinions.

The 1024px stylesheet listed Verdana, Tahoma, Arial, and sans-serif as the search order for fonts. Not a bad list really, I think Verdana is an excellent screen font, with excellent legibility and good weight. Since my eyes have become somewhat presbyopic, and I spend a great deal of time reading stuff on screen, I’ve become somewhat more sensitive to these kind of issues.

But there are a couple of problems with the defaults.

First of all, Verdana and Tahoma (which are truly excellent, it must be said) aren’t really universal. I believe that they are installed by default on Windows and Mac OS, but not on most of the Linux installations that I have seen. On most Linux boxes, I end up using Dejavu Sans as a substitute.

If you don’t have either Verdana or Tahoma, this theme falls back onto Arial. I do have Arial installed on most of my systems, because lots of things need Arial or Helvetica. But here’s the thing: Arial is really ugly. It’s not pretty even in print, but it’s just wonky to use on screen. Sadly, this is what I get a lot of the time.

And, of course, if you don’t have any of those three, it falls to the browser default sans-serif, whatever the system default is, or whatever you’ve chosen.

Okay. So, I thought that perhaps i should just leave it up to you. I’ve all the font selections from the theme’s stylesheet. Whatever default you configure is what you get.

And yet, I’m not happy with that either. It appears (for instance) that on Mobile Safari, the default is always a serif font, and you can’t change it. That’s not very good: I think serif fonts are virtually by definition harder to read on screen. Mobile Safari does support Verdana. I could actually make a special style sheet just for my blog, but that seems to be a slippery slope.

So, the question is: do you think web page authors (and in particular, blog authors) should make font choices for you? If so, what choices are reasonable? If not, are there drawbacks?

Feel free to leave a comment below.

Addendum: Okay, I shifted back to specifying fonts. By default, mobile browsers like Safari on the iPad and iPhone seem to resort to a serif font, which on a tiny screen is simply not a good idea. The list I came up with was Verdana, Tahoma, DejaVu Sans, and then whatever “sans-serif” is. Arial is just too ugly to use.

The Broadcaster Project, revisited

One year ago today, I first published a link to The Broadcaster Project, a site which had several tips on using command line tools such as ffmpeg to assemble videos. I use a similar technique to do my more recent videos: I take the raw footage from the camera and resize it, denoise it, and add some overlaid text before uploading it to YouTube. Revisiting The Broadcaster Project, I see they have a few more recipes that can be useful, such as ones for assembling time lapse movies and the like. I also found a reference to youtube-upload, a command line python script that can automatically upload videos from the command line, a part of my process that I still did with the web interface.

HOWTO make a simple HTTP server…

It’s occasionally useful to have a basic HTTP running to serve the files in a directory. You might want to fetch some MP3 or movie files from one machine, or in my case, a PDF document that I’m hacking on my Linux box while ssh’ed in from my Windows laptop. In the past, I have used Jef Poskanzer’s mini-httpd, but there is an even simpler way, especially if you already have Python installed.

Just “cd towhatever directory”, and then type “python -m SimpleHTTPServer”. Voila! An HTTP server running, bound to port 8000 on that machine.

Very useful.

Revisiting TinyP2P

As I was driving in this morning, I entertained a train of thought that led me back to thinking about peer to peer networks. I recalled that I had seen a posting by Ed Felton some years ago about implementing a peer-to-peer networking scheme in a very few lines of Python. A few minutes of searching once I reached my desk reminded me that the name of this was TinyPTP, and that it was 15 lines of dense Python. A search of Ed’s Freedom To Tinker Website revealed nothing but dead links, but a few minutes with the Internet Wayback Machine resurrected it for our consumption:

TinyP2P

I recall that I tried to run this before, and had some difficulty, but haven’t tried again. Still, the basic idea is pretty interesting: you create a network of XMLRPC servers, whose basic functionality is to ensure that all network nodes have copies of all files. It uses the SimpleXMLRPCServer classes and some overly terse idioms to accomplish its task.

Here’s the thinking that leads me to here: I’ve been listening to a lot of stuff about the scalability of Facebook lately. Ironically, the scalability doesn’t contribute to user experience directly: no user uses very much of any of the Facebook resources. Facebook’s 60,000+ servers serve something like 200M+ users every day, so obviously each user is getting a very tiny fraction of any single server machine, a fraction so small that it could be easily subsumed by even the most tiny computing appliance. It is only the centralization which causes the need for scalability.

And, of course, a distributed architecture has the possibility of fixing a few of Facebook’s other ills, such as allowing more direct control over privacy.

So, here’s the idea: use ideas similar to the TinyPTP client to implement a distributed social network like Facebook. Implement it in a simple language like Python, and in a straightforward way (don’t be overly terse like TinyPTP). Pay attention to security, but also make it simple enough so that people can adopt it by downloading a single Python file and running it in a directory of their choosing on their machine at home.

It’s just a thought experiment at the moment. We’ll see if it gains traction in my head.

Lightbox JS

Here’s a useful little chunk of Javascript which can probably be put to good use on your website:

Lightbox JS is a simple, unobtrusive script used to to overlay images on the current page. It’s a snap to setup and works on all modern browsers.

It works quite well, and is also somewhat instructive. I may work on my own version of this script to use here on my website.

Lightbox JS

[tags]Web Programming,Javascript[/tags]

Addendum: I’ve removed the AJAX tag, since this actually doesn’t do any asynchronous Javascript calls.

Bye Bye, Freeloaders

Don't Bogart My Bandwidth!

I received an email a couple of days ago from someone who thoughtfully noted that someone was stealing bandwidth from my by hotlinking to images in my /images directory. Effectively these people use your webserver to serve images for their websites. It’s tacky: kind of like filling your pockets with napkins and packets of ketchup when you go to a fast food restaurant. Any individual act is admittedly pretty trivial, but as of noon today, 546 images had been served to these people. Sigh.

A bit of research yielded a recipe for preventing this. Since I installed it a half hour ago, 46 further accesses have been routed to a banner image that will hopefully be less attractive.

All of my images are just a click away, but if you are going to swipe them, at least use your own bandwidth to distribute them.

Update: Two people have noted this broke reading from bloglines. I’ll fix it shortly.

Microsoft Virtual Earth is out…

This is getting a lot of play everywhere, but Microsoft’s new mapping application, Virtual Earth is now live. In most respects, I find it very similar to Google’s offering, but the API seems a bit more refined, and it doesn’t have the problems associated with generating usage keys. You can find information about programming the api here, or the simplest example here. You can pan around, or double click somewhere to zoom in. I’ll try to work up some bigger examples later.

Return of Design – Individual Color

I’m not color saavy savvy enough to actually design a good color scheme for websites. What I did years ago was come up with a particular blue (#336699) and just chose to use it as the predominant color for my website. I keep thinking that it would be nice to add some different colors, but frankly, I never get around to it.

Today, I discovered that returnofdesign.com had a cool webpage which you can use to find nearby hues, variations in saturation, and complementary colors. For instance, here is the page for brainwagon blue. Very nifty!

AXS and WordPress Integration Plugin (2.1)

AXS is a pretty nice little program that can gather many of the web usage statistics that are provided by statcounter.com, but without any limits. There is a new AXS and WordPress Integration Plugin that makes it very simple to add to your WordPress setup: basically just put the plugin where they all go, configure three short options, and voila.

Check it out if you are in need of such a thing.

Minimo on Pocket PC

I had heard about the open source browser Minimo, but they just recently released their first trial build.

You can download a sneak peak at this build here:
http://www.meer.net/~dougt/minimo_ce/MinimoCE_0.002.zip. Keep in mind,
it is basically the second build that I made that actually rendered a
page successfully. When you check it out, remember I told you: Lots of
work to do; Lots of work to do.

I’ll have to check that out when I get home. Stay tuned for an update.