Created
February 16, 2012 04:42
-
-
Save veverkap/1842100 to your computer and use it in GitHub Desktop.
poster.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Takes a hash of string and file parameters and returns a string of text | |
# formatted to be sent as a multipart form post. | |
# | |
# Author:: Cody Brimhall <mailto:cbrimhall@ucdavis.edu> | |
# Created:: 22 Feb 2008 | |
require 'cgi' | |
require 'net/http' | |
require 'net/https' | |
# require 'json/lib/json/version' | |
# require 'json/lib/json/common' | |
# require 'json/lib/json/common' | |
# require 'json/lib/json/pure/parser' | |
# require 'json/lib/json/pure/generator' | |
# require 'json/lib/json/pure' | |
require 'rubygems' | |
#all this stuff came from a google search. basically, multipart is hosed in ruby, so we fix it up | |
module Multipart | |
VERSION = "1.0.0" unless const_defined?(:VERSION) | |
# Formats a given hash as a multipart form post | |
# If a hash value responds to :string or :read messages, then it is | |
# interpreted as a file and processed accordingly; otherwise, it is assumed | |
# to be a string | |
class Post | |
# We have to pretend like we're a web browser... | |
USERAGENT = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6" unless const_defined?(:USERAGENT) | |
BOUNDARY = "0123456789ABLEWASIEREISAWELBA9876543210" unless const_defined?(:BOUNDARY) | |
CONTENT_TYPE = "multipart/form-data; boundary=#{ BOUNDARY }" unless const_defined?(:CONTENT_TYPE) | |
HEADER = { "Content-Type" => CONTENT_TYPE, "User-Agent" => USERAGENT } unless const_defined?(:HEADER) | |
def self.prepare_query(params) | |
fp = [] | |
params.each do |k, v| | |
# Are we trying to make a file parameter? | |
if v.respond_to?(:path) and v.respond_to?(:read) then | |
fp.push(FileParam.new(k, v.path, v.read)) | |
# We must be trying to make a regular parameter | |
else | |
fp.push(StringParam.new(k, v)) | |
end | |
end | |
# Assemble the request body using the special multipart format | |
query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--" | |
HEADER['Cookie'] = params['Cookie'] | |
return query, HEADER | |
end | |
end | |
private | |
# Formats a basic string key/value pair for inclusion with a multipart post | |
class StringParam | |
attr_accessor :k, :v | |
def initialize(k, v) | |
@k = k | |
@v = v | |
end | |
def to_multipart | |
return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n" | |
end | |
end | |
# Formats the contents of a file or string for inclusion with a multipart | |
# form post | |
class FileParam | |
attr_accessor :k, :filename, :content | |
def initialize(k, filename, content) | |
@k = k | |
@filename = filename | |
@content = content | |
end | |
def to_multipart | |
# If we can tell the possible mime-type from the filename, use the | |
# first in the list; otherwise, use "application/octet-stream" | |
#mime_type = MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0] | |
return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{ filename }\"\r\n" + | |
"Content-Type: application/x-bittorrent\r\n\r\n#{ content }\r\n" | |
end | |
end | |
end | |
#when testing use this | |
#ARGV[0] = "yourusername" | |
#ARGV[1] = "yourpassword" | |
#ARGV[2] = "TITLE" | |
valid_torrent_types = ["Music", "Applications", "E-Books", "Audiobooks", "E-Learning Videos", "Magazines", "Comics", "Anime", "Movies", "TV", "Games - PC", "Games - Console", "Documentaries", "Misc"] | |
usage = "Usage: ./poster.rb USERNAME PASSWORD TITLE DESCRIPTION TORRENT_TYPE TAGS_SEPARATED_BY_COMMAS TORRENT_FILE" | |
if ARGV.size != 7 | |
puts usage | |
raise "Error: 7 arguments expected, got " + ARGV.size.to_s + "." | |
end | |
username = ARGV[0] | |
password = ARGV[1] | |
title = ARGV[2] | |
description = ARGV[3] | |
# one of Music, Applications, E-Books, Audiobooks, E-Learning Videos, Magazines, Comics, Anime, Movies, TV, Games - PC, Games - Console, Documentaries, Misc | |
torrent_type = ARGV[4] | |
if !valid_torrent_types.include?(torrent_type) | |
raise "Error: " + torrent_type + " is not a valid torrent type" | |
end | |
tag_string = ARGV[5] | |
torrent_file = ARGV[6] | |
if !File.exists?(torrent_file) | |
raise "Error: torrent file does not exist" | |
end | |
#here is where you login to baconbits | |
http = Net::HTTP.new('baconbits.org', 443) | |
http.use_ssl = true | |
path = '/login.php' | |
data = 'username=' + username + '&password=' + password | |
headers = { | |
'Referer' => 'https://baconbits.org/index.php', | |
'Content-Type' => 'application/x-www-form-urlencoded' | |
} | |
resp, data = http.post(path, data, headers) | |
cookie = resp.response['set-cookie'] | |
#we are logged in now | |
headers = { | |
'Cookie' => cookie, | |
'Referer' => 'https://baconbits.org/login.php' | |
} | |
my_file = File.new(torrent_file) | |
params = { | |
"title" => title, | |
"tags" => tag_string, | |
"desc" => description, | |
"type" => "E-Books", | |
"image" => "", #change this yourself | |
"submit" => "true", | |
"Cookie" => cookie, | |
"file_input" => my_file } | |
data, headers = Multipart::Post.prepare_query(params) | |
resp, data = http.post('/upload.php', data, headers) | |
#puts resp.inspect | |
#puts resp.body | |
#puts data.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment