lupus 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,73 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ $:.unshift File.dirname(__FILE__) + '/../library'
5
+
6
+ require 'lupus'
7
+
8
+ begin
9
+ require 'majic'
10
+ $COLOR = true
11
+ rescue LoadError
12
+ $COLOR = false
13
+ end
14
+
15
+ Banner =<<-EOD
16
+ _
17
+ | | _ _ _ __ _ _ ___
18
+ | | | | | | '_ \\| | | / __|
19
+ | |__| |_| | |_) | |_| \\__ \\
20
+ |_____\\__,_| .__/ \\__,_|___/
21
+ |_|
22
+ EOD
23
+
24
+ if ARGV.count < 1
25
+ if $COLOR
26
+ puts Banner ^ :bold
27
+
28
+ puts "Usage" ^ :bold
29
+ puts " #$0 #{"<thread>" ^ :bold}"
30
+ puts "Example" ^ :bold
31
+ puts " #$0 #{"http://boards.4chan.org/hr/res/1269029" ^ :bold}"
32
+ else
33
+ puts Banner
34
+
35
+ puts "Usage"
36
+ puts " #$0 <thread>"
37
+ puts "Exaple"
38
+ puts " #$0 http://boards.4chan.org/hr/res/1269029"
39
+ end
40
+
41
+ exit 1
42
+ end
43
+
44
+ ARGV.each do |address|
45
+ begin
46
+ thread = Lupus::Thread.new address
47
+
48
+ if $COLOR
49
+ puts "-- Downloading #{thread.files.count.to_s ^ :green} files."
50
+ else
51
+ puts "-- Downloading #{thread.files.count} files."
52
+ end
53
+
54
+ archive = Lupus::Archive.new thread
55
+ filename = "#{thread.id}.zip"
56
+
57
+ if $COLOR
58
+ puts "-- Packing as #{filename ^ :green}."
59
+ else
60
+ puts "-- Packing as #{thread.id}.zip."
61
+ end
62
+
63
+ archive.pack
64
+
65
+ puts "-- Done."
66
+ rescue Exception
67
+ if $COLOR
68
+ puts "#{"Error" ^ :red} - Could not fetch #{address ^ :bold} - #{$!.class.name ^ :bold}: #{$!.message}"
69
+ else
70
+ puts "Error - Could not fetch #{address} - #{$!.class.name}: #{$!.message}"
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ require 'zip/zip'
4
+ require 'nokogiri'
5
+ require 'open-uri'
6
+
7
+ require 'lupus/post'
8
+ require 'lupus/thread'
9
+ require 'lupus/archive'
10
+
11
+ module Lupus
12
+ class << Version = [0,1]
13
+ def to_s; join '.' end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ module Lupus
4
+ class Archive
5
+ attr_accessor :path
6
+
7
+ def initialize thread
8
+ @path = ::File.join Dir.pwd, "#{thread.id}.zip"
9
+ @thread = thread
10
+ end
11
+
12
+ def pack path = @path
13
+ Zip::ZipFile.open path, Zip::ZipFile::CREATE do |zip|
14
+ @thread.files.each {|file| file.save and zip.add file.name, file.path }
15
+ end
16
+
17
+ @thread.files.each &:delete
18
+ Dir.unlink ::File.join ::File.dirname(@path), @thread.id
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ module Lupus
4
+ class Post
5
+ class File
6
+ attr_accessor :name, :path, :address
7
+
8
+ def initialize address, thread
9
+ @name = ::File.basename address
10
+ @path = ::File.join Dir.pwd, thread.id, @name
11
+ @thread = thread
12
+ @address = URI address
13
+ end
14
+
15
+ def save
16
+ return if ::File.exists? @path
17
+
18
+ Dir.mkdir ::File.dirname @path unless ::Dir.exists? ::File.dirname @path
19
+
20
+ ::File.open @path, 'w+' do |file|
21
+ http = Net::HTTP.new @address.host
22
+
23
+ http.request_get @address.path do |response|
24
+ response.read_body {|chunk| file.write chunk }
25
+ end
26
+ end
27
+ end
28
+
29
+ def delete
30
+ ::File.unlink path if ::File.exists? path
31
+ end
32
+
33
+ alias_method :url, :address
34
+ end
35
+
36
+ def initialize element, thread
37
+ @thread = thread
38
+ @element = element
39
+ end
40
+
41
+ def file
42
+ if link = @element.at(".filesize a")
43
+ @file ||= File.new link['href'], @thread
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ module Lupus
4
+ class Thread
5
+ attr_accessor :address
6
+
7
+ def initialize address
8
+ @address = URI address
9
+ end
10
+
11
+ def id
12
+ @page.at("input[@name='resto']")['value']
13
+ end
14
+
15
+ def posts
16
+ @posts ||= page.css("td.reply").to_a.map {|element| Post.new element, self }
17
+ end
18
+
19
+ def files
20
+ @files ||= posts.map(&:file).compact
21
+ end
22
+
23
+ private
24
+
25
+ def page; @page ||= Nokogiri::HTML open @address end
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lupus
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Mikkel Kroman
12
+ autorequire:
13
+ bindir: executables
14
+ cert_chain: []
15
+
16
+ date: 2011-03-13 00:00:00 +01:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: rubyzip
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 9
30
+ - 4
31
+ version: 0.9.4
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: majic
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ - 1
45
+ version: "0.1"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description:
49
+ email: mk@maero.dk
50
+ executables:
51
+ - lupus
52
+ extensions: []
53
+
54
+ extra_rdoc_files: []
55
+
56
+ files:
57
+ - library/lupus/thread.rb
58
+ - library/lupus/archive.rb
59
+ - library/lupus/post.rb
60
+ - library/lupus.rb
61
+ - executables/lupus
62
+ has_rdoc: true
63
+ homepage:
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options: []
68
+
69
+ require_paths:
70
+ - library
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 1
78
+ - 9
79
+ - 1
80
+ version: 1.9.1
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.7
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: A tool to extract and zip the images off of a 4chan thread.
96
+ test_files: []
97
+