Skip to content

Instantly share code, notes, and snippets.

@aaronpearce
Last active December 21, 2015 22:19
Show Gist options
  • Save aaronpearce/6374354 to your computer and use it in GitHub Desktop.
Save aaronpearce/6374354 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Require gems needed to run
require 'oauth2'
require "socket"
require 'net/http'
require 'uri'
require 'net/http/post/multipart'
require 'launchy'
toValue = ARGV[0].to_i
# Set client info
client_id = "{INSERT CLIENT ID}"
client_secret = "{INSERT CLIENT SECRET}"
scope = "code"
redirect_uri = 'http://localhost:2000/oauth2callback'
auth = OAuth2::Client.new(client_id, client_secret, {:site => 'https://www.deviantart.com', :authorize_url => "/oauth2/draft15/authorize", :token_url => "/oauth2/draft15/token"}) do |b|
b.request :multipart
b.request :url_encoded
b.adapter :net_http
end
Launchy.open(auth.auth_code.authorize_url(:redirect_uri => redirect_uri))
puts "\n1) Accept the authorization request from deviantART in your browser:"
puts "\n2) deviantART will redirect you to localhost, but just copy the code parameter out of the URL they redirect you to, paste it here and hit enter:\n"
server = TCPServer.open 2000
puts "\nListening on localhost:2000 for code returned by deviantART"
gotCode = false;
until (gotCode == true) do
#looks like a client method call to open the connection
client = server.accept
first_request_header = client.gets
resp = first_request_header
#setting the request headers
headers = ['http/1.1 200 ok',
"date: #{CGI.rfc1123_date(Time.now)}",
'server: ruby',
'content-type: text/html; charset=iso-8859-1',
"content-length: #{resp.length}\r\n\r\n"].join("\r\n")
code = resp.split('?code=').last
code = code.split(' ').first
client.puts code
#closes client connection to local host
client.close
if code
gotCode = true
end
end
server.close
token = auth.auth_code.get_token(code, { :redirect_uri => redirect_uri, :token_method => :post })
puts "\nStarting upload to populate your Sta.sh with files..."
i = 0
while i < toValue do
url = URI.parse("https://www.deviantart.com/api/draft15/stash/submit")
img = File.open("file.png")
req = Net::HTTP::Post::Multipart.new(
"#{url.path}?access_token=#{token.token}",
"file" => UploadIO.new(img, "image/png", img.path),
"filename" => "some"
)
n = Net::HTTP.new(url.host, url.port)
n.use_ssl = true
n.verify_mode = OpenSSL::SSL::VERIFY_NONE
n.start do |http|
@result = http.request(req)
end
i += 1
num = i.to_s
print "\e[0K\r#{num} done."
end
puts "\n\nUploaded #{toValue} files!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment