reddit_junkie 0.0.1 → 0.0.6
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 +4 -4
- data/bin/reddit_junkie +36 -0
- data/lib/reddit_junkie.rb +141 -0
- metadata +6 -18
- data/lib/reddit_image_downloader.rb +0 -73
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7247ea36a4057843ec6d72abc643a91d58a44bfd8ead7aa5e31503fdec47d567
|
4
|
+
data.tar.gz: a699a5b3d3dc8b2a5f839967e3bf1cf10b5390cc65e6dfb7a49b074234187f78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35eb9bf50881c274230872fae552e30c0dd38e34106cc556545468799fc956f05ae1cfac49392a60beb8b0ab30141316b6bdf7bd8feb1e31f2167308b446c819
|
7
|
+
data.tar.gz: 15a3cfb0381e4f240437c265976a360f979eab4fcfa7bd77643279dfb775a3cfcdcb8e224358022f9308425db7c5db1287a93c848810d6396cec3d765a90986d
|
data/bin/reddit_junkie
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require_relative '../lib/reddit_junkie.rb'
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
options = {}
|
7
|
+
|
8
|
+
OptionParser.new do |option|
|
9
|
+
option.on("--subreddit SUB")
|
10
|
+
option.on("--directory DIR")
|
11
|
+
option.on("--count COUNT")
|
12
|
+
option.on("--endpoint ENDPOINT")
|
13
|
+
end.parse!(into: options)
|
14
|
+
|
15
|
+
if options[:subreddit] && options[:count] && options[:directory] && options[:endpoint]
|
16
|
+
count = options[:count].to_i
|
17
|
+
r = RedditImage.new(options[:subreddit], count, options[:directory], options[:endpoint])
|
18
|
+
r.download_images
|
19
|
+
elsif options[:subreddit] && options[:count] && options[:directory]
|
20
|
+
count = options[:count].to_i
|
21
|
+
r = RedditImage.new(options[:subreddit], count, options[:directory])
|
22
|
+
r.download_images
|
23
|
+
elsif options[:subreddit] && options[:directory]
|
24
|
+
count = 25
|
25
|
+
r = RedditImage.new(options[:subreddit], count, options[:directory])
|
26
|
+
r.download_images
|
27
|
+
elsif options[:subreddit] && options[:count]
|
28
|
+
count = options[:count].to_i
|
29
|
+
r = RedditImage.new(options[:subreddit], count)
|
30
|
+
r.download_images
|
31
|
+
elsif options[:subreddit]
|
32
|
+
r = RedditImage.new(options[:subreddit])
|
33
|
+
r.download_images
|
34
|
+
end
|
35
|
+
|
36
|
+
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'httparty'
|
3
|
+
require 'digest'
|
4
|
+
|
5
|
+
class RedditImage
|
6
|
+
def initialize(sub, qty=25, directory="images", endpoint=nil, after_pointer=nil)
|
7
|
+
@sub = sub
|
8
|
+
@qty = qty
|
9
|
+
@directory = directory
|
10
|
+
@endpoint = endpoint
|
11
|
+
@after_pointer = after_pointer
|
12
|
+
@after_pointer_temporary = nil
|
13
|
+
@index = 0
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_info
|
17
|
+
puts "Going to crawl: "
|
18
|
+
puts "https://reddit.com/r/#{@sub}"
|
19
|
+
|
20
|
+
if @endpoint == nil
|
21
|
+
if @after_pointer == nil
|
22
|
+
response = HTTParty.get("https://reddit.com/r/#{@sub}.json?limit=#{@qty}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
23
|
+
else
|
24
|
+
response = HTTParty.get("https://reddit.com/r/#{@sub}.json?limit=#{@qty}&after=#{@after_pointer}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
25
|
+
end
|
26
|
+
else
|
27
|
+
if @after_pointer == nil
|
28
|
+
response = HTTParty.get("https://reddit.com/r/#{@sub}/#{@endpoint}.json?limit=#{@qty}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
29
|
+
else
|
30
|
+
response = HTTParty.get("https://reddit.com/r/#{@sub}/#{@endpoint}.json?limit=#{@qty}&after=#{@after_pointer}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
after_pointer = JSON.parse(response.body)
|
35
|
+
after_pointer = after_pointer['data']['after']
|
36
|
+
|
37
|
+
puts "The value of \"after\" pointer is: #{after_pointer}"
|
38
|
+
@after_pointer_temporary = after_pointer
|
39
|
+
end
|
40
|
+
|
41
|
+
def update_data
|
42
|
+
@after_pointer = @after_pointer_temporary
|
43
|
+
#@index = @index + 1
|
44
|
+
#@directory = @directory + @index.to_s
|
45
|
+
end
|
46
|
+
|
47
|
+
def download_images
|
48
|
+
if @endpoint == nil
|
49
|
+
if @after_pointer == nil
|
50
|
+
response = HTTParty.get("https://reddit.com/r/#{@sub}.json?limit=#{@qty}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
51
|
+
else
|
52
|
+
response = HTTParty.get("https://reddit.com/r/#{@sub}.json?limit=#{@qty}&after=#{@after_pointer}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
53
|
+
end
|
54
|
+
else
|
55
|
+
if @after_pointer == nil
|
56
|
+
response = HTTParty.get("https://reddit.com/r/#{@sub}/#{@endpoint}.json?limit=#{@qty}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
57
|
+
else
|
58
|
+
response = HTTParty.get("https://reddit.com/r/#{@sub}/#{@endpoint}.json?limit=#{@qty}&after=#{@after_pointer}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
json_response = JSON.parse(response.body)
|
63
|
+
data = json_response['data']['children']
|
64
|
+
|
65
|
+
if Dir::exists?"#{@directory}"
|
66
|
+
puts "The directory \"#{@directory}\" exists!"
|
67
|
+
Dir::chdir("#{@directory}")
|
68
|
+
else
|
69
|
+
Dir::mkdir("#{@directory}")
|
70
|
+
Dir::chdir("#{@directory}")
|
71
|
+
end
|
72
|
+
|
73
|
+
count_min = 0
|
74
|
+
#count_max = @qty % 100 ? @qty / 100 : "Sorry!"
|
75
|
+
|
76
|
+
if @qty % 100 == 0
|
77
|
+
count_max = @qty / 100
|
78
|
+
elsif @qty < 100
|
79
|
+
count_max = 1
|
80
|
+
else
|
81
|
+
puts "Sorry, not supported in this version"
|
82
|
+
end
|
83
|
+
|
84
|
+
while count_min < count_max
|
85
|
+
if @qty <= 100
|
86
|
+
links = []
|
87
|
+
data.each do |datum|
|
88
|
+
datum = datum['data']
|
89
|
+
if datum['post_hint'] == "image"
|
90
|
+
links << datum['url_overridden_by_dest']
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
file_name_base = 1
|
95
|
+
|
96
|
+
links.each do |link|
|
97
|
+
if link != nil
|
98
|
+
file_name = "#{@sub}-#{file_name_base}.jpg"
|
99
|
+
final_image = File.open(file_name, "wb")
|
100
|
+
final_image.write(HTTParty.get(link))
|
101
|
+
puts "Wrote on #{file_name}"
|
102
|
+
file_name_base += 1
|
103
|
+
sleep 0.5
|
104
|
+
final_image.close
|
105
|
+
end
|
106
|
+
end
|
107
|
+
else
|
108
|
+
if count_min > 0
|
109
|
+
self.get_info
|
110
|
+
self.update_data
|
111
|
+
end
|
112
|
+
links = []
|
113
|
+
data.each do |datum|
|
114
|
+
datum = datum['data']
|
115
|
+
if datum['post_hint'] == "image"
|
116
|
+
links << datum['url_overridden_by_dest']
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
file_name_base = count_min > 0 ? count_min * 100 : 1
|
121
|
+
|
122
|
+
links.each do |link|
|
123
|
+
if link != nil
|
124
|
+
file_name = "#{@sub}-#{file_name_base}.jpg"
|
125
|
+
final_image = File.open(file_name, "wb")
|
126
|
+
final_image.write(HTTParty.get(link))
|
127
|
+
puts "Wrote on #{file_name}"
|
128
|
+
file_name_base += 1
|
129
|
+
sleep 0.5
|
130
|
+
final_image.close
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
count_min += 1
|
135
|
+
end
|
136
|
+
|
137
|
+
Dir::chdir("..")
|
138
|
+
|
139
|
+
return links
|
140
|
+
end
|
141
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reddit_junkie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Muhammadreza Haghiri
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -24,20 +24,6 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: digest
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: json
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -56,12 +42,14 @@ description: This little library helps people download images from different sub
|
|
56
42
|
much easier. It's actually like a crawler for the images posted on a subreddit.
|
57
43
|
Actually, it's a great tool to have your favorite memes locally!
|
58
44
|
email: haghiri75@gmail.com
|
59
|
-
executables:
|
45
|
+
executables:
|
46
|
+
- reddit_junkie
|
60
47
|
extensions: []
|
61
48
|
extra_rdoc_files: []
|
62
49
|
files:
|
63
50
|
- Gemfile
|
64
|
-
-
|
51
|
+
- bin/reddit_junkie
|
52
|
+
- lib/reddit_junkie.rb
|
65
53
|
homepage: http://github.com/prp-e/reddit_image_downloader
|
66
54
|
licenses:
|
67
55
|
- MIT
|
@@ -1,73 +0,0 @@
|
|
1
|
-
require 'json'
|
2
|
-
require 'httparty'
|
3
|
-
require 'digest'
|
4
|
-
|
5
|
-
class RedditImage
|
6
|
-
def initialize(sub, qty=25, directory="images", endpoint=nil, after_pointer=nil)
|
7
|
-
@sub = sub
|
8
|
-
@qty = qty
|
9
|
-
@directory = directory
|
10
|
-
@endpoint = endpoint
|
11
|
-
@after_pointer = after_pointer
|
12
|
-
end
|
13
|
-
|
14
|
-
def get_info
|
15
|
-
puts "Going to crawl: "
|
16
|
-
puts "https://reddit.com/r/#{@sub}"
|
17
|
-
|
18
|
-
if @endpoint == nil
|
19
|
-
if @after_pointer == nil
|
20
|
-
response = HTTParty.get("https://reddit.com/r/#{@sub}.json?limit=#{@qty}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
21
|
-
else
|
22
|
-
response = HTTParty.get("https://reddit.com/r/#{@sub}.json?limit=#{@qty}&after=#{@after_pointer}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
23
|
-
end
|
24
|
-
else
|
25
|
-
if @after_pointer == nil
|
26
|
-
response = HTTParty.get("https://reddit.com/r/#{@sub}/#{@endpoint}.json?limit=#{@qty}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
27
|
-
else
|
28
|
-
response = HTTParty.get("https://reddit.com/r/#{@sub}/#{@endpoint}.json?limit=#{@qty}&after=#{@after_pointer}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
after_pointer = JSON.parse(response.body)
|
33
|
-
after_pointer = after_pointer['data']['after']
|
34
|
-
|
35
|
-
puts "The value of \"after\" pointer is: #{after_pointer}"
|
36
|
-
end
|
37
|
-
|
38
|
-
def download_images
|
39
|
-
if @endpoint == nil
|
40
|
-
response = HTTParty.get("https://reddit.com/r/#{@sub}.json?limit=#{@qty}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
41
|
-
else
|
42
|
-
response = HTTParty.get("https://reddit.com/r/#{@sub}/#{@endpoint}.json?limit=#{@qty}", headers: {"User-agent" => "Reddit Image Downloader 1.0"})
|
43
|
-
end
|
44
|
-
json_response = JSON.parse(response.body)
|
45
|
-
data = json_response['data']['children']
|
46
|
-
|
47
|
-
Dir::mkdir("#{@directory}")
|
48
|
-
Dir::chdir("#{@directory}")
|
49
|
-
|
50
|
-
links = []
|
51
|
-
data.each do |datum|
|
52
|
-
datum = datum['data']
|
53
|
-
if datum['post_hint'] == "image"
|
54
|
-
links << datum['url_overridden_by_dest']
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
links.each do |link|
|
59
|
-
if link != nil
|
60
|
-
file_name = "#{Digest::MD5.hexdigest(link)}"
|
61
|
-
final_image = File.open(file_name, "wb")
|
62
|
-
final_image.write(HTTParty.get(link))
|
63
|
-
puts "Wrote on #{file_name}"
|
64
|
-
sleep 0.5
|
65
|
-
final_image.close
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
Dir::chdir("..")
|
70
|
-
|
71
|
-
return links
|
72
|
-
end
|
73
|
-
end
|