Advertisement
Guest User

Inst

a guest
Oct 4th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.16 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require "json"
  3. require "open-uri"
  4. require "nokogiri"
  5. require "net/http"
  6. require "uri"
  7. class Node
  8.     attr_accessor :code,:video,:src
  9.     def initialize(node_json)
  10.         @code = node_json["code"]
  11.         @video = node_json["is_video"]
  12.         @src = node_json["display_src"]
  13.     end
  14.     def get_video
  15.             page = fetch_page("https://instagram.com/p/#{@code}")
  16.             return page["entry_data"]["PostPage"][0]["media"]["video_url"]
  17.     end
  18. end
  19.  
  20. class IGPage
  21.     def initialize(json)
  22.         @username = json["entry_data"]["ProfilePage"][0]["user"]["username"]
  23.         @nodes = json["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]
  24.         @next_page = json["entry_data"]["ProfilePage"][0]["user"]["media"]["page_info"]["has_next_page"]
  25.         @end = json["entry_data"]["ProfilePage"][0]["user"]["media"]["page_info"]["end_cursor"]
  26.         @token = json["config"]["csrf_token"]
  27.         @user_id = json["entry_data"]["ProfilePage"][0]["user"]["id"]
  28.         Dir.mkdir @username unless File.exists? @username
  29.     end
  30.     def downloadPosts
  31.         @nodes.each do |node|
  32.             node = Node.new(node)
  33.             if !node.video
  34.                 f = File.new("#{@username}/#{node.code}.jpg","w+")
  35.                 puts "Downloading #{node.src}"
  36.                 f << open(node.src).read
  37.             else
  38.                 f =  File.new("#{@username}/#{node.code}.mp4","w+")
  39.                 puts "#Downloaing #{node.get_video}"
  40.                 f << open(node.get_video).read
  41.             end
  42.             f.close
  43.             sleep 1
  44.         end
  45.         if @next_page
  46.             get_next_page
  47.         end
  48.     end
  49.     def get_next_page
  50.         uri = URI.parse("https://www.instagram.com/query/")
  51.         request = Net::HTTP::Post.new(uri)
  52.         request.content_type = "application/x-www-form-urlencoded"
  53.         request["User-Agent"] = "Mozilla/5.0 (X11; Linux i686; rv:47.0) Gecko/20100101 Firefox/47.0"
  54.         request["Accept"] = "*/*"
  55.         request["Accept-Language"] = "en-US,en;q=0.5"
  56.         request["X-Csrftoken"] = @token
  57.         request["X-Instagram-Ajax"] = "1"
  58.         request["X-Requested-With"] = "XMLHttpRequest"
  59.         request["Referer"] = "https://www.instagram.com/"
  60.         request["Cookie"] = "csrftoken=#{@token};"
  61.         request["Connection"] = "keep-alive"
  62.         request.set_form_data(
  63.           "q" => "ig_user(#{@user_id}) { media.after(#{@end}, 12) {
  64.           count,
  65.           nodes {
  66.             caption,
  67.             code,
  68.             comments {
  69.               count
  70.             },
  71.             comments_disabled,
  72.             date,
  73.             dimensions {
  74.               height,
  75.               width
  76.             },
  77.             display_src,
  78.             id,
  79.             is_video,
  80.             likes {
  81.               count
  82.             },
  83.             owner {
  84.               id
  85.             },
  86.             thumbnail_src,
  87.             video_views
  88.           },
  89.           page_info
  90.         }
  91.          }",
  92.           "ref" => "users::show",
  93.         )
  94.  
  95.         response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
  96.           http.request(request)
  97.         end
  98.         json = JSON.parse response.body
  99.         if json["status"] == "ok"
  100.             @nodes = json["media"]["nodes"]
  101.             @end = json["media"]["page_info"]["end_cursor"]
  102.             @next_page = json["media"]["page_info"]["has_next_page"]
  103.             downloadPosts
  104.         end
  105.     end
  106. end
  107.  
  108. def fetch_page(url)
  109.     page = Nokogiri::HTML(open(url))
  110.     body = page.search("script").select do |s|
  111.         s.text =~ /window\._sharedData/
  112.     end
  113.     json = JSON.parse body.first.text.split(" = ").last[0...-1]
  114.     return json
  115. end
  116. downloader = IGPage.new fetch_page(ARGV[0])
  117. downloader.downloadPosts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement