Auto Blogging with Rake & Rails

I have a long running blog that I struggle to keep current. The blog centers on mountain bike racing scene here in Northern California, so the content needs to stay timely as there are races every weekend all year long. Unfortunately, I don’t have a lot of free time to devote to it. Long story short, I wanted to automatically create a post every Thursday morning to let people know what races were being held that weekend. You can see an example of what I wanted at NorCal MTB Racing blog.

The basic idea was to create a cron job that pulls a list of races (with a link for details) from my rails application’s database, plot the events on map, add some text and then post it to my blog on Blogger. Most of this is pretty standard rails stuff, but I did need some help for the map and posting.

For the maps, I used Google Static Maps. This allows you to embed a Google Maps image on your webpage just like a normal image. All you need to do it feed it the right parameters. For example, this HTML code:

alt="NorCal MTB Race Event Map"

Produces this map:,New+York,NY&zoom=13&size=600x300&maptype=roadmap&markers=color:blue%7Clabel:S%7C40.702147,-74.015794&markers=color:green%7Clabel:G%7C40.711614,-74.012318&markers=color:red%7Clabel:C%7C40.718217,-73.998284

NorCal MTB Map

To post to Blogger, I used the blogger gem (installation instructions). There are other ruby gems out there that interface with Blogger, but this was by far the most straight forward. To use the gem, you need username, password and blog ID. I found my blog ID through an answer in the blogger support forum:

Got to your dashboard and click Settings for the blog you want the ID of. In the address bar you should then see something like this: The long number at the end of the URL is your blog’s ID number.

I decided that this would be best implemented as a rake task, so I could full access to all the rails application environment (especially the database connection). Here is the complete rake task:

 1namespace :blogger do
 2  desc "post weekend update to blogger"
 3  task :weekend_update  => :environment do
 4    require 'blogger'
 6    # preferences for blogger login
 7    username = '' # change this to your username
 8    password = 'xxx129304xxxx' # change this to your password
 9    blog_id = '123456' # change this to your blogid
11    # my entry preferences
12    days_ahead = 5 # how many days of events ?
13    blog_entry = "For those of you making your weekend plans,
14                  here is quick rundown of this weekend\'s MTB racing schedule
15                  for NorCal:\n\n"
17    # google static map preferences
18    base_url = ""
19    map_params = ["size=400x400", "maptype=roadmap", "sensor=false"]
21    # find all events in the db for this weekend
22    @events =
23        "norcal",
24        :conditions => ["start_date >= CURRENT_DATE AND start_date < ?",
25                        days_ahead.days.from_now],
26        :order => "start_date asc")
28    # loop through events adding place markers to the google static map URL
29    x = 0
30    @events.each do |marker|
31      x += 1
32      map_params << "markers=color:blue|label:#{x}|#{},#{marker.lng}"
33      blog_entry += "#{x}. [#{}]({})
34                    (#{marker.start_date.to_s(:compact)})\n"
35    end # marker loop
37    # add the google static map to post body
38    map_pic_url = base_url + map_params.join('&')
39    blog_entry += "\n![NorCal MTB Race Event Map](" + map_pic_url + ")"
41    # create title for the post
42    blog_title = "NorCal MTB Racing This Weekend:
43                  #{} -
44                  #{days_ahead.days.from_now.to_date.to_s(:short)},
45                  #{}"
47    # login into blogger and post this draft
48    account     =,password)
49    new_post    =      => blog_title,
50                                    :content    => blog_entry,
51                                    :formatter  => :rdiscount,
52                                    :draft      => true)
55  end # task    
56end # namespace

Although I did this with a rake task, you could also make it just a regular ruby script. However, then you would need to handle all of the database login and queries yourself.

See also