-
Notifications
You must be signed in to change notification settings - Fork 14
/
hash-test.rb
81 lines (75 loc) · 1.76 KB
/
hash-test.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'digest/md5'
require 'digest/sha1'
require 'digest/sha2'
$:.push(File.dirname($0))
require 'utility-functions'
require 'appscript'
include Appscript
require 'sequel'
# create database if does not exist
dbfile= "/Users/Stian/src/folders2web/pubs.sqlite"
unless File.exists?(dbfile)
DB = Sequel.sqlite("/Users/Stian/src/folders2web/pubs.sqlite")
DB.create_table :items do
primary_key :id
String :hash
String :bibtex
end
else
DB = Sequel.sqlite("/Users/Stian/src/folders2web/pubs.sqlite")
end
@bd = app("BibDesk").document
@dbase = DB[:items]
### INSERT ENTRY
def insert_entry(fname, bibtx = "")
hash = hashsum(fname)
citekey = File.basename(fname)[0..-5]
unless bibtx.size > 0
begin
bibtx = @bd.search({:for =>citekey})[0].BibTeX_string.get.to_s
rescue
puts "Citekey #{citekey} not found in BibDesk"
return -1
end
end
puts "Inserting hash #{hash} for file #{fname}"
@dbase.insert({:hash => hash, :bibtex => bibtx.to_s})
end
if ARGV[0] == "batch"
c=0
@dbase = DB[:items]
path = "/Volumes/SSDHome/Users/Stian/Bibdesk/*.pdf"
Dir[path].select do |f|
fname = File.basename(f)
citekey = fname[0..-5]
puts citekey
begin
bibtx = @bd.search({:for =>citekey})[0].BibTeX_string.get.to_s
rescue
puts "Not found"
next
end
hash = hashsum(fname)
c = c+1
end
puts "Total #{c} entries added"
elsif ARGV[0] == "lookup"
f = ARGV[1]
hash = hashsum(f)
puts hash
result = @dbase.filter[:hash => hash]
unless result == nil
puts result[:bibtex]
else
puts "Nothing found"
end
elsif ARGV[0] == "hash"
puts hashsum(ARGV[1])
elsif ARGV[0] == "insert"
if ARGV[2].size > 0
bibtex = File.read(ARGV[2])
else
bibtex = ''
end
insert_entry(ARGV[1],bibtex)
end