Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby脚本下载照片_Ruby - Fatal编程技术网

Ruby脚本下载照片

Ruby脚本下载照片,ruby,Ruby,目标:通过脚本从iCloud共享网络相册下载照片: 我在这里有以下脚本: (我已取出图像调整大小) 问题:照片似乎是下载的,但它们看起来都像这样(下面的脚本输出): #/usr/bin/env ruby #C:\Users\Win10IE11\Desktop\icloud共享相册下载主机\download_album2.rb B0Q5oqs3qGtDCal 需要“selenium webdriver” 需要“fileutils” 需要“yaml” 相册名称=ARGV[0] options=Se

目标:通过脚本从iCloud共享网络相册下载照片:

我在这里有以下脚本:

(我已取出图像调整大小)

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

#/usr/bin/env ruby
#C:\Users\Win10IE11\Desktop\icloud共享相册下载主机\download_album2.rb B0Q5oqs3qGtDCal
需要“selenium webdriver”
需要“fileutils”
需要“yaml”
相册名称=ARGV[0]
options=Selenium::WebDriver::Chrome::options.new(参数:['headless'])
driver=Selenium::WebDriver.for(:chrome,options:options)
放入“正在下载相册ID{album_name}”:
dir=“C:/Users/Win10IE11/Downloads/#{album_name}”
电影\u dir=“/home/pi/Videos”
FileUtils.mkdir\u p(dir)
URL\u seen=Set.new
文件\u seen=Set.new
驱动程序。获取(“https://www.icloud.com/sharedalbum/##{相册名称}”)
放置“导航到索引页:#{driver.current_url}”
睡眠2
driver.find_元素(css:“[role=button]”)。单击
睡眠5
c=0
当前url=驱动程序。当前url
先看到=错误
退出\u earld=false
直到看到url。包括?(当前url)或c>=200或提前退出
重试次数=0
开始
当前url=驱动程序。当前url
放置“导航到:#{current_url}”
url\u seen.add(当前url)
i=驱动程序。查找元素(css:“img”)
放置“下载图像#{c}:#{i[“src”]}”
u=URI.parse(i[“src”])
ext=u.path.split(“.”)。last.downcase
filename=“#{current_url.split(;”).last}.#{ext}.downcase
path=“#{dir}/#{filename}”
如果文件.exist?(路径)
如果c==0
先看到=正确
放置“已看到第一张图像,现在正在倒退”
elsif先看到_,c==1
提前退出=真
放置“已经看到最后一张图片,我们可能已经完成了!”
其他的
将“跳过已下载的文件#{path}”
结束
其他的
r=Net::HTTP.get\u响应(u)
放置“#{r.inspect}”
File.write(路径,r.body)
将“写入的长度为#{r.body.length}的文件放入#{path}”
视频=驱动程序。查找元素(css:“.play按钮”)
如果videos.length>0
放上“找到视频!!!”
视频。首先。单击
video\u src=driver.find_元素(css:“video>source”)[“src”]
u=URI.parse(视频\u src)
ext=u.path.split(“.”)。last.downcase
filename=“#{current#url.split(#)”.last.gsub(“;”,“#”)}.#{ext}.downcase
path=“#{movies_dir}/#{filename}”
将“从#{video_src}下载到#{path}”
driver.navigate.refresh
r=Net::HTTP.get\u响应(u)
File.write(路径,r.body)
将“写入的#{r.body.length}个视频字节放入#{path}”
结束
结束
c+=1
睡眠1
driver.find_元素(css:“body”)。发送_键(先看到?:arrow_left::arrow_right)
睡眠1
当前url=驱动程序。当前url
救援=>e
放置“错误:#{e.inspect}”
重试次数+=1
如果重试次数小于4次
司机,停止营救
放入“重试##{retries}”
系统“pkill-f chromedriver”
driver=Selenium::WebDriver.for(:chrome,options:options)
获取驱动程序(当前_url)
睡眠5
重试
结束
结束
结束
将“已完成的{c}照片放入相册{album{U name}!”
司机,退出
***输出:

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
导航到:https://www.icloud.com/sharedalbum/#B0Q5oqs3qGtDCal;64D46E01-D439-4FB3-9234-EEADFD92B4B8
下载图片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=Bchoczlbrd6M1lxehn6lxov32onlda-ufrgeaamxh0y&e=1538045095&r=900d8d25-0a15-43e5-be59-2a4c9267cfaf-36&s=C3ee21ErkyHFKzq-JWJZKXPAH4
#
将长度为1248141的文件写入C:/Users/Win10IE11/Downloads/B0Q5oqs3qGtDCal/64d46e01-d439-4fb3-9234-eeadfd92b4b8.jpg
将该函数替换为“打开uri”函数,可在此处找到文档:

替换旧代码:

File.write(path, r.body)
与:

以下是固定代码:

#!/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
#/usr/bin/env ruby
#C:\Users\Win10IE11\Desktop\icloud共享相册下载主机\dl5.rb B0Q5oqs3qGtDCal
需要“selenium webdriver”
需要“fileutils”
需要“yaml”
需要“打开uri”
相册名称=ARGV[0]
options=Selenium::WebDriver::Chrome::options.new(参数:['headless'])
driver=Selenium::WebDriver.for(:chrome,options:options)
放入“正在下载相册ID{album_name}”:
dir=“C:/Users/Win10IE11/Downloads/#{album_name}”
电影目录
FileUtils.mkdir\u p(dir)
URL\u seen=Set.new
文件\u seen=Set.new
驱动程序。获取(“https://www.icloud.com/sharedalbum/##{相册名称}”)
放置“导航到索引页:#{driver.current_url}”
睡眠1
driver.find_元素(css:“[role=button]”)。单击
睡眠1
c=0
当前url=驱动程序。当前url
先看到=正确
提前退出=错误
def下载_图像(url,目的地)
打开(url)do|u|
File.open(dest,'wb'){| f | f.write(u.read)}
将“保存的#{url}放入#{dest}”
结束
结束
直到看到url。包括?(当前url)或c>=200或提前退出
重试次数=0
开始
当前url=驱动程序。当前url
放置“导航到:#{current_url}”
url\u seen.add(当前url)
i=驱动程序。查找元素(css:“img”)
#C:\Users\Win10IE11\Desktop\icloud共享相册下载主机\dl5.rb B0Q5oqs3qGtDCal
放置“count#{c}”
视频=驱动程序。查找元素(css:“.play按钮”)
如果videos.length>0
#放上“找到视频!!!”
视频。首先。单击
i=驱动程序。查找元素(css:“视频>源”)
url=“#{i[“src”]}”
local1=“#{url.split('/').last}”
local=“#{c
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