DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Post To Snippets.dzone Bypassing The Web UI

11.19.2008
| 2562 views |
  • submit to reddit
        
Takes the input from file passed as ARG to script.
Input file has the format:
- first line, post title
- second line, post tags (sepparated by white space)
- rest of file post content


#!/usr/bin/ruby



require 'net/http'

require 'uri'

require 'cgi'



username = "USER"

password = "PASS"

private = "1"



if ARGV.length != 1

	raise

	return

end	



fileName = ARGV[0]



file = File.open(fileName, "r")

text = file.readlines

title = CGI::escape(text[0])

tags =  CGI::escape(text[1])

content = ""

text.each_with_index do |line, idx|

	if idx > 1

		content = content + CGI::escape(line) + "\n"

	end

end



url_home = URI.parse('http://snippets.dzone.com')

url_login = URI.parse('htttp://snippets.dzone.com/account/login')

url_post = URI.parse('http://snippets.dzone.com/posts/create')



# GET request for session id

resp, data = Net::HTTP.new(url_home.host, url_home.port).get('/', nil)

cookie = resp.response['set-cookie']



# POST request for loging in

data = "username="+username+"&password="+password

headers = {

	'Cookie' => cookie,

	'Referer' => 'http://snippets.dzone.com/',

	'Content-Type' => 'application/x-www-form-urlencoded'

}

resp, data = Net::HTTP.new(url_login.host, url_login.port).post('/account/login', data, headers)

cookie2 = resp.response['set-cookie']



data = ""

headers = {

	'Cookie' => cookie2,

	'Referer' => 'http://snippets.dzone.com/account/login',

	'Content-Type' => 'application/x-www-form-urlencoded'

}

resp, data = Net::HTTP.new(url_login.host, url_login.port).post('/user/tsalomie', data, headers)

matchedData = /name=\"post\[user_id\]\" value=\"\d\d\d\d\"/.match(data)[0]

uid = /\d\d\d\d/.match(matchedData)[0]



# POST new entry

data = 'post[id]=&post[user_id]='+uid+'&post[title]='+title+'&post[content]='+content+'&post[tag_list]='+tags+'&post[private]='+private

headers = {

	'Cookie' => cookie2,

	'Referer' => 'http://snippets.dzone.com/user/'+username,

	'Content-Type' => 'application/x-www-form-urlencoded'

}

resp, data = Net::HTTP.new(url_login.host, url_login.port).post('/posts/create', data, headers)



case resp

	when Net::HTTPSuccess, Net::HTTPRedirection

		puts resp.body

	else

		resp.error!

	end