In Ruby, you can use the mechanism to batch download photos from the School Intranet,

Source: Internet
Author: User

In Ruby, you can use the mechanism to batch download photos from the School Intranet,

You have nothing to do with it. You have used "machize" to create a code that can download photos of on-campus albums.

It's a little simple to write .... It mainly analyzes the user ID and album ID based on the album address, simulates the request to the album page, extracts all the photos, and downloads them to a local folder.

Ruby version: ruby1.8.7 or ruby1.9.2
Operating System: windows 7

I have not tried it in Linux ~ However, it should also be possible:

Copy codeThe Code is as follows:
# Encoding: UTF-8
Require 'rubygems'
Require 'delete'

Class Renren

Def initialize (e, p)
@ Agent = mechanic. new
@ Page = @ agent. get ('HTTP: // 3g.renren.com /')
@ Page = @ page. form_with (: method => 'post') do | r |
R. email = e
R. password = p
End. submit
End

Def fetch_other_photo (album_link, foldername)
Photo_urls = []
Puts Iconv. conv ("gb2312", "UTF-8", "Start to analyze the album address .....")
Begin
User_id, album_id = parse_album_uri (album_link)
Rescue
Puts Iconv. conv ("gb2312", "UTF-8", "Your album address is incorrect. Please enter it again! ")
Return
End
Page = @ agent. get ("http://3g.renren.com/album/wgetalbum.do? Id = # {user_id} & owner = # {album_id }")
Puts Iconv. conv ("gb2312", "UTF-8", "getting all photo addresses .....")
Loop do
Page. links_with (: href =>/http: \/3G \. renren \. com \/album \/wgetphoto \. do? /). Each do | link |
Photo = link. click
Photo_urls <photo. link_with (: text => "download this image"). href
End
Break if page. link_with (: text => "next page"). nil?
Page = page. link_with (: text => "next page"). click
End
If photo_urls.length> 0
Puts Iconv. conv ("gb2312", "UTF-8", "download album .....")
Unless File. directory? ("# {Foldername }")
Dir. mkdir ("# {foldername }")
End
Dir. chdir ("# {foldername}") do | path |
Photo_urls.each do | photo_url |
@ Agent. get (photo_url) do | photo |
Puts Iconv. conv ("gb2312", "UTF-8", "Saving file # {photo. filename }...... Downloaded # {(photo_urls.index (photo_url) + 1). to_f/photo_urls.length * 100). to_ I} % ")
Photo. save
End
End
End
Puts Iconv. conv ("gb2312", "UTF-8", "the album has been downloaded .....")
Else
Puts Iconv. conv ("gb2312", "UTF-8", "No photos in the album ~ ")
End
End

Private

Def parse_album_uri (uri)
Uri = uri. chomp ("# thumb ")
Uri = uri. split ("? ")
If uri. length> 1 and uri [1]. include? ("Owner ")
Uri = uri [1]. split ("&")
User_id = uri [0]. split ("=") [1]
Album_id = uri [1]. split ("=") [1]
Else
Uri = uri [0]. split ("/")
Album_id = uri [4]
User_id = uri [5]. split ("-") [1]
End
Return user_id, album_id
End
End

Print Iconv. conv ("gb2312", "UTF-8", "username :")
Username = gets. chomp ()
Print Iconv. conv ("gb2312", "UTF-8", "Password :")
Password = gets. chomp ()
Renren = Renren. new (username, password)
Loop do
Print Iconv. conv ("gb2312", "UTF-8", "paste album address :")
Uri = gets. chomp ()
Renren. fetch_other_photo (uri, username)
Print Iconv. conv ("gb2312", "UTF-8", "press 0 to exit the program, and press other keys to continue downloading other albums :")
Break if gets. chomp () = "0"
End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.