Ruby script to batch find-and-replace in LiveJournal entries

Written . Tagged LiveJournal, Ruby.

I got a request for a script like my Ruby script to make LiveJournal entries private in batch, but to replace a string in all entries (e.g. if you’ve changed the URL of your web site).

I usually do not do commission work for free, but I dislike the idea of someone having to spend ages on something I could do in a few minutes, and I had those minutes to spare. So voilà, lj_batch_replace.rb:

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
#!/usr/bin/env ruby
# By Henrik Nyh 2007-09-11.
# Free to modify and redistribute with due credit.


# Change these:

USERNAME = 'frank'
PASSWORD = 'sesame'

REPLACE_FROM = 'myoldsite.com'
REPLACE_UNTO = 'mynewsite.com'


# Leave these alone unless you know what you're doing:

require 'rubygems'
require 'livejournal/login'
require 'livejournal/entry'

# So the livejournal gem doesn't choke:
# http://henrik.nyh.se/2007/04/ruby-livejournal-batch-private#comments
LiveJournal::Entry::KNOWN_EXTRA_PROPS << "used_rte"

puts "Logging in..."
user = LiveJournal::User.new(USERNAME, PASSWORD)
login = LiveJournal::Request::Login.new(user)
login.run
puts "Login response:"
login.dumpresponse

puts "Getting entries..."
entries = LiveJournal::Request::GetEvents.new(user, :recent => -1).run.values
entries = entries.sort_by { |e| e.time }

puts "Processing entries..."
entries.each do |e|
  puts "#{e.time} #{e.subject}"
  e.event = e.event.gsub(REPLACE_FROM, REPLACE_UNTO)
  LiveJournal::Request::EditEvent.new(user, e).run
  puts e.event
end

puts "All done."

As with the batch private script, you need to install a Ruby interpreter, install RubyGems, install the LiveJournal gem (e.g. sudo gem install livejournal), configure the values at the top of the script and then run it (e.g. ruby lj_batch_replace.rb).