fourchanify 0.0.2

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
+ SHA1:
3
+ metadata.gz: 0dbd54a99408bec704073990feb2b1a0d2b0033e
4
+ data.tar.gz: ba96504b0bd1a9a38a31d5bc50a51c303763d5ce
5
+ SHA512:
6
+ metadata.gz: bf493b835c85655089f4ef14035c8619f9c4598f9df1fc48dc74d3c888019c791e133416451bdaf1e132d9334279102f0f695c2df1fe9d5d1d6293ab5d426cdd
7
+ data.tar.gz: 3f6ae5ae475532c222619698558eff03fb629ae2350985b7605c1678124b12add7dda5284610fe1f754c1044303d3f6992e4e515dc272fb9a1b8f1a6f30c9ae2
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fourchanify.gemspec
4
+ gemspec
5
+
6
+ gem "fourchan-urler"
7
+ gem "json"
8
+
9
+ group :development, :test do
10
+ gem "debugger", "~> 1.6.2"
11
+ gem "rspec", "~> 2.14.1"
12
+ gem "factory_girl", "~> 4.3.0"
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jason Kim
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Fourchanify
2
+
3
+ Fourchanify lets you download images for a 4chan thread from your terminal.
4
+
5
+ ## Installation
6
+
7
+ Fourchanify works with Ruby 1.9.3 and Ruby 2.0.0.
8
+
9
+ 1. Open terminal.
10
+ 2. Run this command.
11
+
12
+ $ gem install fourchanify
13
+
14
+ ## Usage
15
+
16
+ 1. Install the gem.
17
+ 2. Navigate to the directory where you want to download the images.
18
+ 3. Run this command.
19
+
20
+ ```
21
+ $ fourchanify [4chan_url]
22
+ as an example: fourchanify http://boards.4chan.org/mu/res/32745718
23
+ ```
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/fourchanify ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fourchanify'
4
+
5
+ if ARGV[0]
6
+ Fourchanify::Request.download(ARGV[0])
7
+ else
8
+ puts "Usage:"
9
+ puts " $ fourchanify [4chan_url]"
10
+ puts " example of 4chan_url: http://boards.4chan.org/mu/res/32745718"
11
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fourchanify/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fourchanify"
8
+ spec.version = Fourchanify::VERSION
9
+ spec.authors = ["Jason Kim"]
10
+ spec.email = ["active_red@hotmail.com"]
11
+ spec.description = %q{Download images from 4chan to your computer given a thread url}
12
+ spec.summary = %q{Download images from 4chan to your computer}
13
+ spec.homepage = "https://github.com/serv/fourchanify"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+ spec.executables = ["fourchanify"]
21
+
22
+ spec.add_dependency "fourchan-urler"
23
+ spec.add_dependency "json"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "factory_girl"
29
+ end
@@ -0,0 +1,37 @@
1
+ module Fourchanify
2
+ class Image
3
+ attr_accessor :filename, :ext, :fsize, :tim, :board, :url, :directory_name
4
+
5
+ def self.prepare(attributes)
6
+ image = Image.new
7
+ image.filename = attributes[:filename]
8
+ image.ext = attributes[:ext]
9
+ image.fsize = attributes[:fsize]
10
+ image.tim = attributes[:tim]
11
+ image.board = attributes[:board]
12
+ image.set_url
13
+ image
14
+ end
15
+
16
+ def set_url
17
+ self.url = self.get_url
18
+ end
19
+
20
+ def get_url
21
+ "http://images.4chan.org/#{self.board}/src/#{self.tim}#{self.ext}"
22
+ end
23
+
24
+ def download(directory_name)
25
+ self.directory_name = directory_name
26
+ self.save
27
+ print "."
28
+ end
29
+
30
+ def save
31
+ open("#{self.directory_name}/#{self.tim}#{self.ext}", 'wb') do |file|
32
+ file << open(self.url).read
33
+ end
34
+ sleep 1
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,79 @@
1
+ module Fourchanify
2
+ class Request
3
+ attr_accessor :url, :board, :thread_id, :image_count, :images
4
+
5
+ def self.prepare(url)
6
+ fourchan_url = FourchanUrler::Request.new(url)
7
+
8
+ request = Request.new
9
+ request.url = url
10
+ request.board = fourchan_url.board
11
+ request.thread_id = fourchan_url.thread_id
12
+ request.prepare_images
13
+ request
14
+ end
15
+
16
+ def self.get_posts(board, thread_id)
17
+ sleep 1
18
+ api_url = "http://api.4chan.org/#{board}/res/#{thread_id}.json"
19
+ begin
20
+ json = JSON.parse(open(api_url).read)
21
+ json["posts"]
22
+ rescue
23
+ raise JSONError, "JSON processing failed. Please try again"
24
+ end
25
+ end
26
+
27
+ def self.get_first_thread_no(board)
28
+ sleep 1
29
+ api_url = "http://api.4chan.org/#{board}/threads.json"
30
+ begin
31
+ json = JSON.parse(open(api_url).read)
32
+ json.first['threads'].first['no']
33
+ rescue
34
+ raise JSONError, "JSON processing failed. Please try again"
35
+ end
36
+ end
37
+
38
+ def self.get_image_count(board, thread_id)
39
+ posts = self.get_posts(board, thread_id)
40
+ posts.first["images"]
41
+ end
42
+
43
+ def self.download(url)
44
+ request = Request.prepare(url)
45
+ directory_name = request.create_directory
46
+ request.broadcast
47
+ request.images.each do |image|
48
+ image.download(directory_name)
49
+ end
50
+ puts "\n"
51
+ end
52
+
53
+ def broadcast
54
+ puts "=== Fourchanify ==="
55
+ puts "images: #{self.image_count}"
56
+ end
57
+
58
+ def create_directory
59
+ directory_name = "#{self.board}_#{self.thread_id}"
60
+ Dir.mkdir("./#{directory_name}")
61
+ directory_name
62
+ end
63
+
64
+ def prepare_images
65
+ posts = Request.get_posts(self.board, self.thread_id)
66
+ posts_with_images = posts.select {|p| p["filename"] && p["ext"] }
67
+
68
+ # images in replies + the image of OP
69
+ self.image_count = posts.first["images"] + 1
70
+ self.images = []
71
+
72
+ posts_with_images.each do |p|
73
+ image = Image.prepare(:filename => p["filename"], :ext => p["ext"],
74
+ :fsize => p["fsize"], :tim => p["tim"], :board => self.board)
75
+ self.images << image
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ module Fourchanify
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "fourchanify/version"
2
+ require 'fourchanify/models/image'
3
+ require 'fourchanify/models/request'
4
+
5
+ require "fourchan-urler"
6
+ require "json"
7
+ require "open-uri"
8
+
9
+ module Fourchanify
10
+ end
11
+
@@ -0,0 +1,9 @@
1
+ FactoryGirl.define do
2
+ factory :image do
3
+ filename { filename }
4
+ ext { ext }
5
+ fsize { fsize }
6
+ tim { tim }
7
+ url { url }
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ FactoryGirl.define do
2
+ factory :request do
3
+ url { "http://boards.4chan.org/#{board}/res/#{thread_id}" }
4
+ board { board }
5
+ thread_id { thread_id }
6
+ image_count { image_count }
7
+ images { images }
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+
3
+ describe Image do
4
+ let(:board) { board_names_list.sample }
5
+ let(:image) { get_images(board, get_first_thread_no(board)).first }
6
+ let(:filename) { image.filename }
7
+ let(:ext) { image.ext }
8
+ let(:fsize) { image.fsize }
9
+ let(:tim) { image.tim }
10
+ let(:url) { image.get_url }
11
+
12
+ before do
13
+ @request = FactoryGirl.build(:image, :filename => filename, :ext => ext,
14
+ :fsize => fsize, :tim => tim, :url => url)
15
+ end
16
+ subject { @request }
17
+
18
+ it { should respond_to(:filename) }
19
+ it { should respond_to(:ext) }
20
+ it { should respond_to(:fsize) }
21
+ it { should respond_to(:tim) }
22
+
23
+ its(:filename) { should == filename }
24
+ its(:ext) { should == ext }
25
+ its(:fsize) { should == fsize }
26
+ its(:tim) { should == tim }
27
+ end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe Request do
4
+ let(:board) { board_names_list.sample }
5
+ let(:thread_id) { get_first_thread_no(board) }
6
+ let(:image_count) { get_image_count(board, thread_id) }
7
+ let(:images) { get_images(board, thread_id) }
8
+
9
+ before do
10
+ @request = FactoryGirl.build(:request, :board => board,
11
+ :thread_id => thread_id, :image_count => image_count, :images => images)
12
+ end
13
+ subject { @request }
14
+
15
+ it { should respond_to(:board) }
16
+ it { should respond_to(:thread_id) }
17
+ it { should respond_to(:image_count) }
18
+ it { should respond_to(:url) }
19
+ it { should respond_to(:images) }
20
+
21
+ its(:board) { should == board }
22
+ its(:thread_id) { should == thread_id }
23
+ its(:image_count) { should == image_count }
24
+ its(:images) { should == images }
25
+ end
@@ -0,0 +1,34 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
9
+
10
+ require 'rubygems'
11
+ require 'bundler/setup'
12
+ require 'factory_girl'
13
+ require 'json'
14
+ require 'open-uri'
15
+ require 'fourchanify'
16
+
17
+ Dir["spec/support/*.rb"].each { |f| require File.expand_path(f) }
18
+ Dir["spec/factories/*.rb"].each { |f| require File.expand_path(f) }
19
+
20
+ include Fourchanify
21
+
22
+ RSpec.configure do |config|
23
+ config.treat_symbols_as_metadata_keys_with_true_values = true
24
+ config.run_all_when_everything_filtered = true
25
+ config.filter_run :focus
26
+
27
+ # Run specs in random order to surface order dependencies. If you find an
28
+ # order dependency and want to debug it, you can fix the order by providing
29
+ # the seed, which is printed after each run.
30
+ # --seed 1234
31
+ config.order = 'random'
32
+
33
+ config.include FactoryGirl::Syntax::Methods
34
+ end
@@ -0,0 +1,23 @@
1
+ def get_first_thread_no(board)
2
+ Fourchanify::Request.get_first_thread_no(board)
3
+ end
4
+
5
+ def get_image_count(board, thread_id)
6
+ Fourchanify::Request.get_image_count(board, thread_id)
7
+ end
8
+
9
+ def board_names_list
10
+ FourchanUrler.boards_list
11
+ end
12
+
13
+ def get_images(board, thread_id)
14
+ posts = Request.get_posts(board, thread_id)
15
+ posts_with_images = posts.select {|p| p["filename"] && p["ext"] }
16
+
17
+ images = []
18
+ posts_with_images.each do |p|
19
+ images << Image.prepare(:filename => p["filename"],
20
+ :ext => p["ext"], :fsize => p["fsize"], :tim => p["tim"])
21
+ end
22
+ images
23
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fourchanify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jason Kim
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fourchan-urler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
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
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: factory_girl
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Download images from 4chan to your computer given a thread url
98
+ email:
99
+ - active_red@hotmail.com
100
+ executables:
101
+ - fourchanify
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .rspec
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/fourchanify
112
+ - fourchanify.gemspec
113
+ - lib/fourchanify.rb
114
+ - lib/fourchanify/models/image.rb
115
+ - lib/fourchanify/models/request.rb
116
+ - lib/fourchanify/version.rb
117
+ - spec/factories/images.rb
118
+ - spec/factories/requests.rb
119
+ - spec/models/image_spec.rb
120
+ - spec/models/request_spec.rb
121
+ - spec/spec_helper.rb
122
+ - spec/support/utilities.rb
123
+ homepage: https://github.com/serv/fourchanify
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.0.3
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Download images from 4chan to your computer
147
+ test_files:
148
+ - spec/factories/images.rb
149
+ - spec/factories/requests.rb
150
+ - spec/models/image_spec.rb
151
+ - spec/models/request_spec.rb
152
+ - spec/spec_helper.rb
153
+ - spec/support/utilities.rb