Class: Instagram

Inherits:
Object
  • Object
show all
Defined in:
lib/instascrap.rb

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ Instagram

Returns a new instance of Instagram.



8
9
10
11
# File 'lib/instascrap.rb', line 8

def initialize(url = nil)
  $url = url 
  @mechanize = Mechanize.new
end

Instance Method Details

#download_imgObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/instascrap.rb', line 13

def download_img
  unless $url.nil? 
    puts "download from #{$url}"
    puts "========== DOWNLOADING IMAGE =========="

    browser = Watir::Browser.new(:phantomjs)
    browser.goto($url)

             # parsing with nokogiri
    document = Nokogiri::HTML(browser.html)
    link = document.search("div._jjzlb img")[0]["src"]
    photo = browser.goto(link)
    FileUtils.mkdir_p 'ig_photos'
    FileUtils.cd 'ig_photos'
    pwd = FileUtils.pwd 

    getting_photo = @mechanize.get photo 
    unless File.exists?("#{pwd}/#{photo.to_s.split(".jpg").first + ".jpg" }")
      begin
        getting_photo.save!
      rescue
      end
    else
      puts "file already exist"
    end
  end

  puts "images: #{pwd}/#{getting_photo.filename}/"
end

#download_videosObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/instascrap.rb', line 43

def download_videos
  unless $url.nil? 
    puts "download from #{$url}"
    puts "========== DOWNLOADING VIDEO =========="

    browser = Watir::Browser.new(:phantomjs)
    browser.goto($url)

    # parsing with nokogiri
    document = Nokogiri::HTML(browser.html)
    link = document.search("video._c8hkj")[0]["src"]
    video = browser.goto(link)
    FileUtils.mkdir_p 'ig_movies'
    FileUtils.cd 'ig_movies'
    pwd = FileUtils.pwd 

    getting_video = @mechanize.get video
    unless File.exists?("#{pwd}/#{video.to_s.split(".mp4").first + ".mp4" }")
      begin
        getting_video.save!
      rescue
      end
    else
      puts "file already exist"
    end
  end

  puts "videos: #{pwd}/#{getting_video.filename}"
end