Skip to content

Instantly share code, notes, and snippets.

@jheidt jheidt/bingy.rb forked from rudkovskyi/bingy.rb
Created Oct 24, 2016

Embed
What would you like to do?
require 'open-uri'
class Bingy
LANDING_PAGE = 'http://www.bing.com/translator'.freeze
SPEAK_URL = LANDING_PAGE + '/api/language/Speak?locale=en-US&media=audio/mp3'
def initialize(text, gender = 'male')
@user_agent = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
@params = { text: text, gender: gender }
end
def params
URI.encode_www_form(@params)
end
def url
@url = SPEAK_URL + '&' + params
end
def cookies
open(LANDING_PAGE, 'User-Agent' => @user_agent).meta['set-cookie']
end
def token
cookies.scan(/(mtstkn=.+?);|(MUID=.+?);/).flatten.compact.join(';')
end
def request
open(url, 'Cookie' => token,
'Referer' => url,
'Connection' => 'keep-alive',
'Range' => 'bytes=0-',
'Accept-Encoding' => 'identity;q=1, *;q=0',
'Accept-Language' => 'en-US,en;q=0.8,uk;q=0.6')
end
def download(out)
File.write(out, request.read)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.