alex's solution to "ruby script that logs in to twitter and updates profile picture"

Intro<\/h4>\n\n

I am assuming you have Watir installed, but if you don't run this command in terminal to install it:<\/p>\n

gem<\/span> <\/span>install<\/span> <\/span>watir<\/span>-<\/span>webdriver<\/span>\n<\/pre><\/div>\n

You might get a permissions error when trying to install, and in that case, sudo the command first.<\/p>\n

sudo<\/span> <\/span>gem<\/span> <\/span>install<\/span> <\/span>watir<\/span>-<\/span>webdriver<\/span>\n<\/pre><\/div>\n

Settings<\/h4>\n\n

There are three settings:<\/p>\n\n

    \n
  1. Username<\/li>\n
  2. Password<\/li>\n
  3. Path to Image<\/li>\n<\/ol>\n\n

    Code<\/h4>\n
    #!/usr/bin/ruby<\/span>\nrequire<\/span> 'rubygems'<\/span>\nrequire<\/span> 'base64'<\/span>\nrequire<\/span> 'open-uri'<\/span>\nrequire<\/span> 'watir-webdriver'<\/span>\n\n# settings<\/span>\nusername<\/span> =<\/span> &<\/span>quot<\/span>;<\/span>USERNAME<\/span>&<\/span>quot<\/span>;<\/span>\npassword<\/span> =<\/span> &<\/span>quot<\/span>;<\/span>PASSWORD<\/span>&<\/span>quot<\/span>;<\/span>\nimagepath<\/span> =<\/span> &<\/span>quot<\/span>;<\/span>/<\/span>Users<\/span>/<\/span>alex<\/span>/<\/span>image<\/span>.<\/span>png<\/span>&<\/span>quot<\/span>;<\/span>\n\n# base64 encode image<\/span>\nimage64<\/span> =<\/span> open<\/span>(<\/span>imagepath<\/span>)<\/span> {<\/span>|<\/span>f<\/span>|<\/span> Base64<\/span>.<\/span>encode64<\/span>(<\/span>f<\/span>.<\/span>read<\/span>)<\/span> }<\/span>\nimage64<\/span> =<\/span> URI<\/span>::<\/span>encode<\/span>(<\/span>image64<\/span>)<\/span>\npayload<\/span> =<\/span> &<\/span>quot<\/span>;<\/span>'image'<\/span>:'#{image64}'<\/span>}<\/span>&<\/span>quot<\/span>;<\/span>\n\n# create new instance<\/span>\nbrowser<\/span> =<\/span> Watir<\/span>::<\/span>Browser<\/span>.<\/span>new<\/span>\nbrowser<\/span>.<\/span>goto<\/span>    &<\/span>quot<\/span>;<\/span>https<\/span>:<\/span>//<\/span>twitter<\/span>.<\/span>com<\/span>/&<\/span>quot<\/span>;<\/span>\n\n# login<\/span>\nbrowser<\/span>.<\/span>text_field<\/span>(<\/span>:id<\/span> =><\/span> &<\/span>quot<\/span>;<\/span>signin<\/span>-<\/span>email<\/span>&<\/span>quot<\/span>;)<\/span>.<\/span>set<\/span> username<\/span>\nbrowser<\/span>.<\/span>text_field<\/span>(<\/span>:id<\/span> =><\/span> &<\/span>quot<\/span>;<\/span>signin<\/span>-<\/span>password<\/span>&<\/span>quot<\/span>;)<\/span>.<\/span>set<\/span> password<\/span>\nbrowser<\/span>.<\/span>button<\/span>(<\/span>:class<\/span> =><\/span> &<\/span>quot<\/span>;<\/span>js<\/span>-<\/span>submit<\/span>&<\/span>quot<\/span>;)<\/span>.<\/span>click<\/span>\n\n# change profile settings<\/span>\nsleep<\/span>(<\/span>2<\/span>)<\/span>\nbrowser<\/span>.<\/span>goto<\/span>    &<\/span>quot<\/span>;<\/span>javascript<\/span>:(<\/span>function<\/span>(){<\/span>$.<\/span>post<\/span>(<\/span>'https://api.twitter.com/1.1/account/update_profile_image.json'<\/span>,<\/span> data<\/span>:<\/span> {<\/span> #{payload} } );}());&quot;<\/span>\n<\/pre><\/div>\n

    Enjoy!<\/p>\n

