Skip to content

Instantly share code, notes, and snippets.

@shah743
Last active May 11, 2016 14:42
Show Gist options
  • Save shah743/dd042df63a8f307f16ed to your computer and use it in GitHub Desktop.
Save shah743/dd042df63a8f307f16ed to your computer and use it in GitHub Desktop.

Revisions

  1. Shah Zaib renamed this gist Sep 17, 2014. 1 changed file with 0 additions and 0 deletions.
  2. Shah Zaib created this gist Sep 17, 2014.
    46 changes: 46 additions & 0 deletions twitter_widget_id_extractor
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    class TwitterWidgetIdExtractor
    WIDGET_SETTING_URL = "https://twitter.com/settings/widgets/new/user?screen_name="
    SIGNIN_URL = "https://twitter.com/login"

    attr_accessor :screen_name, :twitter_username, :twitter_password

    def initialize(screen_name, twitter_username, twitter_password)
    @screen_name = screen_name
    @twitter_username = twitter_username
    @twitter_password = twitter_password
    end

    def get_widget_id
    signin
    open_widget_page
    extract_id
    end

    def signin
    page = agent.get SIGNIN_URL

    page.form_with(action: "https://twitter.com/sessions") do |form|
    form.field_with(name: 'session[username_or_email]').value = twitter_username
    form.field_with(name: 'session[password]').value = twitter_password
    end.submit

    end

    def open_widget_page
    page = agent.get(WIDGET_SETTING_URL+screen_name)

    @widget_response = page.form_with(id: 'widget-form').submit
    end

    def extract_id
    @widget_response.search("#code")[0].children[1].attributes['data-widget-id'].value
    end

    def agent
    unless @agent
    @agent = Mechanize.new
    end

    @agent
    end
    end