Skip to content

Instantly share code, notes, and snippets.

@rudkovskyi rudkovskyi/bingy.rb
Last active Jun 17, 2019

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
@foxpcteam

This comment has been minimized.

Copy link

foxpcteam commented Apr 1, 2017

Hi,
thanks for the code and the article.
can you help to write the same code in php or js?

@rudkovskyi

This comment has been minimized.

Copy link
Owner Author

rudkovskyi commented Jan 20, 2018

Hi @foxpcteam I would love to although I don't have that much time to lay my hands on. I am very sorry, but I would be happy if you'd write that code using any other language and would share it with us!

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.