social_miner 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dceb80b76ae8ca6044e919608765d26e0e76508672b9a2bfe5e3179d05a0a040
4
+ data.tar.gz: 32da50a190e4ac98d30f88d5710ad2883a3bcf3eedc1a66d8e3e2bb96d20bcfa
5
+ SHA512:
6
+ metadata.gz: 931ed6f7546d1a477489b25d378c6b7c444ed6087c1323b2ad4c8fb27d47aca6d03d8ec0a287fc37e529c991c56c579c05c43b1efb7ee23cc0270f2c6c6bea84
7
+ data.tar.gz: 7c063258a95a5e6c0f3339652d1823a5521f3b24f6e959eba5afaaf6e88d5e14f678dcf5af5d110db857b91bde991f13fa96964617828624c0037422f777cd5a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 3.2
4
+
5
+ Style/StringLiterals:
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ EnforcedStyle: double_quotes
10
+
11
+ Style/Documentation:
12
+ Enabled: false
13
+
14
+ Metrics/BlockLength:
15
+ Max: 70
16
+
17
+ Metrics/MethodLength:
18
+ Max: 30
19
+
20
+ Metrics/AbcSize:
21
+ Max: 30
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2024-10-31
2
+
3
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Null0vector
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,216 @@
1
+ # SocialMiner
2
+
3
+ SocialMiner is a Ruby gem that provides a simple interface to fetch public data from Instagram, including user profiles, posts, and comments.
4
+
5
+ # Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'social_miner'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ bundle install
17
+ ```
18
+
19
+ # Usage
20
+ Fetch user profile info:
21
+
22
+ ```ruby
23
+ mapper = SocialMiner::Instagram::ProfileMapper
24
+
25
+ result = SocialMiner::Instagram.profile_info(username: "instagram") do |profile_attrs|
26
+ mapper.map(profile_attrs)
27
+ end
28
+ # Result contains:
29
+ # {
30
+ # social_id: "123456789",
31
+ # username: "instagram_username",
32
+ # description: "User bio",
33
+ # first_name: "John",
34
+ # last_name: "Doe",
35
+ # avatar_url: "https://...",
36
+ # followers: 1000,
37
+ # following: 500
38
+ # }
39
+ ```
40
+
41
+ To fetch a user's posts:
42
+
43
+ ```ruby
44
+ mapper = SocialMiner::Instagram::PostMapper
45
+ # User ID can be obtained from the profile info (social_id field)
46
+ result, cursor = SocialMiner::Instagram.profile_posts(user_id: "user_id") do |posts_attrs, cursor|
47
+ [
48
+ posts_attrs.map { |post_attrs| mapper.map(post_attrs) },
49
+ cursor
50
+ ]
51
+ end
52
+
53
+ # Result contains:
54
+ # {
55
+ # cursor: "next_page_token",
56
+ # records: [
57
+ # {
58
+ # social_id: "post123",
59
+ # image_url: "https://...",
60
+ # shortcode: "ABC123",
61
+ # location_name: "New York",
62
+ # description: "Post caption",
63
+ # published_at: 2024-03-20 12:00:00
64
+ # }
65
+ # ]
66
+ # }
67
+
68
+ # To fetch next page:
69
+ next_page = SocialMiner::Instagram.profile_posts(user_id: "user_id", cursor: result[:cursor]) do |posts_attrs, _|
70
+ ...
71
+ end
72
+ ```
73
+
74
+ To fetch comments on a specific post:
75
+
76
+ ```ruby
77
+ mapper = SocialMiner::Instagram::CommentMapper
78
+
79
+ result, cursor = SocialMiner::Instagram.post_comments(post_shortcode: "ABC123") do |comments_attrs, cursor|
80
+ [
81
+ comments_attrs.map { |comment_attrs| mapper.map(comment_attrs) },
82
+ cursor
83
+ ]
84
+ end
85
+
86
+ # Result contains:
87
+ # {
88
+ # cursor: "next_page_token",
89
+ # records: [
90
+ # {
91
+ # social_id: "comment123",
92
+ # body: "Great post!",
93
+ # published_at: 2024-03-20 12:00:00
94
+ # }
95
+ # ]
96
+ # }
97
+
98
+ # To fetch next page of comments:
99
+ next_page = SocialMiner::Instagram.post_comments(post_shortcode: "ABC123", cursor: result[:cursor]) do |comments_attrs, _|
100
+ ...
101
+ end
102
+ ```
103
+
104
+ # Custom Headers
105
+
106
+ You can pass custom headers to the requests by passing a hash to the `request_headers` option:
107
+
108
+ ```ruby
109
+ SocialMiner::Instagram.profile_info({ "Custom-Header" => "Header-Value" }, username: 'instagram')
110
+ ```
111
+
112
+ # Custom Mapper
113
+
114
+ You can use a custom mapper to transform the data into a custom structure.
115
+
116
+ ```ruby
117
+ module CustomMapper
118
+ def map(attrs)
119
+ { id: attrs['id'] }
120
+ end
121
+
122
+ module_function :map
123
+ end
124
+ ```
125
+
126
+ Use your custom mapper:
127
+
128
+ ```ruby
129
+ SocialMiner::Instagram.profile_info(username: "instagram") do |profile_attrs|
130
+ CustomMapper.map(profile_attrs)
131
+ end
132
+ ```
133
+
134
+ # Fetch all posts / comments
135
+
136
+ ```ruby
137
+ class BackgroundJob
138
+ @@mapper = SocialMiner::Instagram::PostMapper
139
+
140
+ def perform(user_id, cursor: nil)
141
+ posts, cursor = SocialMiner::Instagram.profile_posts(user_id: user_id, cursor: cursor) do |posts_attrs, cursor|
142
+ [
143
+ posts_attrs.map { |post_attrs| @@mapper.map(post_attrs) },
144
+ cursor
145
+ ]
146
+ end
147
+
148
+ # Bulk import to DB
149
+ Post.import(posts)
150
+
151
+ perform_async(user_id, cursor) unless cursor.nil?
152
+ end
153
+ end
154
+ ```
155
+
156
+ ### Default Mappers
157
+
158
+ The gem includes several default mappers:
159
+
160
+ - `ProfileMapper`: Maps user profile information
161
+ ```ruby
162
+ {
163
+ social_id: String, # Instagram user ID
164
+ username: String, # Instagram username
165
+ description: String, # User bio
166
+ full_name: String, # Full name
167
+ avatar_url: String, # Profile picture URL
168
+ followers: Integer, # Followers count
169
+ following: Integer # Following count
170
+ }
171
+ ```
172
+
173
+ - `PostMapper`: Maps post information
174
+ ```ruby
175
+ {
176
+ social_id: String, # Instagram post ID
177
+ image_url: String, # Post image URL
178
+ shortcode: String, # Post shortcode
179
+ location_name: String, # Location name (optional)
180
+ description: String, # Post description
181
+ published_at: Time # Post creation timestamp
182
+ }
183
+ ```
184
+
185
+ - `CommentMapper`: Maps comment information
186
+ ```ruby
187
+ {
188
+ social_id: String, # Comment ID
189
+ body: String, # Comment text
190
+ published_at: Time # Comment timestamp
191
+ }
192
+ ```
193
+
194
+ # Requirements
195
+
196
+ - Ruby 3.2+
197
+
198
+ # License
199
+
200
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
201
+
202
+ ⚠️ **DISCLAIMER**
203
+
204
+ This gem is provided for educational purposes only. Please note:
205
+
206
+ 1. This tool is not officially associated with Instagram or Meta Platforms, Inc.
207
+ 2. Using this gem might violate Instagram's Terms of Service.
208
+ 3. The author(s) of this gem:
209
+ - Take no responsibility for how you use this software
210
+ - Are not liable for any damages resulting from its use
211
+ - Are not responsible for any legal consequences that may arise from its use
212
+
213
+ By using this gem, you acknowledge that:
214
+ - You are solely responsible for complying with all applicable laws and terms of service
215
+ - You will use this tool responsibly and at your own risk
216
+ - You understand that Instagram's API changes may break this gem's functionality at any time
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SocialMiner
4
+ module Helpers
5
+ module Hash
6
+ def fetch_nested(hash, *keys)
7
+ val = hash.fetch(keys.shift)
8
+ return val if keys.empty?
9
+
10
+ fetch_nested(val, *keys)
11
+ end
12
+
13
+ module_function :fetch_nested
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ require "uri"
6
+ require "net/http"
7
+
8
+ module SocialMiner
9
+ module Instagram
10
+ class Base
11
+ SOURCE_URL = "https://www.instagram.com"
12
+
13
+ API_URL = "#{SOURCE_URL}/api/v1".freeze
14
+ GRAPHQL_URL = "#{SOURCE_URL}/graphql/query/".freeze
15
+
16
+ DEFAULT_HEADERS = {
17
+ "X-IG-App-ID" => "936619743392459"
18
+ }.freeze
19
+
20
+ attr_reader :request_headers
21
+
22
+ def initialize(request_headers = {})
23
+ @request_headers = DEFAULT_HEADERS.merge(request_headers)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SocialMiner
4
+ module Instagram
5
+ module CommentMapper
6
+ def map(attrs)
7
+ published_at = Time.at(attrs["created_at"]) if attrs["created_at"]
8
+
9
+ {
10
+ social_id: attrs.fetch("id"),
11
+ body: attrs.fetch("text"),
12
+ published_at: published_at
13
+ }
14
+ end
15
+
16
+ module_function :map
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SocialMiner
4
+ module Instagram
5
+ class PostComments < Base
6
+ DOC_ID = "8845758582119845"
7
+
8
+ def call(post_shortcode:, cursor: nil)
9
+ variables = { shortcode: post_shortcode }
10
+ variables[:after] = cursor if cursor
11
+
12
+ data = {
13
+ "variables" => { shortcode: post_shortcode }.to_json,
14
+ "doc_id" => DOC_ID
15
+ }
16
+
17
+ response = Net::HTTP.post_form(URI(GRAPHQL_URL), data)
18
+
19
+ case response
20
+ when Net::HTTPSuccess
21
+ response_data = JSON.parse(response.body)
22
+
23
+ edge_media = Helpers::Hash.fetch_nested(response_data,
24
+ "data",
25
+ "xdt_shortcode_media",
26
+ "edge_media_to_parent_comment")
27
+
28
+ cursor = Helpers::Hash.fetch_nested(edge_media, "page_info", "end_cursor")
29
+ .then { |data| JSON.parse(data) }
30
+ .then { |data| data.fetch("server_cursor") }
31
+
32
+ records = edge_media.fetch("edges").map { |edge| edge.fetch("node") }
33
+
34
+ if block_given?
35
+ yield(records, cursor)
36
+ else
37
+ { records: records, cursor: cursor }
38
+ end
39
+ else
40
+ raise Net::HTTPError.new("Request Failed #{response.code}", response)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SocialMiner
4
+ module Instagram
5
+ module PostMapper
6
+ def map(attrs)
7
+ published_at = Time.at(attrs["taken_at_timestamp"]) if attrs["taken_at_timestamp"]
8
+
9
+ {
10
+ social_id: attrs.fetch("id"),
11
+ image_url: attrs.fetch("display_url"),
12
+ shortcode: attrs.fetch("shortcode"),
13
+ location_name: attrs.dig("location", "name"),
14
+ description: description(attrs.dig("edge_media_to_caption", "edges")),
15
+ published_at: published_at
16
+ }
17
+ end
18
+
19
+ module_function :map
20
+
21
+ def description(edges)
22
+ edge = edges&.first
23
+ edge&.dig("node", "text")
24
+ end
25
+
26
+ module_function :description
27
+ private_class_method :description
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SocialMiner
4
+ module Instagram
5
+ class ProfileInfo < Base
6
+ URL = "#{API_URL}/users/web_profile_info/".freeze
7
+
8
+ def call(username:)
9
+ uri = URI(URL)
10
+ uri.query = URI.encode_www_form([
11
+ [:username, username]
12
+ ])
13
+
14
+ response = Net::HTTP.get_response(uri, request_headers)
15
+
16
+ case response
17
+ when Net::HTTPSuccess
18
+ response_data = JSON.parse(response.body)
19
+ record = Helpers::Hash.fetch_nested(response_data, "data", "user")
20
+
21
+ if block_given?
22
+ yield(record)
23
+ else
24
+ record
25
+ end
26
+ else
27
+ raise Net::HTTPError.new("Request Failed #{response.code}", response)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SocialMiner
4
+ module Instagram
5
+ module ProfileMapper
6
+ def map(attrs)
7
+ {
8
+ social_id: attrs.fetch("id"),
9
+ description: attrs["biography"],
10
+ username: attrs.fetch("username"),
11
+ full_name: attrs.fetch("full_name"),
12
+ avatar_url: attrs.fetch("profile_pic_url"),
13
+ followers: Helpers::Hash.fetch_nested(attrs, "edge_followed_by", "count"),
14
+ following: Helpers::Hash.fetch_nested(attrs, "edge_follow", "count")
15
+ }
16
+ end
17
+
18
+ module_function :map
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SocialMiner
4
+ module Instagram
5
+ class ProfilePosts < Base
6
+ PER_PAGE = 12
7
+ QUERY_HASH = "69cba40317214236af40e7efa697781d"
8
+
9
+ def call(user_id:, cursor: nil)
10
+ uri = URI(GRAPHQL_URL)
11
+
12
+ variables = { id: user_id, first: PER_PAGE }
13
+ variables[:after] = cursor if cursor
14
+
15
+ uri.query = URI.encode_www_form([
16
+ [:query_hash, QUERY_HASH],
17
+ [:variables, variables.to_json]
18
+ ])
19
+
20
+ response = Net::HTTP.get_response(uri, request_headers)
21
+
22
+ case response
23
+ when Net::HTTPSuccess
24
+ response_data = JSON.parse(response.body)
25
+ timeline_media = Helpers::Hash.fetch_nested(response_data,
26
+ "data",
27
+ "user",
28
+ "edge_owner_to_timeline_media")
29
+
30
+ cursor = Helpers::Hash.fetch_nested(timeline_media, "page_info", "end_cursor")
31
+ records = timeline_media.fetch("edges").map { |attrs| attrs.fetch("node") }
32
+
33
+ if block_given?
34
+ yield(records, cursor)
35
+ else
36
+ { records: records, cursor: cursor }
37
+ end
38
+ else
39
+ raise Net::HTTPError.new("Request Failed #{response.code}", response)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SocialMiner
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "social_miner/version"
4
+
5
+ require_relative "social_miner/helpers/hash"
6
+
7
+ require_relative "social_miner/instagram/base"
8
+ require_relative "social_miner/instagram/profile_info"
9
+ require_relative "social_miner/instagram/profile_mapper"
10
+ require_relative "social_miner/instagram/profile_posts"
11
+ require_relative "social_miner/instagram/post_mapper"
12
+ require_relative "social_miner/instagram/post_comments"
13
+ require_relative "social_miner/instagram/comment_mapper"
14
+
15
+ module SocialMiner
16
+ def Instagram.profile_info(request_headers = {}, **args, &)
17
+ Instagram::ProfileInfo.new(request_headers).call(**args, &)
18
+ end
19
+
20
+ def Instagram.profile_posts(request_headers = {}, **args, &)
21
+ Instagram::ProfilePosts.new(request_headers).call(**args, &)
22
+ end
23
+
24
+ def Instagram.post_comments(request_headers = {}, **args, &)
25
+ Instagram::PostComments.new(request_headers).call(**args, &)
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: social_miner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - null0vector
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-11-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - root@null0vector.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/social_miner.rb
27
+ - lib/social_miner/helpers/hash.rb
28
+ - lib/social_miner/instagram/base.rb
29
+ - lib/social_miner/instagram/comment_mapper.rb
30
+ - lib/social_miner/instagram/post_comments.rb
31
+ - lib/social_miner/instagram/post_mapper.rb
32
+ - lib/social_miner/instagram/profile_info.rb
33
+ - lib/social_miner/instagram/profile_mapper.rb
34
+ - lib/social_miner/instagram/profile_posts.rb
35
+ - lib/social_miner/version.rb
36
+ homepage:
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ source_code_uri: https://github.com/null0vector/social_miner
41
+ changelog_uri: https://github.com/null0vector/social_miner/blob/main/CHANGELOG.md
42
+ rubygems_mfa_required: 'true'
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 3.2.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.5.16
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: SocialMiner is a Ruby gem that provides a simple interface to fetch public
62
+ data from Instagram, including user profiles, posts, and comments.
63
+ test_files: []