reddit_junkie 0.0.2 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/reddit_junkie +36 -0
- data/lib/reddit_junkie.rb +141 -0
- metadata +7 -5
- 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: 7adfad2199b64b584f1193df16891b47bd490ab8d74e4f3b80e6293ef0f6e929
|
4
|
+
data.tar.gz: 2677f641ed84ac786f1f68cdec076871b9f81d0a5497aa0fb8f51ece94fbd401
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7b78140fa52e6a72b32c0e8d980226905309a57bae1341c54f5432ee4dc0a6bd6359ec64e640d305b092e032c46c5f760e9c35330987eabe89130454b88c3f0
|
7
|
+
data.tar.gz: 406dc20835cc575d00cebfc1bf81e9d4fc55f4ed61dc1658607e10265deef4f7d67dd635ff0e5522110ce6abbfa50e17ef6681e4ce7f7f323ec171ad872cc1ef
|
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.7
|
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-09
|
11
|
+
date: 2020-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -42,13 +42,15 @@ description: This little library helps people download images from different sub
|
|
42
42
|
much easier. It's actually like a crawler for the images posted on a subreddit.
|
43
43
|
Actually, it's a great tool to have your favorite memes locally!
|
44
44
|
email: haghiri75@gmail.com
|
45
|
-
executables:
|
45
|
+
executables:
|
46
|
+
- reddit_junkie
|
46
47
|
extensions: []
|
47
48
|
extra_rdoc_files: []
|
48
49
|
files:
|
49
50
|
- Gemfile
|
50
|
-
-
|
51
|
-
|
51
|
+
- bin/reddit_junkie
|
52
|
+
- lib/reddit_junkie.rb
|
53
|
+
homepage: http://git.capricelinux.ir/prp-e/reddit_image_downloader
|
52
54
|
licenses:
|
53
55
|
- MIT
|
54
56
|
metadata: {}
|
@@ -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
|