Module: Participle::Token
- Included in:
- Bot
- Defined in:
- lib/token.rb
Overview
Interface to dAmn for retrieving tokens. They seriously need to put out an API for this shit.
Class Method Summary (collapse)
-
+ (String) to_qstring(hash)
Converts a hash to a properly escaped query string for POST requests.
Instance Method Summary (collapse)
-
- (String?) get_token(cache = true)
Get an authtoken!.
Class Method Details
+ (String) to_qstring(hash)
Converts a hash to a properly escaped query string for POST requests
11 12 13 14 15 16 17 |
# File 'lib/token.rb', line 11 def self.to_qstring hash hash.map do |a,b| a + "=" + b.to_s.gsub(/([^A-Za-z0-9\-])/) do "%" + $1.unpack("U*")[0].to_s(16) end end.join("&") end |
Instance Method Details
- (String?) get_token(cache = true)
Get an authtoken!
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/token.rb', line 22 def get_token cache = true cachepath = "storage/#{File.basename($opts[:config], ".yaml")}.token.cache" if cache log "Attempting to retrieve token from cache..." if File.file?(cachepath) hl = open(cachepath) token = hl.read hl.close log "Token retrieved." return token else log "No token cache file found." end end log "Fetching authtoken from server, this may take a moment." uri = URI.parse("https://www.deviantart.com/users/login") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE = nil http.start do data = Token.to_qstring({"username" => @conf.nick, "password" => @conf.password, "remember_me" => 1}) http.request_post(uri.path,data) do |res| uri = URI.parse("http://chat.deviantart.com/chat/Botdom") Net::HTTP.new(uri.host, uri.port).start do |ht| request = Net::HTTP::Get.new(uri.path) request["cookie"] = res["Set-Cookie"].scan(/[a-z_]+=[^ ]{20,}/).join(";") ht.request(request) do |req| if req.body.empty? return nil else open(cachepath, "w"){|f|f.write(req.body.scan(/[0-9a-f]{32}/)[0])} return req.body.scan(/[0-9a-f]{32}/)[0] end end end end end end |