-
-
Save johan--/ea918389c80440a09fd4 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
#!/usr/bin/env ruby | |
require 'appscript' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'clipboard' | |
url = ARGV.length >= 1 ? ARGV[0] : Clipboard.paste | |
def zhihu(doc) | |
title = doc.css('title')[0].content.sub(/[ ]*- 知乎$/,'') | |
html = doc.css('div[@data-action="/answer/content"] > .zm-editable-content')[0].children | |
html.css("img").each do |img| | |
if img.attribute('src').to_s.end_with? "whitedot.jpg" | |
img.set_attribute('src', img.attribute('data-actualsrc')) | |
end | |
img.attributes.each_key do |key| | |
if key != 'src' | |
img.remove_attribute(key) | |
end | |
end | |
end | |
html.css('a.toggle-expand').remove | |
return { | |
"title" => title, | |
"html" => html | |
} | |
end | |
def parse_and_add(url) | |
doc = Nokogiri::HTML(open(url)) | |
result = zhihu(doc) | |
note = Appscript.app("Evernote.app").create_note( | |
:title => result["title"], | |
:with_html => result["html"].to_s | |
) | |
note.source_URL.set(url) | |
note.latitude.set(31.201203217036515) | |
note.longitude.set(121.577187656714) | |
end | |
if url.end_with? '.txt' | |
File.open(url).each do |line| | |
parse_and_add(line) | |
end | |
else | |
parse_and_add(url) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment