ig_scrape 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2d0a888ba09af0188257b8924cbaa83f85b15b0
4
+ data.tar.gz: 3d17f54b10d168c4aedc8eeadb40c280ffbb3fbc
5
+ SHA512:
6
+ metadata.gz: 74804b533c38dfd21e4b49aa63380ce652f91700873f2a4916c6a3e591194d5027aa56520d9b7194c756be5016650e70234084537e239a1d76a166eb0c5f7cf3
7
+ data.tar.gz: 624898bb60e1a872e409d843947e214cb0c2691cc8fb6f1d3408d883dcdf9f5c61d86c2faf87182b9120e6cefc170b15442e6d60afa8b76fc485aef177795593
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ig_scrape.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Trevor Kimenye
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # IG_Scrape
2
+
3
+ This gem provides a utility for scraping instagram posts and comments
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ig_scrape'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ig_scrape
20
+
21
+ ## Usage
22
+
23
+ ### Getting information about an Instagram account
24
+
25
+ ```ruby
26
+ require 'ig_scrape'
27
+
28
+ client = IGScrape::Client.new("username")
29
+ puts client.follower_count
30
+ puts client.follows_count
31
+ puts client.post_count
32
+ ```
33
+
34
+ ### Loading all the posts for an instagram account
35
+
36
+ ```ruby
37
+ require 'ig_scrape'
38
+ client = IGScrape::Client.new("username")
39
+ client.load
40
+
41
+ puts client.posts.length == client.post_count
42
+ ```
43
+
44
+ ### Loading a post
45
+
46
+ ```ruby
47
+ require 'ig_scrape'
48
+ code = "KVHudYDs"
49
+ post = IGScrape::Post.load_from_shortcode(code)
50
+ puts post.comment_count
51
+ puts post.has_more_comments?
52
+
53
+ post.load_more_comments
54
+ puts post.has_more_comments?
55
+ ```
56
+
57
+ ## Development
58
+
59
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
60
+
61
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ongair/ig_scrape.
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ig_scrape"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/ig_scrape.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ig_scrape/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ig_scrape"
8
+ spec.version = IGScrape::VERSION
9
+ spec.authors = ["Trevor Kimenye"]
10
+ spec.email = ["kimenye@gmail.com"]
11
+
12
+ spec.summary = %q{A gem to scrape instagram posts and comments}
13
+ spec.description = %q{A gem to scrape instagram posts and comments.}
14
+ spec.homepage = "https://github.com/ongair/ig_scrape"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest", "~> 5.0"
27
+ spec.add_development_dependency "mocha"
28
+ spec.add_development_dependency "webmock"
29
+ spec.add_development_dependency "minitest-reporters"
30
+ spec.add_dependency "httparty"
31
+ end
@@ -0,0 +1,68 @@
1
+ require 'httparty'
2
+
3
+ class IGScrape::Client
4
+
5
+ attr_accessor :username, :full_name, :follower_count, :follows_count, :id ,:post_count, :profile_pic_url, :posts
6
+
7
+ def initialize(username)
8
+ @username = username
9
+ @posts = []
10
+ load_profile
11
+ end
12
+
13
+ def load
14
+ while has_more_posts? do
15
+ load_more_posts
16
+ end
17
+ end
18
+
19
+ def has_more_posts?
20
+ @posts.length < @post_count
21
+ end
22
+
23
+ def loaded_post_count
24
+ @posts.length
25
+ end
26
+
27
+ private
28
+
29
+ def load_profile
30
+ url = "https://www.instagram.com/#{@username}/?__a=1"
31
+ resp = HTTParty.get(url)
32
+
33
+ response = JSON.parse(resp.body)
34
+ user = response["user"]
35
+ @full_name = user["full_name"]
36
+ @follower_count = user["followed_by"]["count"]
37
+ @follows_count = user["follows"]["count"]
38
+ @id = user["id"]
39
+ @post_count = user["media"]["count"]
40
+ @page_info = user["media"]["page_info"]
41
+ @profile_pic_url = user["profile_pic_url"]
42
+
43
+ media = user["media"]["nodes"]
44
+ if media
45
+ @posts = media.collect do |node|
46
+ IGScrape::Post.new(node)
47
+ end
48
+ end
49
+ end
50
+
51
+ def load_more_posts
52
+ cursor = @page_info["end_cursor"]
53
+
54
+ variables = URI.encode_www_form_component("{\"id\":\"#{@id}\",\"first\":12,\"after\":\"#{cursor}\"}")
55
+ url = "https://www.instagram.com/graphql/query/?query_id=17888483320059182&variables=#{variables}"
56
+
57
+ resp = HTTParty.get(url)
58
+ response = JSON.parse(resp.body)
59
+ timeline = response["data"]["user"]["edge_owner_to_timeline_media"]
60
+ @page_info = timeline["page_info"]
61
+ new_posts = timeline["edges"].collect do |edge|
62
+ IGScrape::Post.new(IGScrape::Post.edge_timeline_to_payload(edge["node"]))
63
+ end
64
+
65
+ @posts = @posts.concat(new_posts)
66
+ end
67
+
68
+ end
@@ -0,0 +1,19 @@
1
+ class IGScrape::Comment
2
+
3
+ attr_accessor :id, :text, :created_at, :author_id, :author_name, :author_profile_pic
4
+
5
+ def initialize(payload)
6
+ load_from_payload(payload)
7
+ end
8
+
9
+ private
10
+
11
+ def load_from_payload payload
12
+ @id = payload["id"]
13
+ @text = payload["text"]
14
+ @created_at = payload["created_at"]
15
+ @author_id = payload["owner"]["id"]
16
+ @author_profile_pic = payload["owner"]["profile_pic_url"]
17
+ @author_name = payload["owner"]["username"]
18
+ end
19
+ end
@@ -0,0 +1,81 @@
1
+ class IGScrape::Post
2
+
3
+ attr_accessor :id, :comment_count, :likes, :is_video, :code, :display_src, :caption, :created_at, :type, :comments
4
+
5
+ def initialize payload
6
+ @comments = []
7
+ load_from_payload(payload)
8
+ end
9
+
10
+ def self.load_from_shortcode code
11
+ url = "https://www.instagram.com/p/#{code}/?__a=1"
12
+ resp = HTTParty.get(url)
13
+ response = JSON.parse(resp.body)
14
+ payload = response["graphql"]["shortcode_media"]
15
+
16
+ post = IGScrape::Post.new(self.edge_timeline_to_payload(payload))
17
+ end
18
+
19
+ def has_more_comments?
20
+ @comments.length < @comment_count
21
+ end
22
+
23
+ def load_comments
24
+ while has_more_comments? do
25
+ load_more_comments
26
+ end
27
+ end
28
+
29
+ def self.edge_timeline_to_payload node
30
+ {
31
+ "id" => node["id"],
32
+ "__typename" => node["__typename"],
33
+ "is_video" => node["is_video"],
34
+ "code" => node["shortcode"],
35
+ "display_src" => node["display_url"],
36
+ "caption" => (node["edge_media_to_caption"]["edges"].length > 0 ? node["edge_media_to_caption"]["edges"].first["node"]["text"] : ""),
37
+ "date" => node["taken_at_timestamp"],
38
+ "comments" => node["edge_media_to_comment"],
39
+ "likes" => node["edge_media_preview_like"]
40
+ }
41
+ end
42
+
43
+ private
44
+
45
+ def load_more_comments
46
+ cursor = @comment_page_info["end_cursor"]
47
+ variables = URI.encode_www_form_component("{\"shortcode\":\"#{@code}\",\"first\":20,\"after\":\"#{cursor}\"}")
48
+
49
+ url = "https://www.instagram.com/graphql/query/?query_id=17852405266163336&variables=#{variables}"
50
+ resp = HTTParty.get(url)
51
+ response = JSON.parse(resp.body)
52
+
53
+ edges = response["data"]["shortcode_media"]["edge_media_to_comment"]["edges"]
54
+ new_comments = edges.collect do |edge|
55
+ IGScrape::Comment.new(edge["node"])
56
+ end
57
+
58
+ @comment_page_info = response["data"]["shortcode_media"]["edge_media_to_comment"]["page_info"]
59
+ @comments = @comments.concat(new_comments)
60
+ end
61
+
62
+ def load_from_payload payload
63
+ @id = payload["id"]
64
+ @is_video = payload["is_video"]
65
+ @type = payload["__typename"]
66
+ @caption = payload["caption"]
67
+ @created_at = payload["date"]
68
+ @display_src = payload["display_src"]
69
+ @code = payload["code"]
70
+ @likes = payload["likes"]["count"]
71
+ @comment_count = payload["comments"]["count"]
72
+ @comment_page_info = payload["comments"]["page_info"]
73
+
74
+ # load comments
75
+ if payload["comments"]["edges"]
76
+ @comments = payload["comments"]["edges"].collect do |edge|
77
+ IGScrape::Comment.new(edge["node"])
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,3 @@
1
+ module IGScrape
2
+ VERSION = "0.0.2"
3
+ end
data/lib/ig_scrape.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "ig_scrape/version"
2
+ require "ig_scrape/client"
3
+ require "ig_scrape/post"
4
+ require "ig_scrape/comment"
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ig_scrape
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Trevor Kimenye
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-reporters
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: httparty
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A gem to scrape instagram posts and comments.
112
+ email:
113
+ - kimenye@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - bin/setup
126
+ - ig_scrape.gemspec
127
+ - lib/ig_scrape.rb
128
+ - lib/ig_scrape/client.rb
129
+ - lib/ig_scrape/comment.rb
130
+ - lib/ig_scrape/post.rb
131
+ - lib/ig_scrape/version.rb
132
+ homepage: https://github.com/ongair/ig_scrape
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.6.11
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: A gem to scrape instagram posts and comments
156
+ test_files: []