Last active
December 30, 2022 08:53
-
-
Save yoursdearboy/a503f39adac5ad7af7016a2c37ed4366 to your computer and use it in GitHub Desktop.
Alfred workflow to lookup multitran.ru
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 'net/http' | |
require 'nokogiri' | |
def lookup(query) | |
path = "https://www.multitran.com/c/M.exe?CL=1&s=#{query}&l1=1&l2=2" | |
uri = URI(URI.escape(path)) | |
Net::HTTP.get(uri) | |
end | |
def node_to_str(node) | |
node.xpath('descendant::text()') | |
.map(&:content) | |
.map{|s| s.gsub /[[:space:]]/, ' ' } | |
.map(&:strip) | |
.select{|s| s.size > 0} | |
.join(' ') | |
.gsub('( ', '(').gsub(' )', ')') | |
end | |
def translations(doc) | |
doc.xpath('//form[@id="translation"]/../table[1]/tr').map do |tr| | |
dict = node_to_str tr.xpath('td[1]') | |
translations = node_to_str tr.xpath('td[2]') | |
[dict, translations.split(' ; ')] | |
end.select do |dict, translations| | |
translations.size > 0 | |
end | |
end | |
def format_doc(body) | |
"<?xml version=\"1.0\"?>#{body}" | |
end | |
def format_items(items) | |
items = items.map{|i| format_item *i}.join "\n" | |
"<items>#{items}</items>" | |
end | |
def format_item(title, subtitle) | |
"<item><title>#{title}</title><subtitle>#{subtitle}</subtitle></item>" | |
end | |
query = ARGV.first | |
res = lookup query | |
doc = Nokogiri::HTML res | |
trans = translations(doc) | |
items = trans.map do |dict,transes| | |
[dict, transes.join(', ')] | |
end | |
puts format_doc(format_items(items)) |
also not working with Alfred 3
@yoursdearboy have a look at dict.cc Workflow. This one is working great. It's based on a python script though instead of ruby:
https://github.com/dennis-tra/alfred-dict.cc-workflow
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately not working with Alfred 4