rugram 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 78c6a973bd9e28193b5bd5222986e64db28495b84311f59b49d1ef167efa6534
4
+ data.tar.gz: 8ee6ff83f4dba6fdaa9bb25e1c1729ed7172b786c45cb460d1aa059118db260c
5
+ SHA512:
6
+ metadata.gz: 03feb7cf47dcdda9775759461d4d78c06f4f036103bb0baf796b7ca3c37b6060d634b947ca315dbefbf4d8db2a89b1b0e48d43d44590549e97d1edb156580172
7
+ data.tar.gz: dde5a295899a3eefc3ddb307ecb3567f23caa5d5d8c858868e2ad35a84cd11b272f592cf401c846521dff2341484aaf775c378805c33e1d4ecc678b75a99164d
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'httparty'
4
+ gem 'nokogiri'
5
+
6
+ group :development do
7
+ gem 'byebug'
8
+ gem 'rubocop'
9
+ end
10
+
11
+ group :test do
12
+ gem 'rspec'
13
+ gem 'dotenv'
14
+ end
@@ -0,0 +1,41 @@
1
+ require 'httparty'
2
+ require 'nokogiri'
3
+ require 'json'
4
+ require 'profile'
5
+ require 'user_not_found_error'
6
+ require 'byebug'
7
+
8
+ class Instagram
9
+ attr_reader :profile
10
+ def initialize(username)
11
+ @username = username
12
+ build_profile
13
+ end
14
+
15
+ def url
16
+ "https://instagram.com/#{@username}"
17
+ end
18
+
19
+ private
20
+
21
+ def request_profile_info
22
+ begin
23
+ request = HTTParty.get(url)
24
+ parsed_html = Nokogiri::HTML(request).xpath('/html/body/script[1]').text
25
+ json_data = JSON.parse(parsed_html[21, parsed_html.size-22])
26
+ @profile_information = json_data['entry_data']['ProfilePage'][0]['graphql']['user']
27
+ rescue
28
+ raise UserNotFoundError.new(@username)
29
+ end
30
+ end
31
+
32
+ def build_profile
33
+ request_profile_info
34
+ id = @profile_information['id']
35
+ name = @profile_information['full_name']
36
+ username = @profile_information['username']
37
+ profile_pic_url = @profile_information['profile_pic_url']
38
+ private_user = @profile_information['private_user']
39
+ @profile = Profile.new(id, name, username, profile_pic_url, private_user)
40
+ end
41
+ end
@@ -0,0 +1,63 @@
1
+ require 'httparty'
2
+ require 'nokogiri'
3
+ require 'json'
4
+ require 'byebug'
5
+
6
+ class Post
7
+ attr_reader :id, :profile, :caption, :taken_at, :display_url, :thumbnails
8
+ def initialize(id, profile, caption, taken_at, display_url, thumbnails)
9
+ @id = id
10
+ @profile = profile
11
+ @caption = caption
12
+ @taken_at = taken_at
13
+ @display_url = display_url
14
+ @thumbnails = thumbnails
15
+ end
16
+
17
+ def self.get_image(id)
18
+ @id = id
19
+ request = HTTParty.get(post_url)
20
+ parsed_html = Nokogiri::HTML(request).xpath("/html/body/script[1]").text
21
+ json_data = JSON.parse(parsed_html[21, parsed_html.size-22])
22
+ @post = json_data['entry_data']['PostPage'][0]['graphql']['shortcode_media']
23
+ fill_data
24
+ end
25
+
26
+ def post_url
27
+ "https://www.instagram.com/p/#{@id}"
28
+ end
29
+
30
+ def self.post_url
31
+ "https://www.instagram.com/p/#{@id}"
32
+ end
33
+
34
+ private
35
+ def self.fill_data
36
+ profile = create_profile
37
+ caption = @post['edge_media_to_caption']['edges'][0]['node']['text']
38
+ taken_at = @post['taken_at_timestamp']
39
+ Post.new(@id, profile, caption, taken_at, display_url, thumbnails)
40
+ end
41
+
42
+ def self.create_profile
43
+ owner = @post['owner']
44
+ id = owner['id']
45
+ name = owner['full_name']
46
+ username = owner['username']
47
+ profile_pic_url = owner['profile_pic_url']
48
+ private_user = owner['private_user']
49
+ Profile.new(id, name, username, profile_pic_url, private_user)
50
+ end
51
+
52
+ def self.display_url
53
+ @post['is_video'] ? @post['video_url'] : @post['display_url']
54
+ end
55
+
56
+ def self.thumbnails
57
+ urls = []
58
+ @post['display_resources'].each do |resource|
59
+ urls << resource['src']
60
+ end
61
+ urls
62
+ end
63
+ end
@@ -0,0 +1,14 @@
1
+ class Profile
2
+ attr_reader :id, :name, :username, :profile_pic_url
3
+ def initialize(id, name, username, profile_pic_url, private_user)
4
+ @id = id
5
+ @name = name
6
+ @username = username
7
+ @profile_pic_url = profile_pic_url
8
+ @private_user = private_user
9
+ end
10
+
11
+ def is_private?
12
+ @private_user
13
+ end
14
+ end
@@ -0,0 +1,66 @@
1
+ require 'httparty'
2
+ require 'byebug'
3
+ require 'story'
4
+
5
+ class Stories
6
+ def initialize(id, cookie)
7
+ @id = id
8
+ @cookie = cookie
9
+ end
10
+
11
+ def last_stories
12
+ request_stories
13
+ end
14
+
15
+ def highlight(id)
16
+ request_stories(id)
17
+ end
18
+
19
+ private
20
+
21
+ def request_stories(highlight = nil)
22
+ @query = {
23
+ query_hash: '45246d3fe16ccc6577e0bd297a5db1ab',
24
+ variables: {
25
+ reel_ids:[@id],
26
+ tag_names:[],
27
+ location_ids:[],
28
+ highlight_reel_ids:[],
29
+ precomposed_overlay:false
30
+ }.to_json
31
+ }
32
+
33
+ @headers = {
34
+ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0',
35
+ 'Cookie' => @cookie,
36
+ 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
37
+ }
38
+ #@query[:variables][:highlight_reel_ids] << highlight
39
+
40
+ url = 'https://www.instagram.com/graphql/query'
41
+ request = HTTParty.get(
42
+ url,
43
+ query: @query,
44
+ headers: @headers
45
+ )
46
+
47
+ @parsed_stories = JSON.parse(request.body)
48
+ build_stories
49
+ end
50
+
51
+ def build_stories
52
+ story_items = @parsed_stories['data']['reels_media'][0]['items']
53
+ stories = Array.new
54
+ if story_items
55
+ story_items.each do |story|
56
+ id = story['id']
57
+ taken_at = story['taken_at']
58
+ stories << Story.new(id, taken_at, display_url(story))
59
+ end
60
+ end
61
+ end
62
+
63
+ def display_url(story)
64
+ story['is_video'] ? story['video_resources'].last()['src'] : story['display_url']
65
+ end
66
+ end
@@ -0,0 +1,7 @@
1
+ class Story
2
+ def initialize(id, taken_at, display_url)
3
+ @id = id
4
+ @taken_at = taken_at
5
+ @display_url = display_url
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ class UserNotFoundError < StandardError
2
+ def initialize(user)
3
+ msg = "Instagram User Not Found: #{user}"
4
+ super(msg)
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rugram
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Tripoloni
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: bruno.tripoloni@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - Gemfile
20
+ - lib/instagram.rb
21
+ - lib/post.rb
22
+ - lib/profile.rb
23
+ - lib/stories.rb
24
+ - lib/story.rb
25
+ - lib/user_not_found_error.rb
26
+ homepage:
27
+ licenses: []
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.7.6
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Rugran is a gem to scrap instagram data
49
+ test_files: []