instagram_crawler 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 +7 -0
- data/.gitignore +8 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +20 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/instagram_crawler.gemspec +34 -0
- data/lib/instagram_crawler.rb +147 -0
- data/lib/instagram_crawler/version.rb +3 -0
- metadata +81 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 102e0a0d0693b707f49b4392482feb4b72d32c5f
|
|
4
|
+
data.tar.gz: 209c7a644d8f0eefb0e951e1140f0ba1b024e9a9
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8be6c2cdfcc542a2997aa450df890f745cb040e9761fad4ecf0a452197bd62d0d6c8afbcf90bef6613d4db253823748b82cf1dc9388e1f1e978cab38588b1671
|
|
7
|
+
data.tar.gz: 159850335a25e6ee2e1ad2e5ee32d0c54f544e5f5156cf2269c4a1675d61d499817d5c0cc2bf9528288227eef68faf4bf2a1c7044564d5f406e9041d6e98e39e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
instagram_crawler (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
rake (10.4.2)
|
|
10
|
+
|
|
11
|
+
PLATFORMS
|
|
12
|
+
ruby
|
|
13
|
+
|
|
14
|
+
DEPENDENCIES
|
|
15
|
+
bundler (~> 1.16)
|
|
16
|
+
instagram_crawler!
|
|
17
|
+
rake (~> 10.0)
|
|
18
|
+
|
|
19
|
+
BUNDLED WITH
|
|
20
|
+
1.16.1
|
data/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# InstagramCrawler
|
|
2
|
+
|
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/instagram_crawler`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
4
|
+
|
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'instagram_crawler'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install instagram_crawler
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
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).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/instagram_crawler.
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "instagram_crawler"
|
|
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,34 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "instagram_crawler/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "instagram_crawler"
|
|
8
|
+
spec.version = InstagramCrawler::VERSION
|
|
9
|
+
spec.authors = ["plusquality"]
|
|
10
|
+
spec.email = ["plus.quolity@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{image download from instagram}
|
|
13
|
+
spec.description = %q{image download from instagram}
|
|
14
|
+
spec.homepage = "https://www.plusq.life/"
|
|
15
|
+
|
|
16
|
+
# # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
17
|
+
# # to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
18
|
+
# if spec.respond_to?(:metadata)
|
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
|
20
|
+
# else
|
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
|
22
|
+
# "public gem pushes."
|
|
23
|
+
# end
|
|
24
|
+
|
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
|
27
|
+
end
|
|
28
|
+
spec.bindir = "exe"
|
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
30
|
+
spec.require_paths = ["lib"]
|
|
31
|
+
|
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
34
|
+
end
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
require "instagram_crawler/version"
|
|
2
|
+
require 'logger'
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'net/http'
|
|
5
|
+
|
|
6
|
+
module InstagramCrawler
|
|
7
|
+
class Client
|
|
8
|
+
def initialize(hashtag)
|
|
9
|
+
@hashtag = hashtag
|
|
10
|
+
@crawler = GetHashTagUrl.new(@hashtag)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def get_image_urls(limit = 1000)
|
|
14
|
+
@crawler.download_hashtag(limit)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
module InstagramCrawler
|
|
20
|
+
class GetHashTagUrl
|
|
21
|
+
attr_accessor :images
|
|
22
|
+
def initialize(hashtag)
|
|
23
|
+
@logger = Logger.new(STDOUT)
|
|
24
|
+
@hashtag = hashtag
|
|
25
|
+
@images = []
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def info(message)
|
|
29
|
+
@logger.info(message)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def error(message)
|
|
33
|
+
@logger.error(message)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def download_hashtag(limit)
|
|
37
|
+
next_page = nil
|
|
38
|
+
number = 0
|
|
39
|
+
loop do
|
|
40
|
+
get_number, next_page = download_hashtag_page(@hashtag, next_page)
|
|
41
|
+
number += get_number
|
|
42
|
+
info "getting #{number} ##{@hashtag} images...."
|
|
43
|
+
break if next_page.nil? || number > limit
|
|
44
|
+
end
|
|
45
|
+
@images = @images.take(limit) if @images.count > limit
|
|
46
|
+
return @images
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def download_hashtag_page(hashtag, page)
|
|
50
|
+
json_result = download_per_page(hashtag, page)
|
|
51
|
+
return [0, nil] if json_result.nil?
|
|
52
|
+
hashtag_info = json_result['graphql']['hashtag']
|
|
53
|
+
size = 0
|
|
54
|
+
%w(media top_posts).each do |part|
|
|
55
|
+
next if part == 'top_posts' && page
|
|
56
|
+
edges = hashtag_info["edge_hashtag_to_#{part}"]['edges']
|
|
57
|
+
edges.each do |edge|
|
|
58
|
+
hashtag_format = format_edge(edge)
|
|
59
|
+
@images << hashtag_format
|
|
60
|
+
end
|
|
61
|
+
next if edges.empty?
|
|
62
|
+
size += edges.size
|
|
63
|
+
end
|
|
64
|
+
if hashtag_info['edge_hashtag_to_media']['page_info']['has_next_page']
|
|
65
|
+
[size, hashtag_info['edge_hashtag_to_media']['page_info']['end_cursor']]
|
|
66
|
+
else
|
|
67
|
+
[size, nil]
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# retuen result_hash
|
|
72
|
+
def format_edge(edge)
|
|
73
|
+
return if edge.nil?
|
|
74
|
+
{
|
|
75
|
+
id: edge["node"]["id"].to_i,
|
|
76
|
+
text: edge["node"]["edge_media_to_caption"]["edges"][0]["node"]["text"],
|
|
77
|
+
shortcode: edge["node"]["shortcode"],
|
|
78
|
+
dimensions: edge["node"]["dimensions"],
|
|
79
|
+
image_url: edge["node"]["display_url"],
|
|
80
|
+
owner: edge["node"]["owner"],
|
|
81
|
+
thumbnail_url: edge["node"]["thumbnail_src"],
|
|
82
|
+
hashtag: edge["node"]["edge_media_to_caption"]["edges"][0]["node"]["text"].scan(%r|\s?(#[^\s ]+)\s?|).flatten,
|
|
83
|
+
thumnail_images: edge["node"]["thumbnail_resources"]
|
|
84
|
+
}
|
|
85
|
+
rescue => e
|
|
86
|
+
return nil
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def download_per_page(hashtag, page, use_cookie = false)
|
|
90
|
+
download_uri = hashtag_page_url(hashtag, page)
|
|
91
|
+
return if download_uri.nil?
|
|
92
|
+
result = http_access(download_uri, use_cookie)
|
|
93
|
+
JSON.parse(result)
|
|
94
|
+
rescue JSON::ParserError => e
|
|
95
|
+
error "JSON parsing failed for URI: #{download_uri}, not retrying"
|
|
96
|
+
return nil
|
|
97
|
+
rescue => e
|
|
98
|
+
error "Download failed for URI: #{download_uri} retrying ..."
|
|
99
|
+
sleep 1
|
|
100
|
+
retry
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def http_access(uri, use_cookie = false, try = 3, first = true)
|
|
104
|
+
response = if use_cookie
|
|
105
|
+
http = Net::HTTP.new(uri.hostname, uri.port)
|
|
106
|
+
http.use_ssl = true
|
|
107
|
+
http.get(uri, 'Cookie' => false)
|
|
108
|
+
else
|
|
109
|
+
Net::HTTP.get_response uri
|
|
110
|
+
end
|
|
111
|
+
if response.code.to_i >= 399
|
|
112
|
+
if try > 0
|
|
113
|
+
warn "Downloading #{uri} ended in #{response.code} Retrying #{try} times."
|
|
114
|
+
if response.code == 429
|
|
115
|
+
sleep 5
|
|
116
|
+
return http_access(uri, use_cookie, 10, false) if first
|
|
117
|
+
else
|
|
118
|
+
sleep 1
|
|
119
|
+
end
|
|
120
|
+
http_access(uri, use_cookie, try -1 , first)
|
|
121
|
+
else
|
|
122
|
+
response.body
|
|
123
|
+
end
|
|
124
|
+
else
|
|
125
|
+
response.body
|
|
126
|
+
end
|
|
127
|
+
rescue => e
|
|
128
|
+
if try > 0
|
|
129
|
+
warn "Downlading #{uri} ended in #{e}. Retring #{try} times"
|
|
130
|
+
sleep 1
|
|
131
|
+
http_access(uri, use_cookie, try -1, first)
|
|
132
|
+
else
|
|
133
|
+
""
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def hashtag_page_url(hashtag, page)
|
|
138
|
+
if page
|
|
139
|
+
URI.parse URI.encode "https://www.instagram.com/explore/tags/#{hashtag}/?__a=1&max_id=#{page}"
|
|
140
|
+
else
|
|
141
|
+
URI.parse URI.encode "https://www.instagram.com/explore/tags/#{hashtag}/?__a=1"
|
|
142
|
+
end
|
|
143
|
+
rescue URI::InvalidURIError
|
|
144
|
+
error "Invalid hashtag #{hashtag} .."
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: instagram_crawler
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- plusquality
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-04-29 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.16'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.16'
|
|
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
|
+
description: image download from instagram
|
|
42
|
+
email:
|
|
43
|
+
- plus.quolity@gmail.com
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- ".gitignore"
|
|
49
|
+
- Gemfile
|
|
50
|
+
- Gemfile.lock
|
|
51
|
+
- README.md
|
|
52
|
+
- Rakefile
|
|
53
|
+
- bin/console
|
|
54
|
+
- bin/setup
|
|
55
|
+
- instagram_crawler.gemspec
|
|
56
|
+
- lib/instagram_crawler.rb
|
|
57
|
+
- lib/instagram_crawler/version.rb
|
|
58
|
+
homepage: https://www.plusq.life/
|
|
59
|
+
licenses: []
|
|
60
|
+
metadata: {}
|
|
61
|
+
post_install_message:
|
|
62
|
+
rdoc_options: []
|
|
63
|
+
require_paths:
|
|
64
|
+
- lib
|
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - ">="
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '0'
|
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
requirements: []
|
|
76
|
+
rubyforge_project:
|
|
77
|
+
rubygems_version: 2.5.2
|
|
78
|
+
signing_key:
|
|
79
|
+
specification_version: 4
|
|
80
|
+
summary: image download from instagram
|
|
81
|
+
test_files: []
|