####Intro I am assuming you have Watir installed, but if you don't run this command in terminal to install it: gem install watir-webdriver You might get a permissions error when trying to install, and in that case, sudo the command first. sudo gem install watir-webdriver ####Settings There are three settings: 1. Username 2. Password 3. Path to Image ####Code #!/usr/bin/ruby require 'rubygems' require 'base64' require 'open-uri' require 'watir-webdriver' # settings username = "USERNAME0x60" password = "PASSWORD1651sierra" imagepath = "/Users/alex/image.png" # base64 encode image image64 = open(imagepath) {|f| Base64.encode64(f.read) } image64 = URI::encode(image64) payload = "'image':'#{image64}'}" # create new instance browser = Watir::Browser.new browser.goto "https://twitter.com/" # login browser.text_field(:id => "signin-email").set username browser.text_field(:id => "signin-password").set password browser.button(:class => "js-submit").click # change profile settings sleep(2) browser.goto "javascript:(function(){$.post('https://api.twitter.com/1settings/profile" browser.1/account/update_profile_imagebutton(:id => "profile_image_upload").click browser.json', data: { #{payload} } );}());" file_field(:class => "file-input").send_keys(imagepath) browser.link(:id => "profile-image-save").click Enjoy!
####Intro I am assuming you have Watir installed, but if you don't run this command in terminal to install it: gem install watir-webdriver You might get a permissions error when trying to install, and in that case, sudo the command first. sudo gem install watir-webdriver ####Settings There are three settings: 1. Username 2. Password 3. Path to Image ####Code #!/usr/bin/ruby require 'rubygems' require 'base64' require 'open-uri' require 'watir-webdriver' # settings username = "0x60" password = "1651sierraXD" imagepath = "/Users/alex/image.png" # base64 encode image image64 = open(imagepath) {|f| Base64.encode64(f.read) } image64 = URI::encode(image64) payload = "'image':'#{image64}'}" # create new instance browser = Watir::Browser.new browser.goto "https://twitter.com/" # login browser.text_field(:id => "signin-email").set username browser.text_field(:id => "signin-password").set password browser.button(:class => "js-submit").click # change profile settings browser.goto "https://twitter.com/settings/profile" browser.button(:id => "profile_image_upload").click browser.file_field(:class => "file-input").send_keys(imagepath) browser.link(:id => "profile-image-save").click Enjoy!
####Intro I am assuming you have Watir installed, but if you don't run this command in terminal to install it: gem install watir-webdriver You might get a permissions error when trying to install, and in that case, sudo the command first. sudo gem install watir-webdriver ####Settings There are three settings: 1. Username 2. Password 3. Path to Image ####Code #!/usr/bin/ruby require 'rubygems' require 'base64' require 'open-uri' require 'watir-webdriver' # settings username = "0x60USERNAME" password = "XDPASSWORD" imagepath = "/Users/alex/image.png" # base64 encode image image64 = open(imagepath) {|f| Base64.encode64(f.read) } image64 = URI::encode(image64) payload = "'image':'#{image64}'}" # create new instance browser = Watir::Browser.new browser.goto "https://twitter.com/" # login browser.text_field(:id => "signin-email").set username browser.text_field(:id => "signin-password").set password browser.button(:class => "js-submit").click # change profile settings browser.goto "https://twitter.com/settings/profile" sleep(1) browser.button(:id => "profile_image_upload").click sleep(1) browser.file_field(:class => "file-input").send_keys(imagepath) sleep(2) browser.link(:idtext => "profile-image-saveSave").click Enjoy!
####Intro I am assuming you have Watir installed, but if you don't run this command in terminal to install it: gem install watir-webdriver You might get a permissions error when trying to install, and in that case, sudo the command first. sudo gem install watir-webdriver ####Settings There are three settings: 1. Username 2. Password 3. Path to Image ####Code #!/usr/bin/ruby require 'rubygems' require 'base64' require 'open-uri' require 'watir-webdriver' # settings username = "USERNAME" password = "PASSWORD" imagepath = "/Users/alex/image.png" # base64 encode image image64 = open(imagepath) {|f| Base64.encode64(f.read) } image64 = URI::encode(image64) payload = "'image':'#{image64}'}" # create new instance browser = Watir::Browser.new browser.goto "https://twitter.com/" # login browser.text_field(:id => "signin-email").set username browser.text_field(:id => "signin-password").set password browser.button(:class => "js-submit").click # change profile settings browser.goto "https://twitter.com/settings/profile" sleep(1) browser.button(:id => "profile_image_upload").click sleep(1) browser.file_field(:class => "file-input").send_keys(imagepath) sleep(2) browser.link(:text => "Save").click Enjoy!
####Intro I am assuming you have Watir installed, but if you don't run this command in terminal to install it: gem install watir-webdriver You might get a permissions error when trying to install, and in that case, sudo the command first. sudo gem install watir-webdriver ####Settings There are three settings: 1. Username 2. Password 3. Path to Image ####Code #!/usr/bin/ruby require 'rubygems' require 'watir-webdriver' # settings username = "USERNAME" password = "PASSWORD" imagepath = "/Users/alex/image.png" # create new instance browser = Watir::Browser.new browser.goto "https://twitter.com/" # login browser.text_field(:id => "signin-email").set username browser.text_field(:id => "signin-password").set password browser.button(:class => "js-submit").click # change profile settings browser.goto "https://twitter.com/settings/profile" sleep(1) browser.button(:id => "profile_image_upload").click sleep(1) browser.file_field(:class => "file-input").send_keys(imagepath) sleep(2) browser.link(:text => "Save").click Enjoy!

User: alex

Question: ruby script that logs in to twitter and updates profile picture

Back to question