Ruby Script下载照片

时间:2018-09-27 07:52:01

标签: ruby

目标:通过脚本https://www.icloud.com/sharedalbum/#B0Q5oqs3qGtDCal

从iCloud共享的网络相册下载照片

我在这里有以下脚本:https://github.com/dsboulder/icloud-shared-album-download/blob/master/download_album.rb

(我已将图像调整大小)

问题:照片似乎正在下载,但是它们看起来都像这样(下面的脚本输出还有):

enter image description here

    #!/usr/bin/env ruby

#  C:\Users\Win10IE11\Desktop\icloud-shared-album-download-master\download_album2.rb B0Q5oqs3qGtDCal

require 'selenium-webdriver'
require 'fileutils'
require 'yaml'

album_name = ARGV[0]

options = Selenium::WebDriver::Chrome::Options.new(args: ['headless'])
driver = Selenium::WebDriver.for(:chrome, options: options)
puts "Downloading album ID #{album_name}:"
dir = "C:/Users/Win10IE11/Downloads/#{album_name}"
movies_dir = "/home/pi/Videos"
FileUtils.mkdir_p(dir)
urls_seen = Set.new
files_seen = Set.new

driver.get("https://www.icloud.com/sharedalbum/##{album_name}")

puts "  Navigated to index page: #{driver.current_url}"

sleep 2

driver.find_element(css: "[role=button]").click

sleep 5

c = 0
current_url = driver.current_url 
seen_first = false
exit_early = false
until urls_seen.include?(current_url) or c >= 200 or exit_early do
    retries = 0
    begin
        current_url = driver.current_url 
        puts "  Navigated to: #{current_url}"
        urls_seen.add(current_url)
        i = driver.find_element(css: "img")
        puts "    Downloading image #{c}: #{i["src"]}"
        u = URI.parse(i["src"])
        ext = u.path.split(".").last.downcase
        filename = "#{current_url.split(";").last}.#{ext}".downcase
        path = "#{dir}/#{filename}"

        if File.exist?(path)
            if c == 0
                seen_first = true 
                puts "    Already seen first image, going backwards now"
            elsif seen_first and c == 1
                exit_early = true
                puts "    Already seen last image, we're probably done!"
            else
                puts "    Skipping already downloaded file #{path}"
            end
        else
            r = Net::HTTP.get_response(u)
            puts "  #{r.inspect}"
            File.write(path, r.body)
            puts "    Wrote file of length #{r.body.length} to #{path}" 

            videos = driver.find_elements(css: ".play-button")
            if videos.length > 0
                puts "    Found video!!!"
                videos.first.click
                video_src = driver.find_element(css: "video > source")["src"]
                u = URI.parse(video_src)
                ext = u.path.split(".").last.downcase
                filename = "#{current_url.split("#").last.gsub(";", "_")}.#{ext}".downcase
                path = "#{movies_dir}/#{filename}"
                puts "    Downloading from #{video_src} to #{path}"
                driver.navigate.refresh
                r = Net::HTTP.get_response(u)
                File.write(path, r.body)
                puts "    Wrote #{r.body.length} bytes of video to #{path}"
            end
        end
        c += 1
        sleep 1
        driver.find_element(css: "body").send_keys(seen_first ? :arrow_left : :arrow_right)
        sleep 1
        current_url = driver.current_url 
    rescue => e
        puts "Error: #{e.inspect}"
        retries += 1
        if retries < 4
            driver.quit rescue nil
            puts "RETRY ##{retries}"
            system "pkill -f chromedriver"
            driver = Selenium::WebDriver.for(:chrome, options: options)
            driver.get(current_url)
            sleep 5
            retry
        end
    end
end

puts "  Finished #{c} photos in album #{album_name}!"
driver.quit

***输出:

Navigated to: https://www.icloud.com/sharedalbum/#B0Q5oqs3qGtDCal;64D46E01-D439-4FB3-9234-EEADFD92B4B8
    Downloading image 22: https://cvws.icloud-content.com/S/AZmmX4aAk6O2XpXCavO3rA4XSNms/IMG_0023.JPG?o=AtHCwB51UajcHVvLEboQsSvM4hK5ZHb25DMLu5rjLgMs&v=1&z=https%3A%2F%2Fp26-content.icloud.com%3A443&x=1&a=BqocZLbrD6m1lXeHN6LXov32oNLDA-UfRgEAAAMxH0Y&e=1538045095&r=900d8d25-0a15-43e5-be59-2a4c9267cfaf-36&s=C3ee21ErkyHFKzq-JWjZkKXpah4
  #<Net::HTTPOK 200 OK readbody=true>
    Wrote file of length 1248141 to C:/Users/Win10IE11/Downloads/B0Q5oqs3qGtDCal/64d46e01-d439-4fb3-9234-eeadfd92b4b8.jpg

1 个答案:

答案 0 :(得分:0)

使用“ open-uri”功能替换了该功能,可以在以下位置找到文档:https://cobwwweb.com/download-collection-of-images-from-url-using-ruby.html

交换旧代码:

File.write(path, r.body)

具有:

File.open(dest, 'wb') { |f| f.write(u.read) }

这是固定代码:

#!/usr/bin/env ruby

#  C:\Users\Win10IE11\Desktop\icloud-shared-album-download-master\dl5.rb B0Q5oqs3qGtDCal

require 'selenium-webdriver'
require 'fileutils'
require 'yaml'
require 'open-uri'

album_name = ARGV[0]

options = Selenium::WebDriver::Chrome::Options.new(args: ['headless'])
driver = Selenium::WebDriver.for(:chrome, options: options)
puts "Downloading album ID #{album_name}:"
dir = "C:/Users/Win10IE11/Downloads/#{album_name}"
movies_dir = dir
FileUtils.mkdir_p(dir)
urls_seen = Set.new
files_seen = Set.new

driver.get("https://www.icloud.com/sharedalbum/##{album_name}")

puts "  Navigated to index page: #{driver.current_url}"

sleep 1

driver.find_element(css: "[role=button]").click

sleep 1

c = 0
current_url = driver.current_url 
seen_first = true
exit_early = false

def download_image(url, dest)
  open(url) do |u|
    File.open(dest, 'wb') { |f| f.write(u.read) }
    puts "Saved #{url} to #{dest}"
  end
end


until urls_seen.include?(current_url) or c >= 200 or exit_early do
    retries = 0
    begin
        current_url = driver.current_url 
        puts "  Navigated to: #{current_url}"
        urls_seen.add(current_url)
        i = driver.find_element(css: "img")

        #  C:\Users\Win10IE11\Desktop\icloud-shared-album-download-master\dl5.rb B0Q5oqs3qGtDCal

        puts " count #{c}"


        videos = driver.find_elements(css: ".play-button")
        if videos.length > 0
            #puts "    Found video!!!"
            videos.first.click

            i = driver.find_element(css: "video > source")
            url = "#{i["src"]}"         
            local1 = "#{url.split('/').last}"
            local = "#{c}_#{local1.split('?').first}"

            download_image(url, "#{dir}/#{local}")

            driver.navigate.refresh

            sleep 1

        else
            #puts "    not video!!!"
            url = "#{i["src"]}"
            local1 = "#{url.split('/').last}"
            local = "#{c}_#{local1.split('?').first}"

            download_image(url, "#{dir}/#{local}")
        end

        c += 1
        sleep 0.1
        driver.find_element(css: "body").send_keys(seen_first ? :arrow_left : :arrow_right)
        sleep 0.1
        current_url = driver.current_url 
    rescue => e
        puts "Error: #{e.inspect}"
        retries += 1
        if retries < 4
            driver.quit rescue nil
            puts "RETRY ##{retries}"
            system "pkill -f chromedriver"
            driver = Selenium::WebDriver.for(:chrome, options: options)
            driver.get(current_url)
            sleep 1
            retry
        end
    end
end

puts "  Finished #{c} photos in album #{album_name}!"
driver.quit