Created
August 11, 2017 20:18
-
-
Save theinventor/13114f4c8ac6f91e4f187e2b353eab1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class LittleSchoolPhotoDownloader | |
def connect | |
@user = OpenStruct.new(email: ENV['USER'],password: ENV['PASSWORD']) | |
@login_url = "https://www.thelittleschool.org/user/login?destination=my-portal" | |
@base_url = ENV['CLASSROOM_URL'] | |
@agent = Mechanize.new | |
page = @agent.get(@login_url) | |
form = page.forms.last | |
form.field_with('name').value = @user.email | |
form.field_with('pass').value = @user.password | |
@page = @agent.submit(form) | |
end | |
def get_galleries | |
@page = @agent.get(@base_url) | |
links = @page.links.select {|i| i.text.include?("Photos")} | |
links.each do |link| | |
scrape_gallery(link) | |
end | |
end | |
def scrape_gallery(link) | |
@page = @agent.click(link) | |
photos = @page.links.select {|i| i.href.include?("/file/")} | |
photos.each do |photo_link| | |
save_image(photo_link) | |
end | |
end | |
def save_image(photo_link) | |
photo_base =" https://www.thelittleschool.org/system/files/node_gallery/" | |
@agent.get("#{photo_base}#{photo_link.text}").save "#{@page.search("h2")[3].text} #{photo_link.text}" | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment