class Reddit

Constants

CONTENT_OPTIONS
MESSAGE_SUBPAGES
PAGE_CREATE_SUB
PAGE_MAIN
PAGE_MAIN_NO_SLASH
PAGE_MESSAGES
PAGE_SUBREDDIT
PAGE_USER
SUBREDDIT_SUBPAGES
SUB_TYPES
USER_SORTBYS

Attributes

browser[RW]
username[RW]

Public Instance Methods

add_friend() click to toggle source

Adds the currently open user as a friend, does nothing if the user is already a friend

# File lib/reddit_auto.rb, line 966
def add_friend
        return if is_friend
        @browser.link(text: '+ friends').click
end
comment_has_karma(div) click to toggle source

Checks if the given comment has karma

@param div [Watir::Div] a div containing the comment @return [Boolean] whether the comment has karma or not

# File lib/reddit_auto.rb, line 562
def comment_has_karma(div)
        return div.span(class: 'score').present?
end
comment_has_replies(div) click to toggle source

Checks if a given comment has replies

@param div [Watir::Div] a div containing the comment @return [Boolean] if the comment has replies or not

# File lib/reddit_auto.rb, line 486
def comment_has_replies(div)
        return div.attribute_value('data-replies') != '0'
end
create_account(username, password, captcha_token) click to toggle source

Creates an account on reddit

@param username [String] the account's username @param password [String] the account's password @param captcha_token [String] the google's captcha's token, it's up to you how to get it @return [Hash] a hash containing the account's username and password

# File lib/reddit_auto.rb, line 1216
def create_account(username, password, captcha_token) #if username is nil, selects username from reddit's suggestions
        @browser.goto PAGE_MAIN
        @browser.div(id: 'header-bottom-right').link(text: 'sign up').click
        sleep 3
        @browser.button(text: 'Next').click
        sleep 5
        result = nil
        2.times do
                case get_phase
                when 'picksubs'
                        phase_pick_subs
                when 'enterdata'
                        break if result != nil
                        result = phase_enter_data(username, password, captcha_token)
                end
                move_phase
        end
        wait_login(result['username'])
        return result
end
create_subreddit(subreddit) click to toggle source

Creates a subreddit with the given parameters

@param subreddit [NewSub] A Struct containing the subreddit's parameters @return [Boolean] whether the subreddit was successfully created

# File lib/reddit_auto.rb, line 909
def create_subreddit(subreddit)
        @browser.goto CREATE_SUB_PAGE
        @browser.text_field(id: 'name').set subreddit.name
        @browser.text_field(id: 'title').set subreddit.title
        @browser.textarea(name: 'public_description').set subreddit.description
        @browser.textarea(name: 'description').set subreddit.sidebar
        @browser.textarea(name: 'submit_text').set subreddit.subtext
        @browser.radio(id: SUB_TYPES[subreddit.type]).set
        @browser.radio(id: CONTENT_OPTIONS[subreddit.content]).set
        @browser.button(text: 'create').click
        return wait_sub_creation
end
did_create_sub() click to toggle source

Checks if the subreddit was successfully created

@return [Boolean] whether the subreddit was successfully created

# File lib/reddit_auto.rb, line 888
def did_create_sub
        return @browser.p(text: 'your subreddit has been created').present?
end
expand_all_comments() click to toggle source

Expands all comments in the open page

# File lib/reddit_auto.rb, line 626
def expand_all_comments
        while true
                begin
                        span = @browser.span(class: 'morecomments')
                        span.present? ? span.click : return
                rescue
                end
                sleep 0.5
        end
end
form_post_url(link) click to toggle source

Forms the full post url given the post's link

@param link [String] the post's link @return [String] the full post url

# File lib/reddit_auto.rb, line 411
def form_post_url(link)
        return PAGE_MAIN_NO_SLASH + link
end
form_subreddit_mod_url(subreddit) click to toggle source

Forms the full url for the subreddit's moderator's page

@param subreddit [String, Hash] a subreddit's name or hash @return [String] the full url for the subreddit's moderator's page

# File lib/reddit_auto.rb, line 826
def form_subreddit_mod_url(subreddit)
  case subreddit
  when Hash
    return PAGE_SUBREDDIT + subreddit['name'] + '/about/moderators'
  when String
    return PAGE_SUBREDDIT + subreddit + '/about/moderators'
  end
end
form_subreddit_url(name, subpage = 'hot') click to toggle source

Forms the full subreddit url given the subreddit's name

@param name [String] the subreddit's name @param subpage [String] the subreddit's subpage, defaults to 'hot' @return [String] the full subreddit url

# File lib/reddit_auto.rb, line 776
def form_subreddit_url(name, subpage = 'hot')
  return PAGE_SUBREDDIT + name + '/' + subpage
end
get_activity(div) click to toggle source

Gets a hash containing information about the given activity

@param div [Watir::Div] a div containing the activity @return a hash containing information about the given activity

# File lib/reddit_auto.rb, line 1109
def get_activity(div)
        result = {}
        result['type'] = get_activity_type(div)
        result['link'] = get_activity_link(div)
        result['subreddit'] = get_activity_subreddit(div)
        result['title'] = get_activity_title(div)
        result['content'] = result['type'] == 'link' ? result['title'] : get_activity_content(div)
        result['vote'] = get_activity_vote(div)
        result['karma'] = get_activity_karma(div, result['vote'])
        return result
end
get_activity_content(div) click to toggle source

Gets the activity's content if it is a comment

@param div [Watir::Div] the activity's div @return [String] the activity's text content

# File lib/reddit_auto.rb, line 1053
def get_activity_content(div) #only for comments
        return div.div(class: 'usertext-body').text
end
get_activity_divs() click to toggle source

Gets all the ativity divs from the currently open user

@return [Array] an array including the divs

# File lib/reddit_auto.rb, line 1008
def get_activity_divs
        divs = @browser.div(id: 'siteTable').children
        result = []
        divs.each do |div|
                result.push div if div.id.include? 'thing'
        end
        return result
end
get_activity_karma(div, vote) click to toggle source

Gets the amount of karma the activity received, 'up', 'down' or overall

@param div [Watir::Div] a div containing the activity @param vote [String, nil] 'up' for number of upvotes, 'down' for downvotes, nil for total @return [Integer] the number of votes/karma

# File lib/reddit_auto.rb, line 1062
def get_activity_karma(div, vote)
        case get_activity_type(div)
        when 'comment' 
                return get_comment_karma(div, vote)
        when 'link'
                case vote
                when 'up' 
                        return div.div(class: 'score likes').title.to_i
                when 'down' 
                        return div.div(class: 'score dislikes').title.to_i
                else
                        return div.div(class: 'score unvoted').title.to_i
                end
        else 
                raise 'Unknown activity type!'
        end
end
get_activity_subreddit(div) click to toggle source

Gets the subreddit of the activity

@param div [Watir::Div] the activity's div @return [String] the activity's subreddit

# File lib/reddit_auto.rb, line 1037
def get_activity_subreddit(div)
        return div.attribute_value('data-subreddit')
end
get_activity_title(div) click to toggle source

Gets the title of the activity

@param div [Watir::Div] the activity's div @return [String] the activity's title

# File lib/reddit_auto.rb, line 1045
def get_activity_title(div)
        return div.link(class: 'title').text
end
get_activity_type(div) click to toggle source

Gets the type of the activity

@param div [Watir::Div] the activity's div @return [String] the activity's type

# File lib/reddit_auto.rb, line 1021
def get_activity_type(div)
        return div.attribute_value('data-type')
end
get_activity_vote(div) click to toggle source

Gets the activity's vote by the logged in account

@param div [Watir::Div] a div containing the activity @return [String, nil] 'up' if upvoted, 'down' if downvoted, nil if not voted

# File lib/reddit_auto.rb, line 1093
def get_activity_vote(div)
        return get_comment_vote(div)
end
get_banned_subreddits() click to toggle source

Gets all the subreddits the currently logged user is banned in

@return [Array] an array of strings containing the subreddits the user is banned in

# File lib/reddit_auto.rb, line 1158
def get_banned_subreddits
        msgs = get_messages('messages', true)
        result = []
        msgs.each do |msg|
                result.push msg['author'] if msg['content'].include?('You have been permanently banned')
        end
        return result
end
get_comment(div) click to toggle source

Gets a hash containing information about a given comment

@param div [Watir::Div] a div containing the comment @return [Hash] a hash containing information about a given comment

# File lib/reddit_auto.rb, line 570
def get_comment(div)
        result = {}
        result['author'] = get_comment_author(div)
        result['link'] = get_comment_link(div)
        result['content'] = get_comment_content(div)
        result['vote'] = get_comment_vote(div)
        result['karma'] = get_comment_karma(div, result['vote']) if comment_has_karma(div)
        return result
end
get_comment_author(div) click to toggle source

Gets the author of the comment

@param div [Watir::Div] a div containing the comment @return [String] the author's username

# File lib/reddit_auto.rb, line 494
def get_comment_author(div)
        return div.attribute_value('data-author')
end
get_comment_content(div) click to toggle source

Gets content of the comment

@param div [Watir::Div] a div containing the comment @return [String] the content of the comment

# File lib/reddit_auto.rb, line 510
def get_comment_content(div)
        return div.div(class: 'usertext-body').text
end
get_comment_karma(div, vote) click to toggle source

Gets the amount of karma the comment received, 'up', 'down' or overall

@param div [Watir::Div] a div containing the comment @param vote [String, nil] 'up' for number of upvotes, 'down' for downvotes, nil for total @return [Integer] the number of votes/karma

# File lib/reddit_auto.rb, line 519
def get_comment_karma(div, vote)
        case vote
        when 'up'
                ind = 2
        when 'down'
                ind = 0
        else
                ind = 1
        end
        return div.p(class: 'tagline').spans(class: 'score')[ind].text.split(' ')[0].to_i
end
get_comment_replies_count(div) click to toggle source

Gets the number of replies the given comment received

@param div [Watir::Div] a div containing the comment @return [Integer] the number of replies

# File lib/reddit_auto.rb, line 478
def get_comment_replies_count(div)
        return div.attribute_value('data-replies').to_i
end
get_comment_vote(div) click to toggle source

Gets the comment's vote by the logged in account

@param div [Watir::Div] a div containing the comment @return [String, nil] 'up' if upvoted, 'down' if downvoted, nil if not voted

# File lib/reddit_auto.rb, line 552
def get_comment_vote(div)
        return 'up' if is_comment_voted(div, 'up')
        return 'down' if is_comment_voted(div, 'down')
        return nil
end
get_comments(post, expand = false) click to toggle source

Gets all the comments in the given post

@param post [String, Hash] a post hash or full url @param expand [Boolean] whether to expand all the comments first @return [Array] an array of hashes including information about all the comments and their replies

# File lib/reddit_auto.rb, line 642
def get_comments(post, expand = false)
        open_post(post)
        expand_all_comments if expand
        return parse_comments_divs(get_comments_divs)
end
get_comments_divs() click to toggle source

Gets all the comments' divs in the open page

@return [Array] an array containing all comments' divs in the open page

# File lib/reddit_auto.rb, line 583
def get_comments_divs
        divs = @browser.div(class: 'commentarea').div(class: 'sitetable nestedlisting').children
        result = []
        divs.each do |div|
                result.push div if div.attribute_value('data-type') == 'comment'
        end
        return result
end
get_message(div) click to toggle source

Gets a hash object containing information about a given message

@param div [Watir::Div] the div containing the message @return [Hash] a hash containing informaton about the given message

# File lib/reddit_auto.rb, line 228
def get_message(div) #returns a hash with message data
        result = {}
        result['type'] = get_message_type(div)
        result['author'] = get_message_author(div)
        result['post'] = get_message_post(div) if result['type'] == 'comment'
        result['subreddit'] = result['type'] == 'comment' ? get_message_subreddit(div) : result['author']
        result['vote'] = get_message_vote(div) if result['type'] == 'comment'
        result['content'] = get_message_content(div)
        return result
end
get_message_author(div) click to toggle source

Gets the reddit user who posted the message

@param div [Watir::Div] the div containing the message @return [String] the username of the message's author

# File lib/reddit_auto.rb, line 154
def get_message_author(div)
        return div.attribute_value('data-author')
end
get_message_content(div) click to toggle source

Gets the content of the message

@param div [Watir::Div] the div containing the message @return [String] the message's content

# File lib/reddit_auto.rb, line 170
def get_message_content(div)
        return div.div(class: 'md').text
end
get_message_divs() click to toggle source

Gets all message divs in the page

@return [Array<Watir::Div>] an array containing all message divs in the page

# File lib/reddit_auto.rb, line 177
def get_message_divs
        all_divs = @browser.div(id: 'siteTable').divs
        result = []
        all_divs.each do |div|
                result.push div if div.id.include? 'thing_'
        end
        return result
end
get_message_post(div) click to toggle source

Gets the post that a message belongs to

@param div [Watir::Div] the div containing the message @return [String] a link to the post

# File lib/reddit_auto.rb, line 146
def get_message_post(div)
        return div.p(class: 'subject').link(class: 'title').href
end
get_message_subreddit(div) click to toggle source

Gets the subreddit which the message was posted in

@param div [Watir::Div] the div containing the message @return [String] the subreddit

# File lib/reddit_auto.rb, line 162
def get_message_subreddit(div)
        return div.attribute_value('data-subreddit')
end
get_message_type(div) click to toggle source

Gets the type of a message

@param div [Watir::Div] the div containing the message @return [String] the message type

# File lib/reddit_auto.rb, line 138
def get_message_type(div)
        return div.attribute_value('data-type')
end
get_message_vote(div) click to toggle source

Gets the type of vote the given message received from the logged in user

@param div [Watir::Div] the div containing the message @return [String, nil] returns 'up' or 'down' if voted or nil if not voted

# File lib/reddit_auto.rb, line 199
def get_message_vote(div)
        return 'up' if is_message_voted(div, 'up')
        return 'down' if is_message_voted(div, 'down')
        return nil
end
get_messages(subpage, all_pages = false) click to toggle source

Gets all the messages in a given subpage, raises an exception if an unknown subpage is given

@param subpage [String] the subpage to open, can be: 'inbox', 'unread', 'messages', 'comments' or 'sent' @param all_pages [Boolean] if true will check all pages, if false will check only the first page @return [Array] an array containing hashes with information about all pages

# File lib/reddit_auto.rb, line 263
def get_messages(subpage, all_pages = false)
        open_messages_subpage(subpage)
        result = []
        while true
                get_message_divs.each do |div|
                        result.push get_message(div)
                end
                return result if !all_pages || !message_move_page('next')
        end
end
get_moderating() click to toggle source

Gets the currently open user's moderating pages

@return [Array] an array of strings containing the names of the subreddits

# File lib/reddit_auto.rb, line 950
def get_moderating
        result = []
        @browser.ul(id: 'side-mod-list').lis.each do |li|
                result.push li.link.title.split('/')[1]
        end
        return result
end
get_moderators(subreddit) click to toggle source

Gets an array including the usernames of the moderators of the given subreddit

@param subreddit [String, Hash] a subreddit's name or hash @return [Array] an array including the usernames of the moderators of the given subreddit

# File lib/reddit_auto.rb, line 839
def get_moderators(subreddit)
        @browser.goto form_subreddit_mod_url(subreddit)
        spans = @browser.div(class: 'moderator-table').spans(class: 'user')
        result = []
        spans.each do |span|
                result.push span.link.text
        end
        return result
end
get_original_post_vote() click to toggle source

Gets the type of the vote the logged in account voted the original post that is currently open

@return [String, nil] returns the vote type, 'up' or 'down' or nil if not voted

# File lib/reddit_auto.rb, line 392
def get_original_post_vote
        return 'up' if is_original_post_voted('up')
        return 'down' if is_original_post_voted('down')
        return nil
end
get_phase() click to toggle source
# File lib/reddit_auto.rb, line 1206
def get_phase
        return @browser.button(class: 'c-btn c-btn-primary subreddit-picker__subreddit-button').present? ? 'picksubs' : 'enterdata'
end
get_post(div) click to toggle source

Gets a hash containing information about a given post

@param div [Watir::Div] a div containing the post @return [Hash] a hash containing information about a given post

# File lib/reddit_auto.rb, line 752
def get_post(div)
        result = {}
        result['author'] = get_post_author(div)
        result['link'] = get_post_link(div)
        result['karma'] = get_post_karma(div)
        result['title'] = get_post_title(div)
        result['vote'] = get_post_vote(div)
        result['number_of_comments'] = get_post_number_of_comments(div)
        return result
end
get_post_author(div) click to toggle source

Gets the author of the post

@param div [Watir::Div] a div containing the post @return [String] the author of the post

# File lib/reddit_auto.rb, line 687
def get_post_author(div)
        return div.attribute_value('data-author')
end
get_post_karma(div) click to toggle source

Gets the amount of karma of the post

@param div [Watir::Div] a div containing the post @return [Integer] the amount of karma the post received

# File lib/reddit_auto.rb, line 703
def get_post_karma(div)
        return div.attribute_value('data-score').to_i
end
get_post_link(div) click to toggle source

Gets the link of the post

@param div [Watir::Div] a div containing the post @return [String] the link of the post

# File lib/reddit_auto.rb, line 695
def get_post_link(div)
        return div.attribute_value('data-permalink')
end
get_post_number_of_comments(div) click to toggle source

Gets the number of comments in the post

@param div [Watir::Div] a div containing the post @return [Integer] the number of comments in the post

# File lib/reddit_auto.rb, line 719
def get_post_number_of_comments(div)
        return div.attribute_value('data-comments-count').to_i
end
get_post_title(div) click to toggle source

Gets the title of the post

@param div [Watir::Div] a div containing the post @return [String] the title of the post

# File lib/reddit_auto.rb, line 711
def get_post_title(div)
        return div.link(class: 'title').text
end
get_post_vote(div) click to toggle source

Gets the type of vote the post has

@param div [Watir::Div] a div containing the post @return [String, nil] the of vote, 'up', 'down' or nil if not voted

# File lib/reddit_auto.rb, line 736
def get_post_vote(div)
        return get_comment_vote(div)
end
get_posts(subreddit, subpage = 'hot', max_pages = 1) click to toggle source

Gets all the posts in the given subreddit

@param subreddit [String, Hash] a subreddit's name or hash @param subpage [String] the subreddit's subpage, defaults to 'hot' @param max_pages [Integer] maximum amount of pages to gather posts from @return [Array] an array containing hashes with information about the subreddit's posts

# File lib/reddit_auto.rb, line 807
def get_posts(subreddit, subpage = 'hot', max_pages = 1)
        open_subreddit(subreddit, subpage)
        result = []
        count = 0
        while true
                get_posts_divs.each do |div|
                        result.push get_post(div)
                end
                count += 1
                break if count >= max_pages
                break if !subreddit_move_page('next')
        end
        return result
end
get_posts_divs() click to toggle source

Gets all the posts' divs

@return [Array] an array containing all posts' divs

# File lib/reddit_auto.rb, line 674
def get_posts_divs
        divs = @browser.div(id: 'siteTable').children
        result = []
        divs.each do |div|
                result.push div if div.attribute_value('data-type') == 'link'
        end
        return result
end
get_replies_divs(main_div) click to toggle source

Gets all replies' divs to the given comment

@param div [Watir::Div] a div containing the comment @return [Array] an array containing all replies' divs to the given comment

# File lib/reddit_auto.rb, line 596
def get_replies_divs(main_div)
        divs = main_div.div(class: 'child').div.children
        begin
                x = divs.length
        rescue
                return []
        end
        result = []
        divs.each do |div|
                result.push div if div.attribute_value('data-type') == 'comment'
        end
        return result
end
get_reply_error() click to toggle source

Gets the reason for the reply error

@return [String] the reason

# File lib/reddit_auto.rb, line 449
def get_reply_error
        return @browser.span(class: 'error', style: '').split(" ")[1]
end
get_side_bar() click to toggle source

Gets content of the subreddit currently open sidebar

@return [String] the content of the subreddit's sidebar

# File lib/reddit_auto.rb, line 866
def get_side_bar
        return @browser.div(class: 'usertext-body').text
end
get_subreddit(subreddit) click to toggle source

Gets a hash with information about the given subreddit

@param subreddit [String] the subreddit's name @return [Hash] a hash with information about the given subreddit

# File lib/reddit_auto.rb, line 874
def get_subreddit(subreddit)
        result = {}
        result['name'] = subreddit
        open_subreddit(subreddit)
        result['subscribers'] = get_subscribers
        result['users_online'] = get_users_online
        result['sidebar'] = get_side_bar
        result['moderators'] = get_moderators(subreddit)
        return result
end
get_subscribers() click to toggle source

Gets the number of subscribers the subreddit currently open has

@return [Integer] the number of subscribers the subreddit currently open has

# File lib/reddit_auto.rb, line 852
def get_subscribers
        return @browser.span(class: 'subscribers').span(class: 'number').text.gsub(',', '').to_i
end
get_user(user) click to toggle source

Gets a hash containing information about the given user

@param user [String] the username @return [Hash] a hash containing information about the given user

# File lib/reddit_auto.rb, line 989
def get_user(user)
        open_user_page(user)
        return nil if @browser.div(id: 'classy-error').present?
        result = {}
        result['name'] = user
        result['post_karma'] = get_user_post_karma
        result['comment_karma'] = get_user_comment_karma
        result['is_friend'] = @username ? is_friend : false
        result['moderating'] = get_moderating if is_moderator
        return result
end
get_user_activities(user, sortby = 'new', max_pages = 1) click to toggle source

Gets all the user activities from the given user, raises exception if unknown sorting method is used

@param user [String] the username @param sortby [String] sorting method, can be: 'new', 'hot', 'top', 'controversial' @param max_pages [Integer] maximum amounts of pages to get activies from @return [Array] an array containing all the activities hashes

# File lib/reddit_auto.rb, line 1135
def get_user_activities(user, sortby = 'new', max_pages = 1)
        raise 'Unknown user sortby: ' + sortby if !USER_SORTBYS.include? sortby
        @browser.goto PAGE_USER + user + '/?sort=' + sortby
        result = []
        count = 0
        while true
                get_activity_divs.each do |div|
                        result.push get_activity(div)
                end
                count += 1
                break if count >= max_pages
                break if !user_move_page('next')
        end
        return result
end
get_user_comment_karma() click to toggle source

Gets the currently open user's comment karma

@return [Integer] the currently open user's comment karma

# File lib/reddit_auto.rb, line 936
def get_user_comment_karma
        return @browser.spans(class: 'karma')[1].text.gsub(',', '').to_i
end
get_user_post_karma() click to toggle source

Gets the currently open user's post karma

@return [Integer] the currently open user's post karma

# File lib/reddit_auto.rb, line 929
def get_user_post_karma
        return @browser.span(class: 'karma').text.gsub(',', '').to_i
end
get_users_online() click to toggle source

Gets the number of online users the subreddit currently open has

@return [Integer] the number of online users the subreddit currently open has

# File lib/reddit_auto.rb, line 859
def get_users_online
        return @browser.p(class: 'users-online').span(class: 'number').text.gsub(',', '').to_i
end
has_browser() click to toggle source

Checks if the class has a browser assigned

@return [Boolean]

# File lib/reddit_auto.rb, line 48
def has_browser
        return @browser != nil
end
has_over_18() click to toggle source

Checks if reddit is asking if user is over 18 for nsfw content

@return [Boolean] whether the prompt is present or not

# File lib/reddit_auto.rb, line 109
def has_over_18
        return @browser.button(name: 'over18').present?
end
has_over_18_new() click to toggle source

Checks if reddit is asking if user is over 18 for nsfw content, uses the new interface

@return [Boolean] whether the prompt is present or not

# File lib/reddit_auto.rb, line 121
def has_over_18_new
        return @browser.h3(text: 'You must be 18+ to view this community').present?
end
has_reply(answer) click to toggle source

Checks if the logged in account has already replied to the given post with the given answer

@param answer [String] the answer to look for @return [Boolean] whether or not the logged in account replied to the post with the given answer

# File lib/reddit_auto.rb, line 434
def has_reply(answer)
        form = @browser.form(text: answer)
        return form.present? && form.parent.parent.attribute_value('data-author') == @username
end
has_reply_error() click to toggle source

Checks if there was an error when replying

@return [Boolean] whether there was an error or not

# File lib/reddit_auto.rb, line 442
def has_reply_error
        return @browser.span(class: 'error', style: '').present?
end
has_submit_error() click to toggle source

Checks if there was an error when submiting to a subreddit, typically because of the 10 minute cooldown between posts enforced by reddit

@return [Boolean] whether there as an error or not

# File lib/reddit_auto.rb, line 281
def has_submit_error
        return @browser.span(text: 'you are doing that too much. try again in').present?
end
is_activity_voted(div, type) click to toggle source

Checks if the activity is voted by the logged in account, 'up' or 'down', raises an exception if an unknown vote type is submitted

@param div [Watir::Div] a div containing the activity @param type [String] the vote type, 'up' or 'down' @return [Boolean] if the activity is voted

# File lib/reddit_auto.rb, line 1085
def is_activity_voted(div, type)
        return is_comment_voted(div, type)
end
is_comment_voted(div, type) click to toggle source

Checks if the comment is voted by the logged in account, 'up' or 'down', raises an exception if an unknown vote type is submitted

@param div [Watir::Div] a div containing the comment @param type [String] the vote type, 'up' or 'down' @return [Boolean] if the comment is voted

