Last active
March 16, 2022 12:34
-
-
Save Beyarz/c7920ba96cd13159fa99e758ccf66e12 to your computer and use it in GitHub Desktop.
Download all the images from a user on https://vsco.co
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
| # frozen_string_literal: true | |
| # rubocop:disable Layout/LineLength | |
| require 'net/https' | |
| require 'ruby-cheerio' | |
| require 'json' | |
| require 'cgi' | |
| require 'open-uri' | |
| # Pick your target here | |
| # Example: https://vsco.co/TARGET | |
| TARGET = 'USERNAME' | |
| # The cookie can be grabbed from the devtool in your browser | |
| cookie_value = 'Cookie: ...' | |
| # rubocop:enable Layout/LineLength | |
| raise 'TARGET or cookie_value cannot be empty' if (TARGET || cookie_value).empty? | |
| def send_request(uri, params) | |
| req = Net::HTTP.get_response uri, params | |
| raise "Something went wrong, code #{req.code} with message #{req.body}" unless req.is_a? Net::HTTPSuccess | |
| req | |
| end | |
| ENDPOINT = 'https://vsco.co/' | |
| PATH = '/gallery' | |
| uri = URI(ENDPOINT + TARGET + PATH) | |
| params = { | |
| 'user-agent' => 'curl', | |
| Cookie: cookie_value | |
| } | |
| req = send_request(uri, params) | |
| parsed = RubyCheerio.new req.body | |
| script_tags = parsed.find('script') | |
| tags_length = script_tags.length | |
| padding = 3 | |
| script_tag_variable = script_tags[tags_length - padding] | |
| js_variable = script_tag_variable.text | |
| PREFIX = 'window.__PRELOADED_STATE__ = ' | |
| json_data = js_variable.delete_prefix(PREFIX) | |
| json = JSON.parse(json_data) | |
| user_id = json['sites']['siteByUsername'][TARGET]['site']['id'] | |
| cursor = nil | |
| Dir.mkdir('downloads') unless Dir.exist?('downloads') | |
| loop do | |
| api = "https://vsco.co/api/3.0/medias/profile?site_id=#{user_id}&limit=14&cursor=#{cursor}" | |
| uri = URI(api) | |
| req = send_request(uri, params) | |
| resp = JSON.parse(req.body) | |
| resp['media'].each do |image| | |
| download_file = "https://#{image['image']['responsive_url']}" | |
| file = URI(download_file).open | |
| format = image['image']['is_video'] ? 'mp4' : 'jpg' | |
| filename = File.expand_path("./downloads/#{image['image']['_id']}.#{format}") | |
| IO.copy_stream(file, filename) | |
| puts "Downloaded: #{download_file}" | |
| end | |
| next_cursor = resp['next_cursor'] | |
| break if next_cursor.nil? | |
| cursor = CGI.escape(resp['next_cursor']) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment