bayuploader 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 1.0.0 / 2007-07-26
2
+
3
+ * Initial Release
4
+
data/Manifest.txt ADDED
@@ -0,0 +1,8 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/bayuploader
6
+ lib/bayuploader.rb
7
+ lib/multipart.rb
8
+ test/test_bayuploader.rb
data/README.txt ADDED
@@ -0,0 +1,57 @@
1
+ bayuploader
2
+ by Britt Crawford
3
+ http://www.illtemper.org
4
+
5
+ == DESCRIPTION:
6
+
7
+ * BayUploader is a simple command line uploader for http://bayimg.com the Pirate Bay's image hosting site.
8
+ It allows you to upload and tag images and specify the removal code.
9
+ * This gem makes use of the Multipart module taken from: http://deftcode.com/code/flickr_upload/multipartpost.rb Thanks to Bill Stillwell and Keith (?) keith@oreilly.com for making this code available.
10
+
11
+ == FEATURES/PROBLEMS:
12
+
13
+ * No known issues at this time.
14
+
15
+ == SYNOPSIS:
16
+
17
+ * Using BayUploader is very simple. The easiest way is just to type: bayuploader your_file.jpg
18
+ This will upload the with no tags. The removal code will be set to a default value.
19
+ * To tag a file you are uploading just add the -t or --tags option and a comma separated list of tags: bayuploader your_file.jpg -t yourtag,yourothertag
20
+ * Finally to specify a removal code use the -c or --code option: bayuploader your_file.jpg -t yourfirsttag,yoursecondtag -c your_removal_code
21
+
22
+ == REQUIREMENTS:
23
+
24
+ * ruby 1.8.5+
25
+
26
+ == INSTALL:
27
+
28
+ * sudo gem install bayuploader
29
+
30
+ == LICENSE:
31
+
32
+ Copyright (c) 2007 Britt Crawford
33
+
34
+ Creative Commons Attribution-ShareAlike 3.0
35
+
36
+ <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">
37
+ <img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" />
38
+ </a>
39
+ <br />This work is licensed under a
40
+ <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0 License</a>.
41
+
42
+ You are free:
43
+ * to Share — to copy, distribute and transmit the work
44
+ * to Remix — to adapt the work
45
+
46
+ Under the following conditions:
47
+ * Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
48
+ * Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.
49
+ * For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page: http://creativecommons.org/licenses/by-sa/3.0/
50
+ * Any of the above conditions can be waived if you get permission from the copyright holder.
51
+ * Nothing in this license impairs or restricts the author's moral rights
52
+
53
+ Summary of licensing conditions:
54
+ http://creativecommons.org/licenses/by-sa/3.0/
55
+
56
+ Complete text of the license:
57
+ http://creativecommons.org/licenses/by-sa/3.0/legalcode
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+ $:.unshift(File.dirname(__FILE__) + "/lib")
4
+ require 'bayuploader'
5
+
6
+ Hoe.new('BayUploader', BayUploader::VERSION) do |p|
7
+ p.name = 'bayuploader'
8
+ p.author = 'Britt Crawford'
9
+ p.email = 'britt@illtemper.org'
10
+ p.summary = "A simple command line uploader for BayImg.com, the Piratebay's image hosting site."
11
+ p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
12
+ p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
13
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
14
+ p.extra_deps << ['hpricot', '>= 0.5']
15
+ p.extra_deps << ['mime-types', '>= 1.15']
16
+ p.extra_deps << ['OptionParser', '>= 0.5.1']
17
+ p.remote_rdoc_dir = ''
18
+ end
19
+
20
+ desc "Release and publish documentation"
21
+ task :repubdoc => [:release, :publish_docs]
data/bin/bayuploader ADDED
@@ -0,0 +1,158 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'net/http'
4
+ require 'optparse'
5
+ require 'uri'
6
+ require 'hpricot'
7
+ require 'base64'
8
+
9
+ # A simple command line tool for uploading images
10
+ # to bayimg.com
11
+ #
12
+ # Author:: Britt Crawford (mailto:britt@illtemper.org)
13
+ # License:: Licensed under Creative Commons Attribution-Share Alike 3.0 License
14
+ # <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">
15
+ # <img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" />
16
+ # </a>
17
+ # <br />This work is licensed under a
18
+ # <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0 License</a>.
19
+
20
+ module Multipart
21
+ # From: http://deftcode.com/code/flickr_upload/multipartpost.rb
22
+ ## Helper class to prepare an HTTP POST request with a file upload
23
+ ## Mostly taken from
24
+ #http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/113774
25
+ ### WAS:
26
+ ## Anything that's broken and wrong probably the fault of Bill Stilwell
27
+ ##(bill@marginalia.org)
28
+ ### NOW:
29
+ ## Everything wrong is due to keith@oreilly.com
30
+ require 'rubygems'
31
+ require 'mime/types'
32
+ require 'net/http'
33
+ require 'cgi'
34
+
35
+ class Param
36
+ attr_accessor :k, :v
37
+ def initialize( k, v )
38
+ @k = k
39
+ @v = v
40
+ end
41
+
42
+ def to_multipart
43
+ #return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
44
+ # Don't escape mine...
45
+ return "Content-Disposition: form-data; name=\"#{k}\"\r\n\r\n#{v}\r\n"
46
+ end
47
+ end
48
+
49
+ class FileParam
50
+ attr_accessor :k, :filename, :content
51
+ def initialize( k, filename, content )
52
+ @k = k
53
+ @filename = filename
54
+ @content = content
55
+ end
56
+
57
+ def to_multipart
58
+ #return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n "
59
+ # Don't escape mine
60
+ return "Content-Disposition: form-data; name=\"#{k}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n"
61
+ end
62
+ end
63
+ class MultipartPost
64
+ BOUNDARY = 'tarsiers-rule0000'
65
+ HEADER = {"Content-type" => "multipart/form-data, boundary=" + BOUNDARY + " "}
66
+
67
+ def prepare_query (params)
68
+ fp = []
69
+ params.each {|k,v|
70
+ if v.respond_to?(:read)
71
+ fp.push(FileParam.new(k, v.path, v.read))
72
+ else
73
+ fp.push(Param.new(k,v))
74
+ end
75
+ }
76
+ query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--"
77
+ return query, HEADER
78
+ end
79
+ end
80
+ end
81
+
82
+ class BayUploader
83
+ VERSION = '1.0.0'
84
+ BayImgUrl = 'bayimg.com'
85
+ Usage = 'Usage: bay_uploader filename [options]'
86
+
87
+ def self.upload(argv)
88
+ options = BayUploader.parse_options(argv)
89
+ response = BayUploader.upload_file(options[:file], options[:code], options[:tags])
90
+ image_id = BayUploader.process_response(response)
91
+ image_id.nil? ? nil : ["http://#{BayImgUrl}#{image_id}",options[:code]]
92
+ end
93
+
94
+ def self.upload_file(file_name, removal_code, tags)
95
+ params = {}
96
+
97
+ begin
98
+ params['code'] = removal_code
99
+ params['tags'] = tags.join(' ')
100
+ file = File.new(file_name)
101
+ params['file'] = file
102
+ mp = Multipart::MultipartPost.new
103
+ query,headers = mp.prepare_query(params)
104
+ ensure
105
+ file.close unless file.nil?
106
+ end
107
+
108
+ BayUploader.post(query,headers).body
109
+ end
110
+
111
+ def self.process_response(html)
112
+ doc = Hpricot(html)
113
+ link = doc.at("div#extra2/a")
114
+ link.nil? ? nil : link['href']
115
+ end
116
+
117
+ def self.parse_options(argv)
118
+ file_name = argv.shift
119
+ opts = {:code => BayUploader.encode(file_name), :tags => []}
120
+
121
+ parser = OptionParser.new do |parser|
122
+ parser.on('-c CODE', '--code CODE',"Specify removal code") do |code|
123
+ opts[:code] = code
124
+ end
125
+
126
+ parser.on('--tags [TAGS]', Array,"Specify tags to be applied") do |tags|
127
+ opts[:tags] = tags
128
+ end
129
+ end
130
+
131
+ parser.parse argv
132
+ {:file => file_name, :code => opts[:code], :tags => opts[:tags]}
133
+ end
134
+
135
+ def self.encode(file_name)
136
+ Base64.b64encode(file_name.split(File::Separator).last.gsub(/\s/,'-')).gsub(/[^\w\d]/,'')
137
+ end
138
+
139
+ private
140
+ def self.post( query, headers={} )
141
+ Net::HTTP.start(BayImgUrl) do | con |
142
+ con.post('/upload', query, headers);
143
+ end
144
+ end
145
+ end
146
+
147
+ if $0.split(File::Separator).last == 'bayuploader' || $0 == __FILE__ then
148
+ if ARGV.nil? || ARGV.size ==0
149
+ puts "You have to tell me what file to upload."
150
+ exit 1
151
+ end
152
+ url,code = BayUploader.upload(ARGV)
153
+ unless url.nil?
154
+ puts "Url: #{url}\nRemoval Code: #{code}"
155
+ else
156
+ puts "Error uploading image"
157
+ end
158
+ end
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'net/http'
4
+ require 'optparse'
5
+ require 'uri'
6
+ require 'hpricot'
7
+ require 'base64'
8
+ require File.dirname(__FILE__) + '/multipart.rb'
9
+
10
+ # A simple command line tool for uploading images
11
+ # to bayimg.com
12
+ #
13
+ # Author:: Britt Crawford (mailto:britt@illtemper.org)
14
+ # License:: Licensed under Creative Commons Attribution-Share Alike 3.0 License
15
+ # <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">
16
+ # <img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" />
17
+ # </a>
18
+ # <br />This work is licensed under a
19
+ # <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0 License</a>.
20
+
21
+ class BayUploader
22
+ VERSION = '1.0.0'
23
+ BayImgUrl = 'bayimg.com'
24
+ Usage = 'Usage: bay_uploader filename [options]'
25
+
26
+ def self.upload(argv)
27
+ options = BayUploader.parse_options(argv)
28
+ response = BayUploader.upload_file(options[:file], options[:code], options[:tags])
29
+ image_id = BayUploader.process_response(response)
30
+ image_id.nil? ? nil : ["http://#{BayImgUrl}#{image_id}",options[:code]]
31
+ end
32
+
33
+ def self.upload_file(file_name, removal_code, tags)
34
+ params = {}
35
+
36
+ begin
37
+ params['code'] = removal_code
38
+ params['tags'] = tags.join(' ')
39
+ file = File.new(file_name)
40
+ params['file'] = file
41
+ mp = Multipart::MultipartPost.new
42
+ query,headers = mp.prepare_query(params)
43
+ ensure
44
+ file.close unless file.nil?
45
+ end
46
+
47
+ BayUploader.post(query,headers).body
48
+ end
49
+
50
+ def self.process_response(html)
51
+ doc = Hpricot(html)
52
+ link = doc.at("div#extra2/a")
53
+ link.nil? ? nil : link['href']
54
+ end
55
+
56
+ def self.parse_options(argv)
57
+ file_name = argv.shift
58
+ opts = {:code => BayUploader.encode(file_name), :tags => []}
59
+
60
+ parser = OptionParser.new do |parser|
61
+ parser.on('-c CODE', '--code CODE',"Specify removal code") do |code|
62
+ opts[:code] = code
63
+ end
64
+
65
+ parser.on('--tags [TAGS]', Array,"Specify tags to be applied") do |tags|
66
+ opts[:tags] = tags
67
+ end
68
+ end
69
+
70
+ parser.parse argv
71
+ {:file => file_name, :code => opts[:code], :tags => opts[:tags]}
72
+ end
73
+
74
+ def self.encode(file_name)
75
+ Base64.b64encode(file_name.split(File::Separator).last.gsub(/\s/,'-')).gsub(/[^\w\d]/,'')
76
+ end
77
+
78
+ private
79
+ def self.post( query, headers={} )
80
+ Net::HTTP.start(BayImgUrl) do | con |
81
+ con.post('/upload', query, headers);
82
+ end
83
+ end
84
+ end
85
+
86
+ if $0 == __FILE__ then
87
+ url,code = BayUploader.upload(ARGV)
88
+ unless url.nil?
89
+ puts "Url: #{url}\nRemoval Code: #{code}"
90
+ else
91
+ puts "Error uploading image"
92
+ end
93
+ end
data/lib/multipart.rb ADDED
@@ -0,0 +1,61 @@
1
+ module Multipart
2
+ # From: http://deftcode.com/code/flickr_upload/multipartpost.rb
3
+ ## Helper class to prepare an HTTP POST request with a file upload
4
+ ## Mostly taken from
5
+ #http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/113774
6
+ ### WAS:
7
+ ## Anything that's broken and wrong probably the fault of Bill Stilwell
8
+ ##(bill@marginalia.org)
9
+ ### NOW:
10
+ ## Everything wrong is due to keith@oreilly.com
11
+ require 'rubygems'
12
+ require 'mime/types'
13
+ require 'net/http'
14
+ require 'cgi'
15
+
16
+ class Param
17
+ attr_accessor :k, :v
18
+ def initialize( k, v )
19
+ @k = k
20
+ @v = v
21
+ end
22
+
23
+ def to_multipart
24
+ #return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
25
+ # Don't escape mine...
26
+ return "Content-Disposition: form-data; name=\"#{k}\"\r\n\r\n#{v}\r\n"
27
+ end
28
+ end
29
+
30
+ class FileParam
31
+ attr_accessor :k, :filename, :content
32
+ def initialize( k, filename, content )
33
+ @k = k
34
+ @filename = filename
35
+ @content = content
36
+ end
37
+
38
+ def to_multipart
39
+ #return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n "
40
+ # Don't escape mine
41
+ return "Content-Disposition: form-data; name=\"#{k}\"; filename=\"#{filename}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type: #{MIME::Types.type_for(@filename)}\r\n\r\n" + content + "\r\n"
42
+ end
43
+ end
44
+ class MultipartPost
45
+ BOUNDARY = 'tarsiers-rule0000'
46
+ HEADER = {"Content-type" => "multipart/form-data, boundary=" + BOUNDARY + " "}
47
+
48
+ def prepare_query (params)
49
+ fp = []
50
+ params.each {|k,v|
51
+ if v.respond_to?(:read)
52
+ fp.push(FileParam.new(k, v.path, v.read))
53
+ else
54
+ fp.push(Param.new(k,v))
55
+ end
56
+ }
57
+ query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--"
58
+ return query, HEADER
59
+ end
60
+ end
61
+ end
File without changes
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: bayuploader
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2007-07-30 00:00:00 -07:00
8
+ summary: A simple command line uploader for BayImg.com, the Piratebay's image hosting site.
9
+ require_paths:
10
+ - lib
11
+ email: britt@illtemper.org
12
+ homepage: " by Britt Crawford"
13
+ rubyforge_project: bayuploader
14
+ description: "== FEATURES/PROBLEMS: * No known issues at this time. == SYNOPSIS: * Using BayUploader is very simple. The easiest way is just to type: bayuploader your_file.jpg This will upload the with no tags. The removal code will be set to a default value. * To tag a file you are uploading just add the -t or --tags option and a comma separated list of tags: bayuploader your_file.jpg -t yourtag,yourothertag * Finally to specify a removal code use the -c or --code option: bayuploader your_file.jpg -t yourfirsttag,yoursecondtag -c your_removal_code"
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Britt Crawford
31
+ files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ - Rakefile
36
+ - bin/bayuploader
37
+ - lib/bayuploader.rb
38
+ - lib/multipart.rb
39
+ - test/test_bayuploader.rb
40
+ test_files:
41
+ - test/test_bayuploader.rb
42
+ rdoc_options:
43
+ - --main
44
+ - README.txt
45
+ extra_rdoc_files:
46
+ - History.txt
47
+ - Manifest.txt
48
+ - README.txt
49
+ executables:
50
+ - bayuploader
51
+ extensions: []
52
+
53
+ requirements: []
54
+
55
+ dependencies:
56
+ - !ruby/object:Gem::Dependency
57
+ name: hpricot
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Version::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0.5"
64
+ version:
65
+ - !ruby/object:Gem::Dependency
66
+ name: mime-types
67
+ version_requirement:
68
+ version_requirements: !ruby/object:Gem::Version::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "1.15"
73
+ version:
74
+ - !ruby/object:Gem::Dependency
75
+ name: OptionParser
76
+ version_requirement:
77
+ version_requirements: !ruby/object:Gem::Version::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 0.5.1
82
+ version:
83
+ - !ruby/object:Gem::Dependency
84
+ name: hoe
85
+ version_requirement:
86
+ version_requirements: !ruby/object:Gem::Version::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 1.2.2
91
+ version: