Skip to content

Instantly share code, notes, and snippets.

@bradfordcp
Created July 13, 2011 22:40
Show Gist options
  • Save bradfordcp/1081499 to your computer and use it in GitHub Desktop.
Save bradfordcp/1081499 to your computer and use it in GitHub Desktop.
Retrieve nodes from a D6 installation and push them to the services module on D7
#! /usr/bin/env ruby
require 'rubygems'
require 'uri'
require 'net/http'
require 'json'
require 'pp'
require 'mysql'
SERVER = "http://drupal.example.com/api/blog"
params = {
'username' => 'admin',
'password' => 'password'
}
url = URI.parse(SERVER + '/user/login')
req = Net::HTTP::Post.new(url.path)
req['Accept'] = 'application/json'
req.set_form_data(params, '&')
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
puts res.error!
exit
end
data = JSON.parse(res.body)
puts "Logged In"
mysql = Mysql.new(nil, 'rails', nil, 'd6_source', nil, '/tmp/mysql.sock', nil)
range_start = 1280188800
range_end = 1308009599
node_results = mysql.query("SELECT * FROM node WHERE created > 1280188800 AND created < 1308009599 AND type = 'blog'")
node_results.each do |node_row|
node = {
'type' => node_row[2],
'title' => node_row[4],
'date' => Time.at(node_row[7].to_i).strftime("%Y-%m-%d %H:%M")
}
body_results = mysql.query("SELECT * FROM node_revisions WHERE nid = #{node_row[0]} AND vid = #{node_row[1]}")
body_results.each do |body_row|
node['body'] = body_row[4]
end
user_results = mysql.query("SELECT * FROM users WHERE uid = #{node_row[5]}")
user_results.each do |user_row|
node['user'] = user_row[1]
end
pp node
# Node Save Logic
params = {
'node[type]' => node['type'],
'node[title]' => node['title'],
'node[body][en][0][value]' => node['body'],
'node[name]' => node['user'],
'node[date]' => node['date']
}
url = URI.parse(SERVER + '/node')
req = Net::HTTP::Post.new(url.path)
req['Accept'] = 'application/json'
req['Cookie'] = data['session_name'] + '=' + data['sessid']
req.set_form_data(params, '&')
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.error!
end
pp JSON.parse(res.body)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment