Class: Reddit
- Inherits:
-
Object
- Object
- Defined in:
- lib/reddit_auto.rb
Constant Summary collapse
- PAGE_MAIN =
'https://old.reddit.com/'- PAGE_MAIN_NO_SLASH =
'https://old.reddit.com'- PAGE_MESSAGES =
'https://old.reddit.com/message/'- PAGE_SUBREDDIT =
'https://old.reddit.com/r/'- PAGE_USER =
'https://old.reddit.com/user/'- PAGE_CREATE_SUB =
'https://old.reddit.com/subreddits/create'- MESSAGE_SUBPAGES =
['inbox', 'unread', 'messages', 'comments', 'sent']
- SUBREDDIT_SUBPAGES =
['hot', 'new', 'rising', 'top', 'gilded']
- USER_SORTBYS =
['new', 'hot', 'top', 'controversial']
- SUB_TYPES =
{ 'public' => 'type_public', 'restricted' => 'type_restricted', 'private' => 'type_private', 'premium only' => 'type_gold_only' }
- CONTENT_OPTIONS =
{ 'any' => 'link_type_any', 'links only' => 'link_type_link', 'text posts only' => 'link_type_self' }
Instance Attribute Summary collapse
-
#browser ⇒ Object
Returns the value of attribute browser.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#add_friend ⇒ Object
Adds the currently open user as a friend, does nothing if the user is already a friend.
-
#comment_has_karma(div) ⇒ Boolean
Checks if the given comment has karma.
-
#comment_has_replies(div) ⇒ Boolean
Checks if a given comment has replies.
-
#create_account(username, password, captcha_token) ⇒ Hash
Creates an account on reddit.
-
#create_subreddit(subreddit) ⇒ Boolean
Creates a subreddit with the given parameters.
-
#did_create_sub ⇒ Boolean
Checks if the subreddit was successfully created.
-
#expand_all_comments ⇒ Object
Expands all comments in the open page.
-
#form_post_url(link) ⇒ String
Forms the full post url given the post’s link.
-
#form_subreddit_mod_url(subreddit) ⇒ String
Forms the full url for the subreddit’s moderator’s page.
-
#form_subreddit_url(name, subpage = 'hot') ⇒ String
Forms the full subreddit url given the subreddit’s name.
-
#get_activity(div) ⇒ Object
Gets a hash containing information about the given activity.
-
#get_activity_content(div) ⇒ String
Gets the activity’s content if it is a comment.
-
#get_activity_divs ⇒ Array
Gets all the ativity divs from the currently open user.
-
#get_activity_karma(div, vote) ⇒ Integer
Gets the amount of karma the activity received, ‘up’, ‘down’ or overall.
-
#get_activity_link(div) ⇒ String
Gets the link of the activity.
-
#get_activity_subreddit(div) ⇒ String
Gets the subreddit of the activity.
-
#get_activity_title(div) ⇒ String
Gets the title of the activity.
-
#get_activity_type(div) ⇒ String
Gets the type of the activity.
-
#get_activity_vote(div) ⇒ String?
Gets the activity’s vote by the logged in account.
-
#get_banned_subreddits ⇒ Array
Gets all the subreddits the currently logged user is banned in.
-
#get_comment(div) ⇒ Hash
Gets a hash containing information about a given comment.
-
#get_comment_author(div) ⇒ String
Gets the author of the comment.
-
#get_comment_content(div) ⇒ String
Gets content of the comment.
-
#get_comment_karma(div, vote) ⇒ Integer
Gets the amount of karma the comment received, ‘up’, ‘down’ or overall.
-
#get_comment_link(div) ⇒ String
Gets the link of the comment.
-
#get_comment_replies_count(div) ⇒ Integer
Gets the number of replies the given comment received.
-
#get_comment_vote(div) ⇒ String?
Gets the comment’s vote by the logged in account.
-
#get_comments(post, expand = false) ⇒ Array
Gets all the comments in the given post.
-
#get_comments_divs ⇒ Array
Gets all the comments’ divs in the open page.
-
#get_message(div) ⇒ Hash
Gets a hash object containing information about a given message.
-
#get_message_author(div) ⇒ String
Gets the reddit user who posted the message.
-
#get_message_content(div) ⇒ String
Gets the content of the message.
-
#get_message_divs ⇒ Array<Watir::Div>
Gets all message divs in the page.
-
#get_message_post(div) ⇒ String
Gets the post that a message belongs to.
-
#get_message_subreddit(div) ⇒ String
Gets the subreddit which the message was posted in.
-
#get_message_type(div) ⇒ String
Gets the type of a message.
-
#get_message_vote(div) ⇒ String?
Gets the type of vote the given message received from the logged in user.
-
#get_messages(subpage, all_pages = false) ⇒ Array
Gets all the messages in a given subpage, raises an exception if an unknown subpage is given.
-
#get_moderating ⇒ Array
Gets the currently open user’s moderating pages.
-
#get_moderators(subreddit) ⇒ Array
Gets an array including the usernames of the moderators of the given subreddit.
-
#get_original_post_vote ⇒ String?
Gets the type of the vote the logged in account voted the original post that is currently open.
- #get_phase ⇒ Object
-
#get_post(div) ⇒ Hash
Gets a hash containing information about a given post.
-
#get_post_author(div) ⇒ String
Gets the author of the post.
-
#get_post_karma(div) ⇒ Integer
Gets the amount of karma of the post.
-
#get_post_link(div) ⇒ String
Gets the link of the post.
-
#get_post_number_of_comments(div) ⇒ Integer
Gets the number of comments in the post.
-
#get_post_title(div) ⇒ String
Gets the title of the post.
-
#get_post_vote(div) ⇒ String?
Gets the type of vote the post has.
-
#get_posts(subreddit, subpage = 'hot', max_pages = 1) ⇒ Array
Gets all the posts in the given subreddit.
-
#get_posts_divs ⇒ Array
Gets all the posts’ divs.
-
#get_replies_divs(main_div) ⇒ Array
Gets all replies’ divs to the given comment.
-
#get_reply_error ⇒ String
Gets the reason for the reply error.
-
#get_side_bar ⇒ String
Gets content of the subreddit currently open sidebar.
-
#get_subreddit(subreddit) ⇒ Hash
Gets a hash with information about the given subreddit.
-
#get_subscribers ⇒ Integer
Gets the number of subscribers the subreddit currently open has.
-
#get_user(user) ⇒ Hash
Gets a hash containing information about the given user.
-
#get_user_activities(user, sortby = 'new', max_pages = 1) ⇒ Array
Gets all the user activities from the given user, raises exception if unknown sorting method is used.
-
#get_user_comment_karma ⇒ Integer
Gets the currently open user’s comment karma.
-
#get_user_post_karma ⇒ Integer
Gets the currently open user’s post karma.
-
#get_users_online ⇒ Integer
Gets the number of online users the subreddit currently open has.
-
#has_browser ⇒ Boolean
Checks if the class has a browser assigned.
-
#has_over_18 ⇒ Boolean
Checks if reddit is asking if user is over 18 for nsfw content.
-
#has_over_18_new ⇒ Boolean
Checks if reddit is asking if user is over 18 for nsfw content, uses the new interface.
-
#has_reply(answer) ⇒ Boolean
Checks if the logged in account has already replied to the given post with the given answer.
-
#has_reply_error ⇒ Boolean
Checks if there was an error when replying.
-
#has_submit_error ⇒ Boolean
Checks if there was an error when submiting to a subreddit, typically because of the 10 minute cooldown between posts enforced by reddit.
-
#is_activity_voted(div, type) ⇒ Boolean
Checks if the activity is voted by the logged in account, ‘up’ or ‘down’, raises an exception if an unknown vote type is submitted.
-
#is_comment_voted(div, type) ⇒ Boolean
Checks if the comment is voted by the logged in account, ‘up’ or ‘down’, raises an exception if an unknown vote type is submitted.
-
#is_friend ⇒ Boolean
Checks if the currently open user is a friend of the logged in account.
-
#is_logged_in(username) ⇒ Boolean
Checks if a given account is logged in.
-
#is_message_voted(div, type) ⇒ Boolean
Checks if a given message is voted by the logged in user.
-
#is_moderator ⇒ Boolean
Checks if the currently open user is a moderator of any subreddit.
-
#is_original_post_voted(type) ⇒ Boolean
Checks if the currently open post is voted by the logged in account, raises an exception if an unknown vote type is submitted.
-
#is_post_voted(div, type) ⇒ Boolean
Checks if the post is voted in the given type.
-
#is_submit_open ⇒ Boolean
Checks if the submit page is open.
-
#is_subreddit_banned(subreddit) ⇒ Boolean
Checks if the given subreddit is banned.
-
#is_user_banned(user) ⇒ Boolean
Checks if the given user is banned.
-
#login(username, password) ⇒ Object
Logins the reddit website, raises an exception on failure.
-
#logout ⇒ Object
Waits for logout, raises an exception on failure.
-
#message_move_page(direction) ⇒ Boolean
Moves to the next or previous page in the message inbox.
- #move_phase ⇒ Object
-
#open_messages_subpage(subpage) ⇒ Object
Opens the messages subpage, raises an exception if an unknown subpage is given.
-
#open_post(post) ⇒ Object
Opens the given post.
-
#open_subreddit(subreddit, subpage = 'hot') ⇒ Object
Opens the given subreddit in the given subpage, raises an exception if an unknown subpage is given, known subpages: ‘hot’, ‘new’, ‘rising’, ‘top’, ‘gilded’.
-
#open_user_page(user) ⇒ Object
Opens the page of the given username.
-
#parse_comments_divs(divs) ⇒ Array
Parses all the comments divs and replies.
- #phase_enter_data(username, password, captcha_token) ⇒ Object
- #phase_pick_subs ⇒ Object
-
#remove_friend ⇒ Object
Removes the currently open user as a friend, does nothing if the user is not a friend.
-
#reply_comment(div, answer) ⇒ Object
Replies the given comment with the given anwer.
-
#reply_message(div, answer) ⇒ Object
Replies the given message.
-
#reply_post(post, answer) ⇒ Boolean
Replies the given post.
-
#set_flair(flair) ⇒ Object
Sets the given flair to the post, does nothing if the subreddit has no flair option.
-
#skip_over_18 ⇒ Object
Skips the are you over 18 prompt.
-
#skip_over_18_new ⇒ Object
Skips the are you over 18 prompt, uses the new interface.
-
#sub_has_flair ⇒ Boolean
Checks if the subreddit has an option to add a flair to posts.
-
#submit_link(subreddit, url, title) ⇒ Object
Submits a link to the given subreddit, raises an exception on failure.
-
#submit_link_new(subreddit, url, title, flair = nil) ⇒ Object
Submits a link to the given subreddit using the new interface, raises an exception on failure.
-
#submit_text(subreddit, title, text) ⇒ Object
Submits a text post to the given subreddit, raises an exception on failure.
-
#subreddit_move_page(direction) ⇒ Boolean
Moves to the next or previous page in the subreddit.
-
#user_move_page(direction) ⇒ Boolean
Moves to the next or previous page in the user’s activity box.
-
#vote_activity(div, type) ⇒ Object
Votes the given activity.
-
#vote_comment(div, type) ⇒ Object
Votes the given comment.
-
#vote_message(div, type) ⇒ Object
Votes the given message.
-
#vote_original_post(type) ⇒ Object
Votes the currently open original post, does nothing if already voted, raises an exception if an unknown vote type is submitted.
-
#vote_post(div, type) ⇒ Object
Votes the given post, ‘up’ or ‘down’, raises exception if unknown vote type is sumited.
-
#wait_login(username) ⇒ Object
Waits for a successful loggin, raises an exception if it fails to login.
-
#wait_logout ⇒ Object
Waits for logout, raises an exception on failure.
-
#wait_reply(time = 2) ⇒ Boolean
Sleeps the given time then checks if there was an error when replying.
-
#wait_sub_creation ⇒ Boolean
Waits for the creation of the subreddit.
-
#wait_submit ⇒ Object
Waits for the submit page to open, raises an exception if it fails to open after 10 seconds.
Instance Attribute Details
#browser ⇒ Object
Returns the value of attribute browser.
42 43 44 |
# File 'lib/reddit_auto.rb', line 42 def browser @browser end |
#username ⇒ Object
Returns the value of attribute username.
43 44 45 |
# File 'lib/reddit_auto.rb', line 43 def username @username end |
Instance Method Details
#add_friend ⇒ Object
Adds the currently open user as a friend, does nothing if the user is already a friend
966 967 968 969 |
# File 'lib/reddit_auto.rb', line 966 def add_friend return if is_friend @browser.link(text: '+ friends').click end |
#comment_has_karma(div) ⇒ Boolean
Checks if the given comment has karma
562 563 564 |
# File 'lib/reddit_auto.rb', line 562 def comment_has_karma(div) return div.span(class: 'score').present? end |
#comment_has_replies(div) ⇒ Boolean
Checks if a given comment has replies
486 487 488 |
# 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) ⇒ Hash
Creates an account on reddit
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 |
# 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.(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) ⇒ Boolean
Creates a subreddit with the given parameters
909 910 911 912 913 914 915 916 917 918 919 920 |
# 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. @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.(text: 'create').click return wait_sub_creation end |
#did_create_sub ⇒ Boolean
Checks if the subreddit was successfully created
888 889 890 |
# 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 ⇒ Object
Expands all comments in the open page
626 627 628 629 630 631 632 633 634 635 |
# File 'lib/reddit_auto.rb', line 626 def while true begin span = @browser.span(class: 'morecomments') span.present? ? span.click : return rescue end sleep 0.5 end end |
#form_post_url(link) ⇒ String
Forms the full post url given the post’s link
411 412 413 |
# File 'lib/reddit_auto.rb', line 411 def form_post_url(link) return PAGE_MAIN_NO_SLASH + link end |
#form_subreddit_mod_url(subreddit) ⇒ String
Forms the full url for the subreddit’s moderator’s page
826 827 828 829 830 831 832 833 |
# 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') ⇒ String
Forms the full subreddit url given the subreddit’s name
776 777 778 |
# File 'lib/reddit_auto.rb', line 776 def form_subreddit_url(name, subpage = 'hot') return PAGE_SUBREDDIT + name + '/' + subpage end |
#get_activity(div) ⇒ Object
Gets a hash containing information about the given activity
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 |
# 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) ⇒ String
Gets the activity’s content if it is a comment
1053 1054 1055 |
# 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 ⇒ Array
Gets all the ativity divs from the currently open user
1008 1009 1010 1011 1012 1013 1014 1015 |
# 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) ⇒ Integer
Gets the amount of karma the activity received, ‘up’, ‘down’ or overall
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 |
# 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_link(div) ⇒ String
Gets the link of the activity
1029 1030 1031 |
# File 'lib/reddit_auto.rb', line 1029 def get_activity_link(div) return div.attribute_value('data-permalink') end |
#get_activity_subreddit(div) ⇒ String
Gets the subreddit of the activity
1037 1038 1039 |
# File 'lib/reddit_auto.rb', line 1037 def get_activity_subreddit(div) return div.attribute_value('data-subreddit') end |
#get_activity_title(div) ⇒ String
Gets the title of the activity
1045 1046 1047 |
# File 'lib/reddit_auto.rb', line 1045 def get_activity_title(div) return div.link(class: 'title').text end |
#get_activity_type(div) ⇒ String
Gets the type of the activity
1021 1022 1023 |
# File 'lib/reddit_auto.rb', line 1021 def get_activity_type(div) return div.attribute_value('data-type') end |
#get_activity_vote(div) ⇒ String?
Gets the activity’s vote by the logged in account
1093 1094 1095 |
# File 'lib/reddit_auto.rb', line 1093 def get_activity_vote(div) return get_comment_vote(div) end |
#get_banned_subreddits ⇒ Array
Gets all the subreddits the currently logged user is banned in
1158 1159 1160 1161 1162 1163 1164 1165 |
# File 'lib/reddit_auto.rb', line 1158 def get_banned_subreddits msgs = ('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) ⇒ Hash
Gets a hash containing information about a given comment
570 571 572 573 574 575 576 577 578 |
# File 'lib/reddit_auto.rb', line 570 def get_comment(div) result = {} result['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) ⇒ String
Gets the author of the comment
494 495 496 |
# File 'lib/reddit_auto.rb', line 494 def (div) return div.attribute_value('data-author') end |
#get_comment_content(div) ⇒ String
Gets content of the comment
510 511 512 |
# 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) ⇒ Integer
Gets the amount of karma the comment received, ‘up’, ‘down’ or overall
519 520 521 522 523 524 525 526 527 528 529 |
# 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_link(div) ⇒ String
Gets the link of the comment
502 503 504 |
# File 'lib/reddit_auto.rb', line 502 def get_comment_link(div) return div.attribute_value('data-permalink') end |
#get_comment_replies_count(div) ⇒ Integer
Gets the number of replies the given comment received
478 479 480 |
# 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) ⇒ String?
Gets the comment’s vote by the logged in account
552 553 554 555 556 |
# 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) ⇒ Array
Gets all the comments in the given post
642 643 644 645 646 |
# File 'lib/reddit_auto.rb', line 642 def get_comments(post, = false) open_post(post) if return parse_comments_divs(get_comments_divs) end |
#get_comments_divs ⇒ Array
Gets all the comments’ divs in the open page
583 584 585 586 587 588 589 590 |
# 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) ⇒ Hash
Gets a hash object containing information about a given message
228 229 230 231 232 233 234 235 236 237 |
# File 'lib/reddit_auto.rb', line 228 def (div) #returns a hash with message data result = {} result['type'] = (div) result['author'] = (div) result['post'] = (div) if result['type'] == 'comment' result['subreddit'] = result['type'] == 'comment' ? (div) : result['author'] result['vote'] = (div) if result['type'] == 'comment' result['content'] = (div) return result end |
#get_message_author(div) ⇒ String
Gets the reddit user who posted the message
154 155 156 |
# File 'lib/reddit_auto.rb', line 154 def (div) return div.attribute_value('data-author') end |
#get_message_content(div) ⇒ String
Gets the content of the message
170 171 172 |
# File 'lib/reddit_auto.rb', line 170 def (div) return div.div(class: 'md').text end |
#get_message_divs ⇒ Array<Watir::Div>
Gets all message divs in the page
177 178 179 180 181 182 183 184 |
# File 'lib/reddit_auto.rb', line 177 def 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) ⇒ String
Gets the post that a message belongs to
146 147 148 |
# File 'lib/reddit_auto.rb', line 146 def (div) return div.p(class: 'subject').link(class: 'title').href end |
#get_message_subreddit(div) ⇒ String
Gets the subreddit which the message was posted in
162 163 164 |
# File 'lib/reddit_auto.rb', line 162 def (div) return div.attribute_value('data-subreddit') end |
#get_message_type(div) ⇒ String
Gets the type of a message
138 139 140 |
# File 'lib/reddit_auto.rb', line 138 def (div) return div.attribute_value('data-type') end |
#get_message_vote(div) ⇒ String?
Gets the type of vote the given message received from the logged in user
199 200 201 202 203 |
# File 'lib/reddit_auto.rb', line 199 def (div) return 'up' if (div, 'up') return 'down' if (div, 'down') return nil end |
#get_messages(subpage, all_pages = false) ⇒ Array
Gets all the messages in a given subpage, raises an exception if an unknown subpage is given
263 264 265 266 267 268 269 270 271 272 |
# File 'lib/reddit_auto.rb', line 263 def (subpage, all_pages = false) (subpage) result = [] while true .each do |div| result.push (div) end return result if !all_pages || !('next') end end |
#get_moderating ⇒ Array
Gets the currently open user’s moderating pages
950 951 952 953 954 955 956 |
# File 'lib/reddit_auto.rb', line 950 def 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) ⇒ Array
Gets an array including the usernames of the moderators of the given subreddit
839 840 841 842 843 844 845 846 847 |
# 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 ⇒ String?
Gets the type of the vote the logged in account voted the original post that is currently open
392 393 394 395 396 |
# 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 ⇒ Object
1206 1207 1208 |
# File 'lib/reddit_auto.rb', line 1206 def get_phase return @browser.(class: 'c-btn c-btn-primary subreddit-picker__subreddit-button').present? ? 'picksubs' : 'enterdata' end |
#get_post(div) ⇒ Hash
Gets a hash containing information about a given post
752 753 754 755 756 757 758 759 760 761 |
# File 'lib/reddit_auto.rb', line 752 def get_post(div) result = {} result['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) ⇒ String
Gets the author of the post
687 688 689 |
# File 'lib/reddit_auto.rb', line 687 def (div) return div.attribute_value('data-author') end |
#get_post_karma(div) ⇒ Integer
Gets the amount of karma of the post
703 704 705 |
# 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) ⇒ String
Gets the link of the post
695 696 697 |
# 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) ⇒ Integer
Gets the number of comments in the post
719 720 721 |
# 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) ⇒ String
Gets the title of the post
711 712 713 |
# File 'lib/reddit_auto.rb', line 711 def get_post_title(div) return div.link(class: 'title').text end |
#get_post_vote(div) ⇒ String?
Gets the type of vote the post has
736 737 738 |
# 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) ⇒ Array
Gets all the posts in the given subreddit
807 808 809 810 811 812 813 814 815 816 817 818 819 820 |
# 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 ⇒ Array
Gets all the posts’ divs
674 675 676 677 678 679 680 681 |
# 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) ⇒ Array
Gets all replies’ divs to the given comment
596 597 598 599 600 601 602 603 604 605 606 607 608 |
# 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 ⇒ String
Gets the reason for the reply error
449 450 451 |
# File 'lib/reddit_auto.rb', line 449 def get_reply_error return @browser.span(class: 'error', style: '').split(" ")[1] end |
#get_side_bar ⇒ String
Gets content of the subreddit currently open sidebar
866 867 868 |
# File 'lib/reddit_auto.rb', line 866 def return @browser.div(class: 'usertext-body').text end |
#get_subreddit(subreddit) ⇒ Hash
Gets a hash with information about the given subreddit
874 875 876 877 878 879 880 881 882 883 |
# 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'] = result['moderators'] = get_moderators(subreddit) return result end |
#get_subscribers ⇒ Integer
Gets the number of subscribers the subreddit currently open has
852 853 854 |
# 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) ⇒ Hash
Gets a hash containing information about the given user
989 990 991 992 993 994 995 996 997 998 999 |
# 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'] = if is_moderator return result end |
#get_user_activities(user, sortby = 'new', max_pages = 1) ⇒ Array
Gets all the user activities from the given user, raises exception if unknown sorting method is used
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 |
# 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 ⇒ Integer
Gets the currently open user’s comment karma
936 937 938 |
# 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 ⇒ Integer
Gets the currently open user’s post karma
929 930 931 |
# 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 ⇒ Integer
Gets the number of online users the subreddit currently open has
859 860 861 |
# 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 ⇒ Boolean
Checks if the class has a browser assigned
48 49 50 |
# File 'lib/reddit_auto.rb', line 48 def has_browser return @browser != nil end |
#has_over_18 ⇒ Boolean
Checks if reddit is asking if user is over 18 for nsfw content
109 110 111 |
# File 'lib/reddit_auto.rb', line 109 def has_over_18 return @browser.(name: 'over18').present? end |
#has_over_18_new ⇒ Boolean
Checks if reddit is asking if user is over 18 for nsfw content, uses the new interface
121 122 123 |
# 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) ⇒ Boolean
Checks if the logged in account has already replied to the given post with the given answer
434 435 436 437 |
# 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 ⇒ Boolean
Checks if there was an error when replying
442 443 444 |
# File 'lib/reddit_auto.rb', line 442 def has_reply_error return @browser.span(class: 'error', style: '').present? end |
#has_submit_error ⇒ Boolean
Checks if there was an error when submiting to a subreddit, typically because of the 10 minute cooldown between posts enforced by reddit
281 282 283 |
# 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) ⇒ Boolean
Checks if the activity is voted by the logged in account, ‘up’ or ‘down’, raises an exception if an unknown vote type is submitted
1085 1086 1087 |
# 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) ⇒ Boolean
Checks if the comment is voted by the logged in account, ‘up’ or ‘down’, raises an exception if an unknown vote type is submitted
536 537 538 539 540 541 542 543 544 545 546 |
# 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 ⇒ Boolean
Checks if the currently open user is a friend of the logged in account
961 962 963 |
# 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) ⇒ Boolean
Checks if a given account is logged in
56 57 58 |
# File 'lib/reddit_auto.rb', line 56 def is_logged_in(username) return @browser.link(text: username).present? end |
#is_message_voted(div, type) ⇒ Boolean
Checks if a given message is voted by the logged in user
191 192 193 |
# File 'lib/reddit_auto.rb', line 191 def (div, type) return div.div(class: 'midcol').div(class: type + 'mod').present? end |
#is_moderator ⇒ Boolean
Checks if the currently open user is a moderator of any subreddit
943 944 945 |
# File 'lib/reddit_auto.rb', line 943 def is_moderator return @browser.ul(id: 'side-mod-list').present? end |
#is_original_post_voted(type) ⇒ Boolean
Checks if the currently open post is voted by the logged in account, raises an exception if an unknown vote type is submitted
377 378 379 380 381 382 383 384 385 386 387 |
# 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) ⇒ Boolean
Checks if the post is voted in the given type
728 729 730 |
# File 'lib/reddit_auto.rb', line 728 def is_post_voted(div, type) return is_comment_voted(div, type) end |
#is_submit_open ⇒ Boolean
Checks if the submit page is open
288 289 290 |
# 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) ⇒ Boolean
Checks if the given subreddit is banned
1251 1252 1253 1254 1255 |
# 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) ⇒ Boolean
Checks if the given user is banned
1241 1242 1243 1244 1245 |
# 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) ⇒ Object
Logins the reddit website, raises an exception on failure
78 79 80 81 82 83 84 85 86 |
# 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.(text: 'login').click wait_login(username) end |
#logout ⇒ Object
Waits for logout, raises an exception on failure
101 102 103 104 |
# File 'lib/reddit_auto.rb', line 101 def logout @browser.link(text: 'logout').click wait_logout end |
#message_move_page(direction) ⇒ Boolean
Moves to the next or previous page in the message inbox
243 244 245 246 247 248 |
# File 'lib/reddit_auto.rb', line 243 def (direction) = @browser.span(class: direction + '-button') result = .present? .click if result return result end |
#move_phase ⇒ Object
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 |
# File 'lib/reddit_auto.rb', line 1189 def move_phase @browser.(text: 'Submit').each do || if .present? .click sleep 5 return end end @browser.(text: 'Next').each do || if .present? .click sleep 5 return end end end |
#open_messages_subpage(subpage) ⇒ Object
Opens the messages subpage, raises an exception if an unknown subpage is given
253 254 255 256 |
# File 'lib/reddit_auto.rb', line 253 def (subpage) raise 'Unknown message subpage: ' + subpage if !MESSAGE_SUBPAGES.include? subpage @browser.goto PAGE_MESSAGES + subpage end |
#open_post(post) ⇒ Object
Opens the given post
418 419 420 421 422 423 424 425 426 427 428 |
# 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') ⇒ Object
Opens the given subreddit in the given subpage, raises an exception if an unknown subpage is given, known subpages: ‘hot’, ‘new’, ‘rising’, ‘top’, ‘gilded’
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 |
# 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) ⇒ Object
Opens the page of the given username
980 981 982 983 |
# 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) ⇒ Array
Parses all the comments divs and replies
614 615 616 617 618 619 620 621 622 623 |
# 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) ⇒ Object
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 |
# 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 ⇒ Object
1167 1168 1169 |
# 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 ⇒ Object
Removes the currently open user as a friend, does nothing if the user is not a friend
972 973 974 975 |
# File 'lib/reddit_auto.rb', line 972 def remove_friend return if !is_friend @browser.link(text: '- friends').click end |
#reply_comment(div, answer) ⇒ Object
Replies the given comment with the given anwer
652 653 654 655 656 |
# File 'lib/reddit_auto.rb', line 652 def reply_comment(div, answer) div.li(text: 'reply').click div.textarea(name: 'text').set answer div.(class: 'save').click end |
#reply_message(div, answer) ⇒ Object
Replies the given message
218 219 220 221 222 |
# File 'lib/reddit_auto.rb', line 218 def (div, answer) div.li(text: 'reply').click div.textarea.set answer div.(text: 'save').click end |
#reply_post(post, answer) ⇒ Boolean
Replies the given post
467 468 469 470 471 472 |
# 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').(text: 'save').click return wait_reply end |
#set_flair(flair) ⇒ Object
Sets the given flair to the post, does nothing if the subreddit has no flair option
326 327 328 329 330 331 332 333 334 335 |
# 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.(text: 'Apply').click end |
#skip_over_18 ⇒ Object
Skips the are you over 18 prompt
114 115 116 |
# File 'lib/reddit_auto.rb', line 114 def skip_over_18 @browser.(text: 'continue').click end |
#skip_over_18_new ⇒ Object
Skips the are you over 18 prompt, uses the new interface
126 127 128 |
# File 'lib/reddit_auto.rb', line 126 def skip_over_18_new @browser.link(text: 'Yes').click end |
#sub_has_flair ⇒ Boolean
Checks if the subreddit has an option to add a flair to posts
319 320 321 |
# File 'lib/reddit_auto.rb', line 319 def sub_has_flair return !@browser.div('aria-label': 'Not available for this community').present? end |
#submit_link(subreddit, url, title) ⇒ Object
Submits a link to the given subreddit, raises an exception on failure
307 308 309 310 311 312 313 314 |
# File 'lib/reddit_auto.rb', line 307 def submit_link(subreddit, url, title) @browser.goto PAGE_SUBREDDIT + subreddit + '/submit' skip_over_18 if has_over_18 @browser.text_field(id: 'url').set url @browser.textarea(name: 'title').set title @browser.(name: 'submit').click wait_submit end |
#submit_link_new(subreddit, url, title, flair = nil) ⇒ Object
Submits a link to the given subreddit using the new interface, raises an exception on failure
343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/reddit_auto.rb', line 343 def submit_link_new(subreddit, url, title, flair = nil) #uses new reddit @browser.goto 'https://www.reddit.com/r/' + subreddit + '/submit' skip_over_18_new if has_over_18_new blink = @browser.(text: 'Link') blink.click if blink.present? @browser.textarea(placeholder: 'Title').set title @browser.textarea(placeholder: 'Url').set url set_flair(flair) @browser.(text: 'Post')[1].click wait_submit end |
#submit_text(subreddit, title, text) ⇒ Object
Submits a text post to the given subreddit, raises an exception on failure
360 361 362 363 364 365 366 367 |
# 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.(name: 'submit').click wait_submit end |
#subreddit_move_page(direction) ⇒ Boolean
Moves to the next or previous page in the subreddit
767 768 769 |
# File 'lib/reddit_auto.rb', line 767 def subreddit_move_page(direction) return (direction) end |
#user_move_page(direction) ⇒ Boolean
Moves to the next or previous page in the user’s activity box
1125 1126 1127 |
# File 'lib/reddit_auto.rb', line 1125 def user_move_page(direction) return (direction) end |
#vote_activity(div, type) ⇒ Object
Votes the given activity
1101 1102 1103 |
# File 'lib/reddit_auto.rb', line 1101 def vote_activity(div, type) (div, type) end |
#vote_comment(div, type) ⇒ Object
Votes the given comment
662 663 664 665 |
# 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) ⇒ Object
Votes the given message
209 210 211 212 |
# File 'lib/reddit_auto.rb', line 209 def (div, type) return if (div, type) div.div(class: 'midcol').div(class: type).click end |
#vote_original_post(type) ⇒ Object
Votes the currently open original post, does nothing if already voted, raises an exception if an unknown vote type is submitted
401 402 403 404 405 |
# 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) ⇒ Object
Votes the given post, ‘up’ or ‘down’, raises exception if unknown vote type is sumited
744 745 746 |
# File 'lib/reddit_auto.rb', line 744 def vote_post(div, type) vote_comment(div, type) end |
#wait_login(username) ⇒ Object
Waits for a successful loggin, raises an exception if it fails to login
63 64 65 66 67 68 69 70 71 72 |
# 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 ⇒ Object
Waits for logout, raises an exception on failure
89 90 91 92 93 94 95 96 97 98 |
# 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) ⇒ Boolean
Sleeps the given time then checks if there was an error when replying
457 458 459 460 |
# File 'lib/reddit_auto.rb', line 457 def wait_reply(time = 2) sleep time return !has_reply_error end |
#wait_sub_creation ⇒ Boolean
Waits for the creation of the subreddit
895 896 897 898 899 900 901 902 903 |
# 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 ⇒ Object
Waits for the submit page to open, raises an exception if it fails to open after 10 seconds
293 294 295 296 297 298 299 300 |
# 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 |