ig_scrape 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a2d0a888ba09af0188257b8924cbaa83f85b15b0
4
- data.tar.gz: 3d17f54b10d168c4aedc8eeadb40c280ffbb3fbc
3
+ metadata.gz: 9027f0e5b57aeefba527eceb037a323e2d6e90bd
4
+ data.tar.gz: f34bf0399ef36ea67bbee37cea822dd540cc650c
5
5
  SHA512:
6
- metadata.gz: 74804b533c38dfd21e4b49aa63380ce652f91700873f2a4916c6a3e591194d5027aa56520d9b7194c756be5016650e70234084537e239a1d76a166eb0c5f7cf3
7
- data.tar.gz: 624898bb60e1a872e409d843947e214cb0c2691cc8fb6f1d3408d883dcdf9f5c61d86c2faf87182b9120e6cefc170b15442e6d60afa8b76fc485aef177795593
6
+ metadata.gz: 07f3eb3e145d9c832f641ef3848002146e4904d8f9dd900822cdb4346f9e8799a1fdb86e81d67be9a3609dd5a9c83f45f45cc3e45fbd74bfd028905ef261bc05
7
+ data.tar.gz: 599447632f4ef2bd9d769960e51b9ab5a86e433f5aaba69e70849b271e3df718f6222d358f096cd7ca4099b0648ecfe367fc47a08cc2cbdb1cea446269823ab5
data/README.md CHANGED
@@ -54,6 +54,22 @@ Or install it yourself as:
54
54
  puts post.has_more_comments?
55
55
  ```
56
56
 
57
+ ### Using the CLI
58
+
59
+ You can use the CLI to get a dump in JSON of posts and comments
60
+
61
+ ```
62
+ gem install ig_scrape
63
+
64
+ ig_scrape help
65
+
66
+ # load all the posts for an account
67
+ ig_scrape posts --username theusername
68
+
69
+ # load all the comments for a post
70
+ ig_scrape comments --shortcode theshortcode
71
+ ```
72
+
57
73
  ## Development
58
74
 
59
75
  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.
data/bin/ig_scrape ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ig_scrape'
4
+ require 'thor'
5
+ require 'json'
6
+
7
+ class IGScrape::CLI < Thor
8
+
9
+ desc "posts username", "Get all the posts for the username"
10
+ option :username, :required => true
11
+ def posts
12
+ begin
13
+ client = IGScrape::Client.new(options[:username])
14
+ client.load
15
+ posts = client.posts
16
+ puts JSON.pretty_generate(posts)
17
+ rescue ArgumentError => e
18
+ puts e.message
19
+ end
20
+ end
21
+
22
+ desc "comments shortcode", "Get all the comments for a post's shortcode"
23
+ option :shortcode, :required => true
24
+ def comments
25
+ begin
26
+ post = IGScrape::Post.load_from_shortcode(options[:shortcode])
27
+ post.load_comments
28
+
29
+ comments = post.comments
30
+ puts JSON.pretty_generate(comments)
31
+ rescue ArgumentError => e
32
+ puts e.message
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ IGScrape::CLI.start(ARGV)
data/ig_scrape.gemspec CHANGED
@@ -17,8 +17,8 @@ Gem::Specification.new do |spec|
17
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
18
  f.match(%r{^(test|spec|features)/})
19
19
  end
20
- spec.bindir = "exe"
21
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.bindir = "bin"
21
+ spec.executables = 'ig_scrape'
22
22
  spec.require_paths = ["lib"]
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.15"
@@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency "webmock"
29
29
  spec.add_development_dependency "minitest-reporters"
30
30
  spec.add_dependency "httparty"
31
+ spec.add_dependency "thor"
31
32
  end
@@ -30,21 +30,27 @@ class IGScrape::Client
30
30
  url = "https://www.instagram.com/#{@username}/?__a=1"
31
31
  resp = HTTParty.get(url)
32
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"]
33
+ case resp.code
34
+ when 200
35
+ response = JSON.parse(resp.body)
36
+ user = response["user"]
37
+ @full_name = user["full_name"]
38
+ @follower_count = user["followed_by"]["count"]
39
+ @follows_count = user["follows"]["count"]
40
+ @id = user["id"]
41
+ @post_count = user["media"]["count"]
42
+ @page_info = user["media"]["page_info"]
43
+ @profile_pic_url = user["profile_pic_url"]
42
44
 
43
- media = user["media"]["nodes"]
44
- if media
45
- @posts = media.collect do |node|
46
- IGScrape::Post.new(node)
47
- end
45
+ media = user["media"]["nodes"]
46
+ if media
47
+ @posts = media.collect do |node|
48
+ IGScrape::Post.new(node)
49
+ end
50
+ end
51
+ when 404
52
+ # the client does not exist
53
+ raise ArgumentError.new("#{@username} does not exist!")
48
54
  end
49
55
  end
50
56
 
@@ -6,6 +6,17 @@ class IGScrape::Comment
6
6
  load_from_payload(payload)
7
7
  end
8
8
 
9
+ def to_json(*args)
10
+ JSON.generate({
11
+ id: @id,
12
+ text: @text,
13
+ created_at: @created_at,
14
+ author_id: @author_id,
15
+ author_name: @author_name,
16
+ author_profile_pic: @author_profile_pic
17
+ })
18
+ end
19
+
9
20
  private
10
21
 
11
22
  def load_from_payload payload
@@ -1,3 +1,4 @@
1
+ require 'json'
1
2
  class IGScrape::Post
2
3
 
3
4
  attr_accessor :id, :comment_count, :likes, :is_video, :code, :display_src, :caption, :created_at, :type, :comments
@@ -10,10 +11,16 @@ class IGScrape::Post
10
11
  def self.load_from_shortcode code
11
12
  url = "https://www.instagram.com/p/#{code}/?__a=1"
12
13
  resp = HTTParty.get(url)
13
- response = JSON.parse(resp.body)
14
- payload = response["graphql"]["shortcode_media"]
15
14
 
16
- post = IGScrape::Post.new(self.edge_timeline_to_payload(payload))
15
+ case resp.code
16
+ when 200
17
+ response = JSON.parse(resp.body)
18
+ payload = response["graphql"]["shortcode_media"]
19
+
20
+ post = IGScrape::Post.new(self.edge_timeline_to_payload(payload))
21
+ when 404
22
+ raise ArgumentError.new("Post with #{code} does not exist!")
23
+ end
17
24
  end
18
25
 
19
26
  def has_more_comments?
@@ -40,6 +47,18 @@ class IGScrape::Post
40
47
  }
41
48
  end
42
49
 
50
+ def to_json(*args)
51
+ JSON.generate({
52
+ id: @id,
53
+ code: @code,
54
+ caption: @caption,
55
+ type: @type,
56
+ created_at: @created_at,
57
+ comment_count: @comment_count,
58
+ likes: @likes
59
+ })
60
+ end
61
+
43
62
  private
44
63
 
45
64
  def load_more_comments
@@ -1,3 +1,3 @@
1
1
  module IGScrape
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ig_scrape
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trevor Kimenye
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-25 00:00:00.000000000 Z
11
+ date: 2017-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,10 +108,25 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: thor
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  description: A gem to scrape instagram posts and comments.
112
126
  email:
113
127
  - kimenye@gmail.com
114
- executables: []
128
+ executables:
129
+ - ig_scrape
115
130
  extensions: []
116
131
  extra_rdoc_files: []
117
132
  files:
@@ -122,6 +137,7 @@ files:
122
137
  - README.md
123
138
  - Rakefile
124
139
  - bin/console
140
+ - bin/ig_scrape
125
141
  - bin/setup
126
142
  - ig_scrape.gemspec
127
143
  - lib/ig_scrape.rb