Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twitter Developers/Changing application's settings
require "nokogiri"
require "uri"
require "net/http"
UA = "Mozilla/5.0 (X11; Linux x86_64; rv:18.0) Gecko/18.0 Firefox/18.0 FirePHP/0.7.1"
@cookie = {}
def request(address, method = :get, data = {})
uri = URI.parse(address)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.port == 443
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.start do |connect|
header = {
"User-Agent" => UA,
"Cookie" => @cookie.map{|k, v| "#{k}=#{v}"}.join(";")
}
body = data.map{|k, v| "#{k}=#{v}"}.join("&")
if method == :post
response = connect.post(uri.path, body, header)
elsif method == :post_multipart
boundary = "re4kboundary"
header["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
body_multipart = ""
data.each do |k, v|
body_multipart += "--#{boundary}\r\n"
body_multipart += "Content-Disposition: form-data; name=#{k}\r\n"
body_multipart += "\r\n"
body_multipart += v
body_multipart += "\r\n"
end
body_multipart += "--#{boundary}--\r\n"
response = connect.post(uri.path, body_multipart, header)
else
response = connect.get(uri.path + "?" + body, header)
end
if response.key?("Set-Cookie")
response.get_fields("Set-Cookie").each do |str|
k, v = str[0...str.index(";")].split("=")
@cookie[k] = v
end
end
return response
end
end
def prepare_login
doc = Nokogiri.HTML(request("https://dev.twitter.com/user/login").body)
return {
"form_build_id" => doc.xpath('//form[@id="user-login"]/input[@name="form_build_id"]').attribute("value").value,
"form_token" => doc.xpath('//form[@id="user-login"]/input[@name="form_token"]').attribute("value").value,
"form_id" => "user_login",
"op" => "Log in"
}
end
def login(login, password)
data = prepare_login()
data["name"] = login
data["pass"] = password
request("https://dev.twitter.com/user/login", :post, data).get_fields("Location")
end
def prepare_update(id)
doc = Nokogiri.HTML(request("https://dev.twitter.com/apps/#{id}/settings").body)
return {
"name" => doc.xpath('//*[@id="edit-name"]').attribute("value").value,
"description" => doc.xpath('//*[@id="edit-description"]').attribute("value").value,
"url" => doc.xpath('//*[@id="edit-url"]').attribute("value").value,
"files[image]" => "",
"access_level" => doc.xpath('//input[@name="access_level" and @checked="checked"]').attribute("value").value,
"callback_url" => doc.xpath('//*[@id="edit-callback-url"]').attribute("value").value,
"organization" => doc.xpath('//*[@id="edit-organization"]').attribute("value").value,
"organization_url" => doc.xpath('//*[@id="edit-organization-url"]').attribute("value").value,
"op" => "Update this Twitter application's settings",
"form_build_id" => doc.xpath('//form[@id="twitter-apps-form-settings"]/input[@name="form_build_id"]').attribute("value").value,
"form_token" => doc.xpath('//form[@id="twitter-apps-form-settings"]/input[@name="form_token"]').attribute("value").value,
"form_id" => "twitter_apps_form_settings"
}
end
def update(id, newname)
data = prepare_update(id)
data["name"] = newname
request("https://dev.twitter.com/apps/#{id}/settings", :post_multipart, data).get_fields("Location")
end
login("screen_name", "password")
update("app_id", "new_name")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.