Last active
June 17, 2019 06:55
-
-
Save paractmol/879efcc060d8146fea1561969d464a57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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!