-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.rb
61 lines (47 loc) · 1.51 KB
/
init.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
class SSRNImporter
SSRN_BASE_URL = "http://ssrn.com/abstract="
def initialize
files.each do |f|
get_metadata File.basename(f)
end
end
def files
if !ARGV.empty?
if File.directory?(ARGV[0])
Dir.chdir(ARGV[0])
Dir.glob('*')
else
ARGV
end
else
Dir.glob('*')
end.find_all { |f| f.match /^SSRN.+\.pdf$/i }
end
def get_metadata(filename)
md = {}
md[:filename] = filename
md[:ssrn_id] = filename.match(/\d+/)[0]
md[:ssrn_url] = SSRN_BASE_URL + md[:ssrn_id]
ssrn_page = Nokogiri::HTML(open(md[:ssrn_url]))
md[:title] = ssrn_page.css("#abstractTitle h1").text().strip || "Untitled"
md[:authors] = []
ssrn_page.css("a[title='View other papers by this author'] h2").each do |a|
md[:authors] << a.text().strip || ""
end
md[:keywords] = ssrn_page.xpath("//b[contains(., 'Keywords:')]/following-sibling::text()").text().strip.gsub(", ",",").gsub(" ", "-") || ""
set_metadata md
new_title = (md[:title].match /[^0-9A-Za-z\-]/).nil? ?
md[:title] :
md[:title].gsub!(/[^0-9A-Za-z\-]/, '-')
File.rename(filename, new_title + ".pdf")
end
def set_metadata(md)
`xattr -w "com.apple.metadata:kMDItemTitle" "#{md[:title]}" #{md[:filename]}`
`xattr -w "com.apple.metadata:kMDItemAuthors" "#{md[:authors].join(", ")}" #{md[:filename]}`
`/usr/local/bin/tag --add #{md[:keywords]} #{md[:filename]}`
end
end
SSRNImporter.new