# File lib/reddit_auto.rb, line 536
def is_comment_voted(div, type)
        case type
        when 'up'
                buffer = 'likes'
        when 'down'
                buffer = 'dislikes'
        else
                raise 'Unknown vote type!'
        end
        return div.div(class: 'entry').attribute_value('class') == 'entry ' + buffer
end
is_friend() click to toggle source

Checks if the currently open user is a friend of the logged in account

@return [Boolean] if the currently open user is a friend of the logged in account

# File lib/reddit_auto.rb, line 961
def is_friend
        return @browser.span(class: 'fancy-toggle-button').link(text: '- friends').attribute_value('class').include?('active')
end
is_logged_in(username) click to toggle source

Checks if a given account is logged in

@param username [String] the account's username @return [Boolean] whether it is logged in or not

# File lib/reddit_auto.rb, line 56
def is_logged_in(username)
        return @browser.link(text: username).present?
end
is_message_voted(div, type) click to toggle source

Checks if a given message is voted by the logged in user

@param div [Watir::Div] the div containing the message @param type [String] the type of vote, can be 'up' or 'down' @return [Boolean] whether the message is voted in the given type

# File lib/reddit_auto.rb, line 191
def is_message_voted(div, type)
        return div.div(class: 'midcol').div(class: type + 'mod').present?
end
is_moderator() click to toggle source

Checks if the currently open user is a moderator of any subreddit

@return [Boolean] if the currently open user is a moderator of any subreddit

# File lib/reddit_auto.rb, line 943
def is_moderator
        return @browser.ul(id: 'side-mod-list').present?
end
is_original_post_voted(type) click to toggle source

Checks if the currently open post is voted by the logged in account, raises an exception if an unknown vote type is submitted

@param type [String] the vote type, can be 'up' or 'down' @return [Boolean] whether or not the original post is voted in the given type

# File lib/reddit_auto.rb, line 377
def is_original_post_voted(type)
        div = @browser.div(id: 'siteTable').div(class: 'midcol')
        case type
        when 'up'
                return div.attribute_value('class') == 'midcol likes'
        when 'down'
                return div.attribute_value('class') == 'midcol dislikes'
        else
                raise 'Unknown vote type: ' + type
        end
end
is_post_voted(div, type) click to toggle source

Checks if the post is voted in the given type

@param div [Watir::Div] a div containing the post @param type [String] the type of vote: 'up' or 'down' @return [Boolean] whether the post is voted or not in the given type

# File lib/reddit_auto.rb, line 728
def is_post_voted(div, type)
        return is_comment_voted(div, type)
end
is_submit_open() click to toggle source

Checks if the submit page is open

@return [Boolean] whether the page is open or not

# File lib/reddit_auto.rb, line 288
def is_submit_open
        return @browser.textarea(name: 'title').present? || @browser.textarea(placeholder: 'Title').present?
end
is_subreddit_banned(subreddit) click to toggle source

Checks if the given subreddit is banned

@param subreddit [String] the subreddit's name @return [Boolean] whether the subreddit is banned or not

# File lib/reddit_auto.rb, line 1251
def is_subreddit_banned(subreddit)
        @browser.goto PAGE_SUBREDDIT + subreddit
        skip_over_18 if has_over_18
        return @browser.h3(text: 'This community has been banned').present?
end
is_user_banned(user) click to toggle source

Checks if the given user is banned

@param user [String] the username @return [Boolean] whether the user is banned or not

# File lib/reddit_auto.rb, line 1241
def is_user_banned(user)
        @browser.goto PAGE_USER + user
        skip_over_18 if has_over_18
        return @browser.div(id: 'classy-error').present? || @browser.h3(text: 'This account has been suspended').present?
end
login(username, password) click to toggle source

Logins the reddit website, raises an exception on failure

@param username [String] the account's username @param password [String] the account's password

# File lib/reddit_auto.rb, line 78
def login(username, password)
        @username = username
        @browser.goto PAGE_MAIN
        @browser.text_field(name: 'user').set username
        @browser.text_field(name: 'passwd').set password
        @browser.checkbox(id: 'rem-login-main').set
        @browser.button(text: 'login').click
        wait_login(username)
end
logout() click to toggle source

Waits for logout, raises an exception on failure

# File lib/reddit_auto.rb, line 101
def logout
        @browser.link(text: 'logout').click
        wait_logout
end
message_move_page(direction) click to toggle source

Moves to the next or previous page in the message inbox

@param direction [String] the direction to move, can be 'next' or 'prev' @return [Boolean] returns true if moved to the desired page or false if didn't because you're already in the last (move next) or first (move prev) page

# File lib/reddit_auto.rb, line 243
def message_move_page(direction)
        button = @browser.span(class: direction + '-button')
        result = button.present?
        button.click if result
        return result
end
move_phase() click to toggle source
# File lib/reddit_auto.rb, line 1189
def move_phase
        @browser.buttons(text: 'Submit').each do |button|
                if button.present?
                        button.click
                        sleep 5
                        return
                end
        end
        @browser.buttons(text: 'Next').each do |button|
                if button.present?
                        button.click
                        sleep 5
                        return
                end
        end
end
open_messages_subpage(subpage) click to toggle source

Opens the messages subpage, raises an exception if an unknown subpage is given

@param subpage [String] the subpage to open, can be: 'inbox', 'unread', 'messages', 'comments' or 'sent'

# File lib/reddit_auto.rb, line 253
def open_messages_subpage(subpage)
        raise 'Unknown message subpage: ' + subpage if !MESSAGE_SUBPAGES.include? subpage
        @browser.goto PAGE_MESSAGES + subpage
end
open_post(post) click to toggle source

Opens the given post

@param post [Hash, String] accepts either a post hash or the post's full url

# File lib/reddit_auto.rb, line 418
    def open_post(post)
case post
when Hash
  @browser.goto form_post_url(post['link'])
when String
  @browser.goto post
else
  return
end
skip_over_18 if has_over_18
    end
open_subreddit(subreddit, subpage = 'hot') click to toggle source

Opens the given subreddit in the given subpage, raises an exception if an unknown subpage is given, known subpages: 'hot', 'new', 'rising', 'top', 'gilded'

@param subreddit [String, Hash] a subreddit's name or hash @param subpage [String] the subreddit's subpage, defaults to 'hot'

# File lib/reddit_auto.rb, line 784
    def open_subreddit(subreddit, subpage = 'hot')
            raise 'Unknown subreddit subpage: ' + subpage if !SUBREDDIT_SUBPAGES.include? subpage
case subreddit
when Hash
  @browser.goto form_subreddit_url(subreddit['name'], subpage)
when String
  if subreddit.include? '/'
    @browser.goto subreddit
  else
    @browser.goto form_subreddit_url(subreddit, subpage)
  end
else
  return
end
            skip_over_18 if has_over_18
    end
open_user_page(user) click to toggle source

Opens the page of the given username

@param user [String] the username

# File lib/reddit_auto.rb, line 980
def open_user_page(user)
        @browser.goto PAGE_USER + user
        skip_over_18 if has_over_18
end
parse_comments_divs(divs) click to toggle source

Parses all the comments divs and replies

@param div [Watir::Div] a div containing the comment @return [Array] an array of hashes containing the comments and their replies

# File lib/reddit_auto.rb, line 614
def parse_comments_divs(divs)
        result = []
        divs.each do |div|
                result.push get_comment(div)
                if comment_has_replies(div)
                        result[result.length-1]['replies'] = parse_comments_divs(get_replies_divs(div))
                end
        end
        return result
end
phase_enter_data(username, password, captcha_token) click to toggle source
# File lib/reddit_auto.rb, line 1171
def phase_enter_data(username, password, captcha_token)
        result = {}
        if username == nil
                link = @browser.link(class: 'username-generator__item')
                result['username'] = link.text
                link.click
        else
                result['username'] = username
                @browser.text_field(id: 'user_reg').set username
        end
        @browser.text_field(id: 'passwd_reg').set password
        result['password'] = password
        sleep 5
        @browser.execute_script(%{document.getElementById("g-recaptcha-response").innerHTML="} + captcha_token + %{"})
        sleep 5
        return result
end
phase_pick_subs() click to toggle source
# File lib/reddit_auto.rb, line 1167
def phase_pick_subs
        @browser.execute_script("var buttons = document.getElementsByClassName('c-btn c-btn-primary subreddit-picker__subreddit-button');\nfor (var i = 0; i < 8; i++) {\nbuttons[i].click();\n}")
end
remove_friend() click to toggle source

Removes the currently open user as a friend, does nothing if the user is not a friend

# File lib/reddit_auto.rb, line 972
def remove_friend
        return if !is_friend
        @browser.link(text: '- friends').click
end
reply_comment(div, answer) click to toggle source

Replies the given comment with the given anwer

@param div [Watir::Div] a div containing the comment @param answer [String] the answer

# File lib/reddit_auto.rb, line 652
def reply_comment(div, answer)
        div.li(text: 'reply').click
        div.textarea(name: 'text').set answer
        div.button(class: 'save').click
end
reply_message(div, answer) click to toggle source

Replies the given message

@param div [Watir::Div] the div containing the message @param answer [String] answer, the content of the reply

# File lib/reddit_auto.rb, line 218
def reply_message(div, answer)
        div.li(text: 'reply').click
        div.textarea.set answer
        div.button(text: 'save').click
end
reply_post(post, answer) click to toggle source

Replies the given post

@param post [Hash, String] the post to reply, can be a post hash or full url @param answer [String] the answer to the post @return [Boolean] if replying as successful

# File lib/reddit_auto.rb, line 467
    def reply_post(post, answer)
open_post(post)
            @browser.div(class: 'commentarea').textarea(name: 'text').set answer
            @browser.div(class: 'commentarea').button(text: 'save').click
            return wait_reply
    end
set_flair(flair) click to toggle source

Sets the given flair to the post, does nothing if the subreddit has no flair option

@param flair [String] the desired flair

# File lib/reddit_auto.rb, line 326
def set_flair(flair)
        return if !sub_has_flair
        @browser.div('aria-label': 'Add flair').click
        if flair == nil
                @browser.div('aria-label': 'flair_picker').div.click
        else
                @browser.div('aria-label': 'flair_picker').span(text: flair).click
        end
        @browser.button(text: 'Apply').click
end
skip_over_18() click to toggle source

Skips the are you over 18 prompt

# File lib/reddit_auto.rb, line 114
def skip_over_18
        @browser.button(text: 'continue').click
