Skip to content

Instantly share code, notes, and snippets.

@benwoodward
Last active June 23, 2018 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benwoodward/9b8dd9f4e176f0d167af84fa5b95e01c to your computer and use it in GitHub Desktop.
Save benwoodward/9b8dd9f4e176f0d167af84fa5b95e01c to your computer and use it in GitHub Desktop.
Script to unclap/unrecommend all stories on Medium
require 'rubygems'
require 'selenium-webdriver'
class Unclap
USERNAME = "@yourusername"
def initialize
@driver = Selenium::WebDriver.for :chrome
@links_to_unclap = []
@problem_links = []
end
def start
@driver.get "https://medium.com/#{USERNAME}/has-recommended"
sleep 20 # This is where you manually log, however you log in
@driver.get "https://medium.com/#{USERNAME}/has-recommended"
scroll_to_end
fetch_links
end
def start_again
@links_to_unclap = []
@driver.get "https://medium.com/#{USERNAME}/has-recommended"
sleep 1
scroll_to_end
(@problem_links.count*2).times do
scroll_to_end
end
sleep 1
fetch_links
end
def process_links links
links.each do |link|
if link.attribute(:href) =~ /\/p\/[0-9a-z]*/
add_link_to_list link.attribute(:href).gsub(/\?.*/, '')
end
end
unclap_all
end
def add_link_to_list link
if @links_to_unclap.include?(link) || @problem_links.include?(link)
return
else
@links_to_unclap << link
end
end
def unclap_all
@links_to_unclap.each do |link|
begin
@driver.get link
@driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # need to scroll to the bottom lest the bottom bar gets in the way
el = @driver.find_element(:css, 'button[data-action="multivote"]')
@driver.action.move_to(el).perform
sleep 2
@driver.find_element(:css, 'button[data-action="multivote-undo"]').click
# @driver.find_element(:css, 'a[data-action="show-user-card"]').click
# Selenium::WebDriver::Error::ElementNotVisibleError occurs on articles with audio, they have an alternative layout and the unclap button isn't visible
# NoSuchElementError occurs on membership only pages
rescue Exception => e
puts e.message
@problem_links << link
puts "PROBLEM_LINKS: #{@problem_links}"
next
end
end
start_again
end
def fetch_links
links = @driver.find_elements(:tag_name, "a")
process_links links
end
def scroll_to_end
@driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep 10
end
end
Unclap.new.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment