Skip to content

Instantly share code, notes, and snippets.

@paractmol
Last active June 17, 2019 06:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paractmol/879efcc060d8146fea1561969d464a57 to your computer and use it in GitHub Desktop.
Save paractmol/879efcc060d8146fea1561969d464a57 to your computer and use it in GitHub Desktop.
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
Copy link

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

@paractmol
Copy link
Author

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