end
skip_over_18_new() click to toggle source

Skips the are you over 18 prompt, uses the new interface

# File lib/reddit_auto.rb, line 126
def skip_over_18_new
        @browser.link(text: 'Yes').click
end
sub_has_flair() click to toggle source

Checks if the subreddit has an option to add a flair to posts

@return [Boolean] whether or not it has a flair option

# File lib/reddit_auto.rb, line 319
def sub_has_flair
        return !@browser.div('aria-label': 'Not available for this community').present?
end
submit_text(subreddit, title, text) click to toggle source

Submits a text post to the given subreddit, raises an exception on failure

@param subreddit [String] the name of the subreddit to submit the link to @param title [String] the title of the post @param text [String] the text content of the post

# File lib/reddit_auto.rb, line 360
def submit_text(subreddit, title, text)
        @browser.goto PAGE_SUBREDDIT + subreddit + '/submit?selftext=true'
        skip_over_18 if has_over_18
        @browser.textarea(name: 'title').set title
        @browser.textarea(name: 'text').set text
        @browser.button(name: 'submit').click
        wait_submit
end
subreddit_move_page(direction) click to toggle source

Moves to the next or previous page in the subreddit

@param direction [String] the direction to move, can be 'next' or 'prev' @return [Boolean] returns true if moved to the desired page or false if didn't because you're already in the last (move next) or first (move prev) page

# File lib/reddit_auto.rb, line 767
def subreddit_move_page(direction)
        return message_move_page(direction)
end
user_move_page(direction) click to toggle source

Moves to the next or previous page in the user's activity box

@param direction [String] the direction to move, can be 'next' or 'prev' @return [Boolean] returns true if moved to the desired page or false if didn't because you're already in the last (move next) or first (move prev) page

# File lib/reddit_auto.rb, line 1125
def user_move_page(direction)
        return message_move_page(direction)
end
vote_activity(div, type) click to toggle source

Votes the given activity

@param div [Watir::Div] a div containing the activity @param [String] the vote type can be 'up' or 'down'

# File lib/reddit_auto.rb, line 1101
def vote_activity(div, type)
        vote_message(div, type)
end
vote_comment(div, type) click to toggle source

Votes the given comment

@param div [Watir::Div] a div containing the comment @param type [String] the vote type can be 'up' or 'down'

# File lib/reddit_auto.rb, line 662
def vote_comment(div, type)
        return if is_comment_voted(div, type)
        div.div(class: 'midcol').div(class: type).click
end
vote_message(div, type) click to toggle source

Votes the given message

@param div [Watir::Div] the div containing the message @param type [String] the type of vote, can be 'up' or 'down'

# File lib/reddit_auto.rb, line 209
def vote_message(div, type)
        return if is_message_voted(div, type)
        div.div(class: 'midcol').div(class: type).click
end
vote_original_post(type) click to toggle source

Votes the currently open original post, does nothing if already voted, raises an exception if an unknown vote type is submitted

@param type [String] vote type: 'up' or 'down'

# File lib/reddit_auto.rb, line 401
def vote_original_post(type)
        return if is_original_post_voted(type)
        div = @browser.div(id: 'siteTable').div(class: 'midcol')
        div.div(class: type).click
end
vote_post(div, type) click to toggle source

Votes the given post, 'up' or 'down', raises exception if unknown vote type is sumited

@param div [Watir::Div] a div containing the post @param type [String] the type of vote: 'up' or 'down'

# File lib/reddit_auto.rb, line 744
def vote_post(div, type)
        vote_comment(div, type)
end
wait_login(username) click to toggle source

Waits for a successful loggin, raises an exception if it fails to login

@param username [String] the account's username

# File lib/reddit_auto.rb, line 63
def wait_login(username)
        count = 0.0
        while !is_logged_in(username)
                sleep 0.25
                count += 0.25
                if count > 10
                        raise 'Reddit login failed for username: ' + username
                end
        end
end
wait_logout() click to toggle source

Waits for logout, raises an exception on failure

# File lib/reddit_auto.rb, line 89
def wait_logout
        count = 0.0
        while is_logged_in(@username)
                sleep 0.25
                count += 0.25
                if count > 10
                        raise 'Reddit logout failed for username: ' + @username
                end
        end
end
wait_reply(time = 2) click to toggle source

Sleeps the given time then checks if there was an error when replying

@param time [Integer] the number of seconds to sleep @return [Boolean] whether there was an error or not

# File lib/reddit_auto.rb, line 457
def wait_reply(time = 2)
        sleep time
        return !has_reply_error
end
wait_sub_creation() click to toggle source

Waits for the creation of the subreddit

@return [Boolean] whether the subreddit was successfully created

# File lib/reddit_auto.rb, line 895
def wait_sub_creation
        count = 0.0
        while true
                return true if did_create_sub
                sleep 0.25
                count += 0.25
                return false if count >= 10
        end
end
wait_submit() click to toggle source

Waits for the submit page to open, raises an exception if it fails to open after 10 seconds

# File lib/reddit_auto.rb, line 293
def wait_submit
        count = 0.0
        while is_submit_open
                sleep 0.25
                count += 0.25
                raise 'Post submission failed!' if count >= 10
        end
end